Page 1 of 1

Problem converting a non-standard sized document to PDF

PostPosted: Mon Sep 26, 2016 1:34 am
by gizwhu
I am having problems converting a non-standard sized document to pdf. Below is the stacktrace. Any help is appreciated.

Code: Select all
[2016-09-25 22:16:14.539] The contents of fo:region-body on page 4 exceed its viewport by 21238 millipoints. (See position 1:448)

[2016-09-25 22:16:14.539] The contents of fo:region-body on page 3 exceed its viewport by 21238 millipoints. (See position 1:448)

[2016-09-25 22:16:14.540] The contents of fo:region-body on page 2 exceed its viewport by 21238 millipoints. (See position 1:448)

[2016-09-25 22:16:14.540] The contents of fo:region-body on page 1 exceed its viewport by 21238 millipoints. (See position 1:448)

[2016-09-25 22:16:14.549] Using repackaged ToXMLStream

[2016-09-25 22:16:14.549] Using repackaged ToXMLStream

[2016-09-25 22:16:14.557] No theme part - default to Calibri

[2016-09-25 22:16:14.571] Exception exporting package

org.docx4j.openpackaging.exceptions.Docx4JException: Exception writing Document to OutputStream: org.apache.fop.fo.ValidationException: "fo:flow" is missing child elements. Required content model: marker* (%block;)+ (See position 1:958)
   at org.docx4j.utils.XmlSerializerUtil.serialize(XmlSerializerUtil.java:31)
   at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:209)
   at org.docx4j.convert.out.fo.renderers.FORendererApacheFOP.render(FORendererApacheFOP.java:159)
   at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:168)
   at org.docx4j.convert.out.fo.AbstractFOExporter.postprocess(AbstractFOExporter.java:47)
   at org.docx4j.convert.out.common.AbstractExporter.export(AbstractExporter.java:82)
   at org.docx4j.Docx4J.toFO(Docx4J.java:568)



Here is my test code:
Code: Select all
        String regex = null;
        PhysicalFonts.setRegex(regex);
       
        WordprocessingMLPackage wordMLPackage;
       
        InputStream is = new ByteArrayInputStream(document);
        ByteArrayOutputStream baos = null;
        try {
            wordMLPackage = WordprocessingMLPackage.load(is);
           
             wordMLPackage = wordMLPackage.createPackage(PageSizePaper.A5, false);           
                   
            FieldUpdater updater = new FieldUpdater(wordMLPackage);
            updater.update(true);
           
            Mapper fontMapper = new IdentityPlusMapper();
            wordMLPackage.setFontMapper(fontMapper);
           

           
            FOSettings foSettings = Docx4J.createFOSettings();
            foSettings.setWmlPackage(wordMLPackage);
           
            /*OutputStream os = new java.io.FileOutputStream("C:/test.pdf");
            Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
            os.close();*/
               
            baos = new ByteArrayOutputStream();
           
            Docx4J.toFO(foSettings, baos, Docx4J.FLAG_EXPORT_PREFER_XSL);
            byte[] pdf = baos.toByteArray();
           
            updater = null;
            foSettings = null;
            wordMLPackage = null;
           
            return pdf;
        } catch (Docx4JException e) {
            e.printStackTrace();
            LOG.error(e.getMessage(), e);
        }

Re: Problem converting a non-standard sized document to PDF

PostPosted: Mon Sep 26, 2016 2:31 pm
by jason
To understand what is happening, you should enable saving the intermediate .FO document, then inspect the fo:flow

Re: Problem converting a non-standard sized document to PDF

PostPosted: Mon Sep 26, 2016 2:39 pm
by gizwhu
Thanks for your quick reply. How do I enable this?

Re: Problem converting a non-standard sized document to PDF

PostPosted: Tue Sep 27, 2016 5:12 pm
by jason
Something like foSettings.setFoDumpFile(new java.io.File(inputfilepath + ".fo"));

or foSettings.setApacheFopMime(FOSettings.INTERNAL_FO_MIME) to get FO instead of PDF:

Code: Select all
      // Document format:
      // The default implementation of the FORenderer that uses Apache Fop will output
      // a PDF document if nothing is passed via
      // foSettings.setApacheFopMime(apacheFopMime)
      // apacheFopMime can be any of the output formats defined in org.apache.fop.apps.MimeConstants eg org.apache.fop.apps.MimeConstants.MIME_FOP_IF or
      // FOSettings.INTERNAL_FO_MIME if you want the fo document as the result.
      //foSettings.setApacheFopMime(FOSettings.INTERNAL_FO_MIME);