1

With the documents4j-server running and listening at http://localhost:9998 is it possible to convert a document with a direct HTTP command?

Example:

http://localhost:9998?source=C:\Test.doc?target=C:\Test.pdf

More info:

I was a few steps ahead of myself...

I am using Apache FOP servlet running on Apache-Tomcat as a service to generate PDF documents from XML / XSLT.

Once running a PDF can be generated via http.

Example:

http://localhost:8080/fop/
  ?xml=C:/temp/Test.xml
  &xslt=C:/temp/Test-Style-Sheet.xsl
  &pdf=C:/temp/Test.pdf

I execute this command from my database application (which sets up the XML source and manages the resultant PDF).

I was looking for the ability to do something similar with documents4j for Word Doc to PDF conversion.

So I now realise that what I actually need is the ability to pass the name/type of the source document and the type of conversion (plus any other required parameters) to an external program / http port which can then package the request appropriately and then initiate the formal conversion process.

Would anyone be able to provide advice or a solution?

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192

1 Answers1

0

Not the way you attempt it, the conversion server would not be able to read from or write to your file system. No server can do that, this would be a severe security breach.

Instead, you can send the file via HTTP POST as the body of the message, that is what the client does. The answer then contains the converted file as the body of the response. You are using the request headers to specify your request:

  1. For defining the input type, you are using the HTTP Content-Type header.
  2. For defining the requested type, you are using the HTTP Accept header.

As an example, for converting a file from MS Word to PDF, you would for example use application/vnd.com.documents4j.any-msword as an input and application/pdf as the accept header's type.

You can also use the the client implementation that ships with documents4j and which is described under Converter client in the readme. This client sends exactly such a request.

Edit: You would need to set up your own minimal client application for that. A minimal application would look like this:

class MyApp {
  public static void main(String[] args) {
    IConverter converter = LocalConver.make();
    converter
      .convert(new File(args[0])).as(DocumentType.MS_WORD)
      .to(new File(args[1])).as(DocumentType.PDF)
      .execute();
    converter.shutDown();
  }
}

Given that you hand over the first and second command via the command line. Alternatively, you can connect to a server via the RemoteConverter. Of course, you can also use the built in command line tool for that which is however not available via HTTP. You could write a small app that delegates to that command line tool if this was your requirement.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • I must have missed references to the command line tool - can you please provide some info on this. Thanks again. – Graeme Wellington Feb 12 '15 at 09:11
  • You can start the command line tool by `java -jar documents4j-client-standalone-shaded.jar http://localhost:9998`, simply download the shaded jar from http://search.maven.org/#artifactdetails|com.documents4j|documents4j-client-standalone|0.2.1|jar – Rafael Winterhalter Feb 12 '15 at 09:20
  • Thank you - Success! I have got minimal client working using the above code in a very coarse fashion using lots of import lines and referencing both the 'shaded' jars in the java execute command to get the Classpath established. The time to convert a document was noticeably longer than via the web method in the local demo so I will now look into productivity improvements eliminating any introduced overheads. – Graeme Wellington Feb 13 '15 at 00:53
  • It is slower, becausw this client fires up a converter for every single conversion. You want to reuse the same converter instead (such as the web example converter does). – Rafael Winterhalter Feb 13 '15 at 06:34
  • I have run documents4j-client-standalone-shaded.jar and see that it accepts the conversion type and source file / target file from controlled prompts. How would I go about using this jar to pass the required conversion details to and re-use the code so that the conversions run faster. My apologies but I am still in a steep learning curve in this area - any assistance is much appreciated. – Graeme Wellington Feb 20 '15 at 04:02