1

I want to use documents4j to convert Excel files to PDF but there are two issues:

  1. I must open an Excel file when convert. When I convert Word to PDF, I didn't need open a doc/docx file.
  2. The PDFs are not containing a chart imagewhich is contained in Excel.

How can I resolve this? Here is code that reproduces the issue:

private void convertExcelToPDF1() throws Exception {

    InputStream excelFileIS = new BufferedInputStream(new FileInputStream("C:\\test_convert\\test.xlsx"));
    File target = new File("C:\\test_convert\\sim_status_excel.pdf");

    IConverter converter = RemoteConverter.builder()
                       .baseFolder(new File("D:\\temp"))
                       .workerPool(20, 25, 2, TimeUnit.SECONDS)
                       .requestTimeout(10, TimeUnit.SECONDS)
                       .baseUri("http://localhost:9998")
                       .build();

    Future<Boolean> conversion = converter.convert(excelFileIS).as(DocumentType.XLSX)
                                    .to(target).as(DocumentType.PDF)
                                    .prioritizeWith(1000)
                                    .schedule();
}
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
Yang
  • 19
  • 3

1 Answers1

0

MS Excel is not necessarily intended for use of programmatic conversion. Therefore, strange issues can occure. First of all: Did you try to convert your file to PDF by directly using MS Excel? If the issues occure there, too, documents4j is powerless.

Other than that, there might be report-options set that are required for making the export successful. You can have a look at the VBS scripts that documents4j runs for triggering Excel conversion: https://github.com/documents4j/documents4j/tree/master/documents4j-transformer-msoffice/documents4j-transformer-msoffice-excel/src/main/resources

You can save these files locally and run them. First, you run excel_start.vbs from the command line in Windows. Then you run excel_convert.vbs input.xls output.pdf 999 which triggers the to-PDF conversion for the given file. You can clean up by running excel_stop.vbs. after you are done.

The two parameters that are interesting to you are:

excelApplication.Workbooks.Open(inputFile, , True, , , , , , , , , , , , 2)

and

excelDocument.ExportAsFixedFormat xlTypePDF, outputFile

which you can tweak to make your conversion run. I only tested with standard Excel files which work fine in my unit tests. If you find out a solution that works for you, I am happy to merge your changes. The methods (ExportAsFixedFormat and Open) are documented on MSDN.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192