Excel ByteArrayInputStream to pdf

For a webservice I am using aspose cells and aspose words. For the cells part, I get a byte[] (representing a xls or xlsx file) from a service. I have to convert that to a byte[] representing a .pdf file and afterwards I convert it to an inputStream (to pass it to my webservice). After that I want to create a pdf from incomming inputStream. I do the same for the byte[] of the .docx and that works without problems. If I use the ByteArrayOutputStream.writeTo I get that my file is corrupted. If I use my InputStream to write to a file I also get the corrupted message.

I added an attachment with the code snippet.

Hi,

Thanks for the code segment and details.

I have tested your scenario/ case using the following sample code with a simple template Excel file and it works fine. I first get the Excel file into byte array first. The output PDF file is fine tuned.
e.g
Sample code:

byte[] data = FileUtils.readFileToByteArray(new File("f:\files\Book_new1.xlsx"));

ByteArrayInputStream inStream = new ByteArrayInputStream(data);

final Workbook doc = new Workbook(inStream);

// Save the modified document into out stream

final ByteArrayOutputStream baos = new ByteArrayOutputStream();

doc.save(baos, SaveFormat.PDF);


//You can then get the PDF document back into the InputStream as follows

final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());

inputStream.close();



// Use this to convert the stream to a .pdf file

final OutputStream outputStream = new FileOutputStream("f:/files/out1" + ".pdf");

IOUtils.copy(inputStream, outputStream);

outputStream.close();

The template Excel file and output PDF files are attached.

Thank you.

When I import your .xlsx I still get the same problem and I can’t open the pdf. I am using the latest aspose cells version.

Hi,


It might be an issue with your code. Please try my (exact) sample code to evaluate if you still find the issue? It should work fine as I do not find any issue, you may also open my output PDF file which is generated against the sample code I pasted above.

Thank you.

Hi

This is the code I used:

final java.io.File f = new java.io.File(“c:\files\Book_new1.xlsx”);
final byte[] data2 = FileUtils.readFileToByteArray(f);
final ByteArrayInputStream inStream = new ByteArrayInputStream(data2);
final Workbook doc = new Workbook(inStream);
// Save the modified document into out stream
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
doc.save(baos, SaveFormat.PDF);

// You can then get the PDF document back into the InputStream as follows
final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());
inputStream.close();

// Use this to convert the stream to a .pdf file
final OutputStream outputStream = new FileOutputStream(“c:/files/out1” + “.pdf”);
IOUtils.copy(inputStream, outputStream);
outputStream.close();

Still no result, I tried opening the pdf in another reader, same problem. When I change the extension to .xlsx it obviously works.

Hi,

That is really strange as I used the exact code (just changed the file path) as shown below and it works absolutely fine. Please find attached the output PDF file for your reference. Please open it into acrobat reader if you find any issue with the file.
e.g
Sample code:

final java.io.File f = new java.io.File(“f:\files\Book_new1.xlsx”);

final byte[] data2 = FileUtils.readFileToByteArray(f);

final ByteArrayInputStream inStream = new ByteArrayInputStream(data2);

final Workbook doc = new Workbook(inStream);

// Save the modified document into out stream

final ByteArrayOutputStream baos = new ByteArrayOutputStream();

doc.save(baos, SaveFormat.PDF);

// You can then get the PDF document back into the InputStream as follows

final InputStream inputStream = new ByteArrayInputStream(baos.toByteArray());

inputStream.close();

// Use this to convert the stream to a .pdf file

final OutputStream outputStream = new FileOutputStream("f:/files/out1" + ".pdf");

IOUtils.copy(inputStream, outputStream);

outputStream.close();

(Please note, for “IOUtils” class used in the code, I set its class path using Apache Commons IO 2.5 API i.e., “commons-io-2.5.jar” and have imported all the classes using the statement: “import org.apache.commons.io.*;” into my JAVA program at the start. )

Thank you.

Hi,

jvra:
Hi Can you attach the .java file you used and send it to me? I don't know if the imports and the io utils can be the problem.

Please find it attached.

Thank you.