Search code examples
javapdf-generationapache-poidocx

POI - Unexpected end of file after null


I've a problem with converting docx file created with poi to pdf. I create file with next code (just empty file, but I've tried with filled with text/images):

   private static void createEmpty() throws IOException{
         XWPFDocument doc = new XWPFDocument();
         FileOutputStream out = new FileOutputStream("empty.docx");
         doc.write(out);
         out.close();
         doc.close();
   }

The file I ge I could open with Word 2010 and it looks fine, however when I try to convert it to pdf with POI pdfConverter I get an error:

org.apache.poi.xwpf.converter.core.XWPFConverterException: org.apache.xmlbeans.XmlException: error: Unexpected end of file after null
   at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:59)
   at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:37)
   at org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45)
   at sbt.oot.metrics.pdf.DocxToPdfConverter.create(DocxToPdfConverter.java:44)
   at sbt.oot.metrics.pdf.DocxToPdfConverter.main(DocxToPdfConverter.java:23)
Caused by: org.apache.xmlbeans.XmlException: error: Unexpected end of file after null
   at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3471)
   at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1270)
   at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1257)
   at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
   at org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument$Factory.parse(Unknown Source)
   at org.apache.poi.xwpf.usermodel.XWPFDocument.getStyle(XWPFDocument.java:446)
   at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.<init>(XWPFStylesDocument.java:192)
   at org.apache.poi.xwpf.converter.core.styles.XWPFStylesDocument.<init>(XWPFStylesDocument.java:186)
   at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.createStylesDocument(XWPFDocumentVisitor.java:161)
   at org.apache.poi.xwpf.converter.core.XWPFDocumentVisitor.<init>(XWPFDocumentVisitor.java:154)
   at org.apache.poi.xwpf.converter.pdf.internal.PdfMapper.<init>(PdfMapper.java:149)
   at org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:54)
   ... 4 more
Caused by: org.xml.sax.SAXParseException; systemId: file:; lineNumber: 1; columnNumber: 1; Unexpected end of file after null
   at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.reportFatalError(Piccolo.java:1038)
   at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:723)
   at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3439)
   ... 15 more

Here goes the code how I convert it:

// 1) Load docx with POI XWPFDocument
XWPFDocument document = new XWPFDocument(new FileInputStream(new File(docxFile)));
XWPFDocument template = new XWPFDocument(new FileInputStream(new File("Template.dotx")));
XWPFStyles newStyles = document.createStyles();
newStyles.setStyles(template.getStyle());

// 2) Convert POI XWPFDocument 2 PDF with iText
File outFile = new File(pdfFile);
OutputStream out = new FileOutputStream( outFile );
PdfOptions options = null;
PdfConverter.getInstance().convert( document, out, options );

However if I open file in word and save it again - I can convert without issues. Any suggestions?


Solution

  • I've fixed this by using template during creation of docx file:

    XWPFDocument document = new XWPFDocument();
    XWPFDocument template = new XWPFDocument(new FileInputStream(new File("Template.dotx")));
    XWPFStyles newStyles = document.createStyles();
    newStyles.setStyles(template.getStyle());