1

I'm trying convert word to pdf, my code is:

public static void main(String[] args) {
    try  {
        XWPFDocument document = new XWPFDocument();
        document.createStyles();
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun title = paragraph.createRun();
        title.setText("gLETS GO");

        PdfOptions options = PdfOptions.create();
        OutputStream out = new FileOutputStream(new File("C:/Users/pepe/Desktop/DocxToPdf1.pdf"));
        PdfConverter.getInstance().convert(document, out, options);
        System.out.println("Done");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I'm getting error:

fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: org.apache.xmlbeans.XmlException: error: Unexpected end of file after null
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:71)
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:39)

Caused by: org.apache.xmlbeans.XmlException: error: Unexpected end of file 

I have tried other solutions but doesnt works. I create a java project, if someone can help me or other way to do

sirdaiz
  • 250
  • 3
  • 16
  • 2
    See https://stackoverflow.com/questions/51330192/trying-to-make-simple-pdf-document-with-apache-poi/51337157#51337157. There must be a styles document, even if it is empty. – Axel Richter Feb 19 '20 at 13:22
  • I added document.createStyles(); but nothing – sirdaiz Feb 19 '20 at 13:26
  • My linked answer contains two complete working examples. Both are working for me. Just tested again. – Axel Richter Feb 19 '20 at 13:28
  • I tried your first example adapting it to my code but I have problems – sirdaiz Feb 19 '20 at 14:15
  • Maybe you should tell us what the problems are, so we can help you solve them. – Nand Feb 19 '20 at 14:35
  • I edited my question, hope can help me – sirdaiz Feb 19 '20 at 15:36
  • See https://stackoverflow.com/questions/51330192/trying-to-make-simple-pdf-document-with-apache-poi/51337157#51337157. There must be section properties for the page having at least the page size set. And because we need changing the underlying low level objects, the document must be written so underlying objects will be committed. Else the XWPFDocument which we hand over the PdfConverter will be incomplete. – Axel Richter Feb 19 '20 at 18:49
  • Does this answer your question? [How to convert MS Word to PDF in the web browser](https://stackoverflow.com/questions/14806515/how-to-convert-ms-word-to-pdf-in-the-web-browser) – archzi Sep 14 '22 at 03:40

2 Answers2

1

This is probably a duplicate of Trying to make simple PDF document with Apache poi. But let's have a complete example again to show how to create a new XWPFDocument from scratch using the latest apache poi 4.1.2 which then can be converted to PDF using PdfConverter of fr.opensagres.poi.xwpf.converter version 2.0.2 and iText.

As told the default *.docx documents created by apache poi lacks some content which PdfConverter needs.

There must be a styles document, even if it is empty.

And there must be section properties for the page having at least the page size set. To fulfilling this we must add some code additionally in our program. Unfortunately this then needs the full jar of all of the schemas ooxml-schemas-1.4.jar as mentioned in Faq-N10025.

And because we need changing the underlying low level objects, the document must be written so underlying objects will be committed. Else the XWPFDocument which we hand over the PdfConverter will be incomplete.

Minimal complete working example:

import java.io.*;
import java.math.BigInteger;

//needed jars: fr.opensagres.poi.xwpf.converter.core-2.0.2.jar, 
//             fr.opensagres.poi.xwpf.converter.pdf-2.0.2.jar,
//             fr.opensagres.xdocreport.itext.extension-2.0.2.jar,
//             itext-4.2.1.jar                                   
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;

//needed jars: apache poi and it's dependencies
//             and additionally: ooxml-schemas-1.4.jar 
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.Units;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

public class XWPFToPDFConverterSampleMin {

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument();

  // there must be a styles document, even if it is empty
  XWPFStyles styles = document.createStyles();

  // there must be section properties for the page having at least the page size set
  CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
  CTPageSz pageSz = sectPr.addNewPgSz();
  pageSz.setW(BigInteger.valueOf(12240)); //12240 Twips = 12240/20 = 612 pt = 612/72 = 8.5"
  pageSz.setH(BigInteger.valueOf(15840)); //15840 Twips = 15840/20 = 792 pt = 792/72 = 11"

  // filling the body
  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun title = paragraph.createRun();
  title.setText("gLETS GO");

  //document must be written so underlaaying objects will be committed
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  document.write(out);
  document.close();

  document = new XWPFDocument(new ByteArrayInputStream(out.toByteArray()));
  PdfOptions options = PdfOptions.create();
  PdfConverter converter = (PdfConverter)PdfConverter.getInstance();
  converter.convert(document, new FileOutputStream("XWPFToPDFConverterSampleMin.pdf"), options);

  document.close();

 }
}
Axel Richter
  • 56,077
  • 6
  • 60
  • 87
0

I would not suggest you to use apache poi since its library to convert word to pdf have been discontinued now. As of today I don't think that there is any open source library which do the conversion (they require some dependencies like some need MS word to be installed, etc). The best way I could think of (it will only work if you are deploying project on linux machine) is that install Libre Office (open source) in the linux machine and run this :

  String command = "libreoffice --headless --convert-to pdf " + inputPath + " --outdir " + outputPath;
 
 try {
         Runtime.getRuntime().exec(command);
      } catch (IOException e) {
         e.printStackTrace();
      }
Anmol Jain
  • 336
  • 4
  • 12