48433

Issue with JODConverter and running LibreOffice in headless mode

Question:

I am using the following code to convert .doc to .pdf using JOD.

File inputFile = new File("document.doc"); File outputFile = new File("document.pdf"); // connect to an OpenOffice.org instance running on port 8100 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); connection.connect(); // convert DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile); // close the connection connection.disconnect();

But I have to run

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

separately to start LibreOffice in headless mode.

Is there a way to start LibreOffice programmatically? Or, can't we just give the path to LibreOffice folder to JOD to do the conversion?

Answer1:

One way would be to wrap your cmd instruction

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

as java process, see <a href="https://stackoverflow.com/questions/7248405/java-execute-cmd-c-start-path-with-spaces-program-exe" rel="nofollow">this</a> question on SO.

Solution could be:

File inputFile = new File("document.doc"); File outputFile = new File("document.pdf"); String[] args = {"cmd","/c","\\path to libreoffice executable","-headless", "-accept='socket,host=127.0.0.1,port=8100;urp;'", "-nofirststartwizard"}; try{ Runtime rt = Runtime.getRuntime(); ProcessBuilder pb = new ProcessBuilder(args); Process pr = pb.start(); // connect to an OpenOffice.org instance running on port 8100 OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); connection.connect(); }catch{Exception e){ } // convert DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(inputFile, outputFile); // close the connection connection.disconnect();

It is ad-hoc and not tested solution but it might work. Another option is to make batch file in windows or shell script in linux with your cmd command and set it to auto start on windows or linux login. After that execute your code as it is...

Answer2:

You don't need JOD at all to convert a doc file to a PDF. This can be directly done with LibreOffice:

libreoffice --headless --convert-to pdf document.doc

Recommend

  • Unhandled promise rejection (rejection id: 1): Error: kill ESRCH
  • Image resources disappearing from react-native app
  • How to send an std::vector of unsigned char over an UDP socket using boost asio?
  • Socket.io on socket disconnect
  • Getting socket.io namespace from anywhere in the project
  • How to require a namespace programmatically
  • How to read data from socket connection - android
  • Cannot send user message with Spring Websocket
  • How to make a npm command, that executes two commands in the console (one after another)?
  • google-maps-sdk-ios - arm 7/iphone 5 issues for Google Maps 1.4.3 for iOS
  • Issue when joining serf nodes located in different Docker containers
  • TensorFlow C++, runtime issue
  • Use allowDiskUse in criteria query with Grails and the MongoDB plugin?
  • What is Closure Compiler?
  • Getting zero results in search using elastic4s
  • Android NFC read Tags issue. Activity starts each time on data received
  • openssl handshake failed
  • Ruby: FileUtils.cp truncates file; FileUtils.mv it does not?
  • How to resolve docker host names (/etc/hosts) in containers
  • Update varbinary(MAX) field in SQLServer 2012 Lost Last 4 bits
  • Setting src to Base64-encoded image with Javascript is failing
  • NSMutableArray instance used in a block
  • XOR with Neural Networks (Matlab)
  • Error in making a socket connection
  • pymongo replication secondary readreference not work
  • What causes the runtime difference in this trivial fortran code?
  • Is there a package like bigmemory in R that can deal with large list objects?
  • netsh acl setting (need alternative method - registry settings?)
  • Yii2: Finding file and getting path in a directory tree
  • Clear activity stack before launching another activity
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • How do I configure context broker accept post requests from my remote sensor?
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Running a C# exe file
  • Perl system calls when running as another user using sudo
  • how to add data labels for bar graph in matlab
  • need help with bizarre java.net.HttpURLConnection behavior
  • Reading document lines to the user (python)