Generated PDF from Xlsx Cannot be Open

I’ve worked a simple Unit Test to convert a very simple Xlsx document to PDF. The test seams to work ok, but when I try to open the generated document, I get the following message (Mac Preview):


The file “converted.pdf” could not be opened.
It may be damaged or use a file format that Preview doesn’t recognize.

The Unit Test is as follows:

public void doIt() throws FileNotFoundException, FileFormatException
<span style=“font-family: “Courier New”;”>{
String file = “/sampledocs/contracts.xlsx”;
String converted = “testoutputs/converted.pdf”;

InputStream in = this.getClass().getResourceAsStream(file);
OutputStream out = new FileOutputStream(converted);

xlsxToPdf(in, out);
<span style=“font-family: “Courier New”;”>}

public void xlsxToPdf(InputStream inputStream, OutputStream outputStream) throws FileFormatException
{
try
{
Workbook wb = new Workbook(inputStream);
wb.save(outputStream, SaveFormat.PDF);
}
catch (Exception e)
{
throw new FileFormatException(“Failed to convert file from XLSX to PDF (using Aspose)”, e);
}
}

Like stated, no error messages are displayed, and all seams to be working fine.

Some enviroment info:
Using Java API v16.12.0
From Maven
Running on OSX El Capitan
In file (simplest.xlsx) and out (converted.pdf) annexed in issue

Thanks

Hi,


Thanks for your posting and using Aspose.Cells.

Your converted.pdf is not a pdf file, it is actually an excel file. Please rename it to converted.pdf.xlsx and it will open fine in Microsoft Excel.

Converting excel into pdf file is simple and your code also looks good. Please try the following sample code at your end in a separate program and see the results.

Java
Workbook wb = new Workbook(“sample.xlsx”);
wb.save(“output.xlsx”, SaveFormat.XLSX);

Thank you, with that observation and suggestion I noticed that they are different distributions for SaveFormat (depending on the package). Since in the same class I had other conversion tests, including Word, it was using the SaveFormat from the Word package. To fix, I changed the code to explicitly use the one needed:


Workbook wb = new Workbook(inputStream);
wb.save(outputStream, com.aspose.cells.SaveFormat.PDF);

That solved it… Thanks

Hi again,


Thank you for the confirmation. It is good to know that you are up & running again. Please feel free to contact us back in case you need our further assistance with Aspose APIs.

Please note, each Aspose API has its own version of the SaveFormat class therefore it advised to use fully qualified namespaces if you are using more than one Aspose APIs in single project.