Page 1 of 1

PDF Conversion takes too long

PostPosted: Fri Nov 21, 2014 5:30 pm
by java_dev
Hi,
I'm using Docx4J to convert docx files to pdf. But the conversion seems to take too long, approximately 20 secs for one document.

All of my docx files are just one page documents and the resulting PDF files are ~ 100 KB. I tried to do multi-threading to achileve higher conversion rate but it seems to make it worse as the time taken jumps to 40 secs. My save to PDF code looks like below. Please suggest me some way to decrease the conversion time.

Code: Select all
Mapper fontMapper;
File dir = new File(System.getProperty("user.dir") + "/fonts");
File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
   for (File child : directoryListing) {
      PhysicalFonts.addPhysicalFont(child.toURL());
   }
}
fontMapper = new IdentityPlusMapper();

wordMLPackage.setFontMapper(fontMapper);
      
FOSettings foSettings = Docx4J.createFOSettings();

foSettings.setWmlPackage(wordMLPackage);

String outputfilepath;
outputfilepath = System.getProperty("user.dir") + "/Output.pdf";   
OutputStream os = new java.io.FileOutputStream(outputfilepath);

Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

My environment is Unix with Java 1.6

Re: PDF Conversion takes too long

PostPosted: Mon Nov 24, 2014 5:32 pm
by jason
Are you starting a new VM each time? If so you are incurring JAXB startup time needlessly...

Also remember there are 2 steps:

step 1: docx to XSL FO

step 2: XSL FO to PDF.

Docx4J.FLAG_EXPORT_PREFER_XSL specifies you want to use XSLT for step 1. The alternative, Docx4J.FLAG_EXPORT_PREFER_NONXSL is faster, but not yet at feature parity.

You can perform the processing in the background, and get notified when it is done (Docx4jEvent).

Apart from that, it is what it is...it just takes a while (bot not 20 secs!) to do those 2 steps.

Your alternative is to invest time/effort in doing it using iText, or some non-docx4j approach, perhaps using the http://wordsdk.com/ component or LibreOffice/OpenOffice.