Page 1 of 1

RunFontSelector Exception with PDF conversion

PostPosted: Tue Dec 17, 2013 6:23 am
by todd
I'm using latest docx4j-3.0.0 with fop-1.1
I programatically generate a document using docx4j.
I set the following document defaults when generating the document.

Code: Select all
Styles styles = dleReportMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getJaxbElement();   
        DocDefaults docDefaults = styles.getDocDefaults();
        RFonts defaultRPrRFonts = docDefaults.getRPrDefault().getRPr().getRFonts();
        defaultRPrRFonts.setAscii(TIMES_NEW_ROMAN);
        defaultRPrRFonts.setHAnsi(TIMES_NEW_ROMAN);     
        defaultRPrRFonts.setCs(TIMES_NEW_ROMAN);


I then run the sample ConvertToPDF code to convert the document, I get no errors.
Code: Select all
public boolean convert(WordprocessingMLPackage wMLP) throws Docx4JException {

      // Font regex (optional)
      // Set regex if you want to restrict to some defined subset of fonts
      // Here we have to do this before calling createContent,
      // since that discovers fonts
      // String regex = null;
      // Windows:
      String regex = ".*(calibri|cour|arial|times|comic|georgia|impact|LSANS|pala|tahoma|trebuc|verdana|symbol|webdings|wingding).*";
      PhysicalFonts.setRegex(regex);

      // Document loading (required)
      // Set up font mapper (optional)
         
      Mapper fontMapper = new IdentityPlusMapper();
      try {
         wMLP.setFontMapper(fontMapper);
      } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
         
      // .. example of mapping missing font Algerian to installed font
      // Comic
      // Sans MS
      PhysicalFont font = PhysicalFonts.getPhysicalFonts().get(
            "Times New Roman");
      fontMapper.getFontMappings().put("Calibri", font);
         
      // New code
      FOSettings foSettings = Docx4J.createFOSettings();
      if (saveFO) {
         foSettings.setFoDumpFile(new java.io.File(DIR_OUT + inputfile + ".fo"));
      }
      foSettings.setWmlPackage(wMLP);

      // exporter writes to an OutputStream.
      try {
         String outputFile = FilenameUtils.removeExtension(inputfile) + ".pdf";
         OutputStream os = new java.io.FileOutputStream(DIR_OUT + outputFile);
         // Don't care what type of exporter you use
         Docx4J.toFO(foSettings, os, Docx4J.FLAG_NONE);
         System.out.println("Saved " + DIR_OUT + outputFile);
      } catch (FileNotFoundException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      // end new code

      return false;
   }


If I open the original generated word docucent and just add a simple character and save in Word 2010.
I then run the ConvertToPDF code to try and convert to PDF, I get a NULL pointer error with the default font settings.
The error exception is a NULL pointer from the RunFontSelector when retriving getPhysicalFont(getDafaultFont())
When retriving the PropertResolver, I notice, in debug, the documentDefaultPPr and documentdeaultRPr values are null.
This is then causing the NULL pointer exception when calling propertyResolver.getDocumentDefaultRPr().getRFonts();
The online ConvertPDF converter tool, running docx4j 2.8, is not having this issue when converting this document.
It appears to be failing with the new docx4j 3.0.0 version.
It also appears, when I save the document in word 2010, it is overriding my deafult document settings (from above). Using Open XML 2.5 toolkit to validate the document code.
Document enclosed

Please advise.
Thanks

Re: RunFontSelector Exception with PDF conversion

PostPosted: Tue Dec 17, 2013 8:11 am
by jason
What happens if you use http://www.docx4java.org/docx4j/docx4j- ... 131211.jar instead of 3.0?

Re: RunFontSelector Exception with PDF conversion

PostPosted: Tue Dec 17, 2013 9:36 am
by todd
Jason,

I linked in the requested jar, but still get the same error.
I was also able to duplicate in a simpler manner.
I created a simple document with the following code.
Code: Select all
   WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
   wordMLPackage.getMainDocumentPart().addParagraphOfText("Hello Word!");
   wordMLPackage.save(new java.io.File("C:\\docx4java\\Test PDF\\HelloWord3.docx"));

Then open word and just modified the string. Saved.
Then ran the pdf converter code.
Same error with statement.
Code: Select all
org.docx4j.wml.RFonts rFonts = propertyResolver.getDocumentDefaultRPr().getRFonts();


Thanks

Re: RunFontSelector Exception with PDF conversion

PostPosted: Tue Dec 17, 2013 1:28 pm
by jason
The StyleDefinitionsPart contains something like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<w:styles  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" >
  <w:docDefaults>
    <w:rPrDefault>
      <w:rPr>
        <w:rFonts w:asciiTheme="minorHAnsi" w:eastAsiaTheme="minorHAnsi" w:hAnsiTheme="minorHAnsi" w:cstheme="minorBidi"/>
        <w:sz w:val="22"/>
        <w:szCs w:val="22"/>
        <w:lang w:val="en-US" w:eastAsia="en-US" w:bidi="ar-SA"/>
      </w:rPr>
    </w:rPrDefault>
    <w:pPrDefault>
      <w:pPr>
        <w:spacing w:after="200" w:line="276" w:lineRule="auto"/>
      </w:pPr>
    </w:pPrDefault>
  </w:docDefaults>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


For ease of processing the style hierarchy, docx4j creates a style called DocDefaults based on that, ie something like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  <w:style w:type="paragraph" w:customStyle="1" w:styleId="DocDefaults">
    <w:name w:val="DocDefaults"/>
    :
  </w:style>
 
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


For some reason, your saved docx was created with that element being empty.

I've made some changes to handle this case; see https://github.com/plutext/docx4j/commi ... 2a8e1fa287

I'll create a nightly jar containing this code tonight.

Thanks for your report, which has improved docx4j!

Re: RunFontSelector Exception with PDF conversion

PostPosted: Wed Dec 18, 2013 5:24 am
by todd
Thanks Jason, I will be looking for the new build.

Re: RunFontSelector Exception with PDF conversion

PostPosted: Wed Dec 18, 2013 5:23 pm
by jason

Re: RunFontSelector Exception with PDF conversion

PostPosted: Thu Dec 19, 2013 12:37 pm
by todd
Jason,

That solved the issue.
Thanks