0

I'm trying to convert a docx file to pdf, docx is a template with header, text and pictures that has been edited through docx4j. This is the piece of code for conversion...

private static void wordClientToPDF() throws FileNotFoundException, IOException
{
    try {

        // 1) Load DOCX into WordprocessingMLPackage
        InputStream is = new FileInputStream(new File(WORD_FILE_PATH_CLIENT));
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);

        // 2) Prepare Pdf settings
        PdfSettings pdfSettings = new PdfSettings();

        // 3) Convert WordprocessingMLPackage to Pdf
        OutputStream out = new FileOutputStream(new File(PDF_FILE_PATH_CLIENT));
        PdfConversion converter = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
        converter.output(out, pdfSettings);

    } catch (Throwable e) {
        //YURY: print stack trace in message
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        sw.toString(); // stack trace as a string
        javax.swing.JOptionPane.showMessageDialog(panel, "PDF conversion error: " + sw.toString());
    }
}

However, I'm receiving the following error DocxToPdfConversionError

Any help is appreciated.

Kara
  • 6,115
  • 16
  • 50
  • 57
Yury Lankovskiy
  • 143
  • 3
  • 14
  • Looks like your docx4j has problems finding the correct form in your MicrosoftFontsRegistry... Maybe you can give it a try using a very common font like Arial or TimesNewRoman? – Mathias G. Feb 21 '14 at 09:09
  • So basically change font inside my docx file, did I understand you correctly? – Yury Lankovskiy Feb 21 '14 at 09:11
  • Changed font to Times New Roman but still run into the same error. – Yury Lankovskiy Feb 21 '14 at 09:15
  • Hmm, looks like you are running on a Mac. Null handling needs to be added in PhysicalFonts at line 438. But better MicrosoftFonts.xml should be altered so that each entry includes @mac. See now https://github.com/plutext/docx4j/issues/108 – JasonPlutext Feb 21 '14 at 09:48
  • I cannot patch this up myself can I? – Yury Lankovskiy Feb 21 '14 at 09:55
  • @JasonPlutext Do you know that the patch will work? Have you tried it? – Yury Lankovskiy Feb 23 '14 at 17:23
  • I have tried running my program on a Windows machine and although it creates PDF file it has errors converting images and this is what it prints on the document: "TODO for fo:float INcapable renderer, support no-wrap + mso-position-vertical-relative=line". Any thoughts? – Yury Lankovskiy Feb 24 '14 at 07:58
  • There is no "patch" yet; I thought I might write one tomorrow...FOP does not support fo:float; an imperfect workaround for the case noted in the comment may be possible. If your documents include this sort of thing, you might want to see whether LibreOffice/OpenOffice gives better PDF output. Please let us know your findings, thanks. – JasonPlutext Feb 24 '14 at 10:37
  • http://www.docx4java.org/docx4j/docx4j-nightly-20140225.jar fixes the Mac NPE – JasonPlutext Feb 25 '14 at 09:24
  • @JasonPlutext Right now I'm converting docx files generated by application to pdf manually with microsoft word, it generates output as expected. I would like to have pdf produced on both mac and windows systems though by my application. – Yury Lankovskiy Feb 25 '14 at 15:21

0 Answers0