I have used aspose-words and document4j to convert Word to PDF, but they depend on the rendering engine to get the layout. There is a difference in the fonts, font sizes, and format of Word and PDF files generated on UNIX compared to Windows. I tried using Apache POI, but the conversion was not good.


    //using document4j
    InputStream docxInputStream =new FileInputStream("input.docx");
    OutputStream outputStream = new FileOutputStream("output.pdf");
    IConverter converter = LocalConverter.builder().build();
    outputStream.close();
    converter.convert(docxInputStream).as(DocumentType.DOCX)
    .to(outputStream).as(DocumentType.PDF).execute();
    converter.shutdown()


    //using aspose word
    Document doc=new Document(new FileInputStream(filePath));
    String outputFilePath="output.pdf";
    doc.save(outputFilePath,SaveFormat.PDF);


    //using apache poi
    InputStream doc = new FileInputStream(new File(docPath));
    XWPFDocument document = new XWPFDocument(doc);
    PdfOptions options = PdfOptions.create();
    OutputStream out = new FileOutputStream(new File(pdfPath));
    PdfConverter.getInstance().convert(document, out, options);

I want to convert my Word document to a PDF. The Word documents are created on Windows, and the code will run on a UNIX server. I need a way to convert Word (all extensions) to PDF with an inbuilt method using an external open source library such that if I ran it on Windows or UNIX, the PDF created should have the same font, font size, and number of pages.

1

There are 1 best solutions below

0
On BEST ANSWER

MS Word documents are flow documents by their nature and does not contain any information about document layout. So their appearance depend on the fonts available to the consumer application and on the rendering engine used by application.

To get the same rendering result on Windows and Unix machines, the same fonts must be availabe on both machines. Otherwise consumer applications (MS Word or OpenOffice) the same as Aspose.Words substitutes the missed fonts with ones that are availabe. Due to the font metrics difference there might be document layout differences after rendering document. In Aspose.Words You can implement IWarningCallback to get a notification when font substitution is performed. Please see Aspose.Words documentation to lean how to install fonts on Linux or specify fonts source.

I am afraid it is impossible to build accurate document layout and render MS Word document to PDF without fonts.