1

I'm trying to use docx4j 3.0 to load an existing docx file, make a few simple textual changes and save as PDF. My code looks like this:

        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load (new File (template));
        MainDocumentPart doc = wordMLPackage.getMainDocumentPart ();

        try
        {
            VariablePrepare.prepare (wordMLPackage);
        }
        catch (Exception e)
        {
            System.err.println ("Warning: VariablePrepare failed: " + e);
        }

                    (here I enumerate all Text nodes and make changes as required)

        PdfConversion c = new Conversion (wordMLPackage);
        try (FileOutputStream out = new FileOutputStream (outputFile)) {
            c.output (out, new PdfSettings ());
        }

This is producing the following exception:

org.docx4j.openpackaging.exceptions.Docx4JException: Exception exporting package
    at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:79)
    at org.docx4j.Docx4J.toFO(Docx4J.java:467)
    at org.docx4j.Docx4J.toPDF(Docx4J.java:477)
    at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.output(Conversion.java:70)
    at net.meridiandigital.binco.invoicegen.InvoiceGenerator.generateFile(InvoiceGenerator.java:80)
    at net.meridiandigital.binco.invoicegen.Test.main(Test.java:46)
Caused by: org.docx4j.openpackaging.exceptions.Docx4JException: Exception loading default template "org/docx4j/convert/out/fo/docx2fo.xslt", Cannot convert argument/return type in call to method 'org.docx4j.convert.out.common.XsltCommonFunctions.notImplemented(reference, node-type, string)'
    at org.docx4j.convert.out.common.AbstractXsltExporterDelegate.loadDefaultTemplates(AbstractXsltExporterDelegate.java:92)
    at org.docx4j.convert.out.common.AbstractXsltExporterDelegate.getDefaultTemplate(AbstractXsltExporterDelegate.java:75)
    at org.docx4j.convert.out.common.AbstractXsltExporterDelegate.getTemplates(AbstractXsltExporterDelegate.java:66)
    at org.docx4j.convert.out.common.AbstractXsltExporterDelegate.process(AbstractXsltExporterDelegate.java:57)
    at org.docx4j.convert.out.common.AbstractWmlExporter.process(AbstractWmlExporter.java:63)
    at org.docx4j.convert.out.common.AbstractWmlExporter.process(AbstractWmlExporter.java:32)
    at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:71)
    ... 5 more
Caused by: javax.xml.transform.TransformerConfigurationException: Cannot convert argument/return type in call to method 'org.docx4j.convert.out.common.XsltCommonFunctions.notImplemented(reference, node-type, string)'
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:945)
    at org.docx4j.XmlUtils.getTransformerTemplate(XmlUtils.java:842)
    at org.docx4j.convert.out.common.AbstractXsltExporterDelegate.loadDefaultTemplates(AbstractXsltExporterDelegate.java:88)
    ... 11 more

Any ideas what's going wrong?

1 Answer 1

2

It shouldn't be using

com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl

but rather

org.apache.xalan.processor.TransformerFactoryImpl

(You need the real Xalan on your classpath)

See also instantiateTransformerFactory() at line 224 of https://github.com/plutext/docx4j/blob/master/src/main/java/org/docx4j/XmlUtils.java

4
  • Thanks. It's rather unfortunate that the logging system you're using decides not to display helpful messages like the one you've included there by default (my personal preference would be a system that displays all messages unless configured otherwise) and that as I haven't figured out how to configure logging yet, I missed it. :(
    – Jules
    Jan 24, 2014 at 13:31
  • Interestingly, though, I did have xalan-2.7.1.jar on my classpath... but it has a transitive dependency on 'serializer-2.7.1.jar' which I had somehow missed.
    – Jules
    Jan 24, 2014 at 13:36
  • (and, yes, I know I really should be using maven...)
    – Jules
    Jan 24, 2014 at 13:37
  • In fairness, log.error("Warning: Xalan jar missing from classpath; xslt not supported",e); is pretty clear!
    – Ben
    Feb 11, 2014 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.