We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Problem converting (Excel)XML from InputStream to Pdf using XMLDocument (Aspose Pdf 2.5.0)

Hello,

I get the (Excel) XML from an Aspose Cells workbook as a generic java.io.InputStream. I then need to convert the stream’s xml into Pdf using Aspose Pdf 2.5.0. I thought that XMLDocument was the right way to do it, but someway the line “pdf1.bindXML(xmlDoc, null);” would throw a NullPointer:

Exception in thread “main” java.lang.NullPointerException
at aspose.pdf.e.ap.a(Unknown Source)
at aspose.pdf.e.ap.a(Unknown Source)
at aspose.pdf.Pdf.bindXML(Unknown Source)
at TestPdf.main(TestPdf.java:29)

Here’s the code I used (find the java+xml file attached as well):

Pdf pdf1 = new Pdf();
pdf1.setTruetypeFontDirectoryPath(“fonts”);

FileInputStream fin = new FileInputStream(“test.xml”);
InputStream xmlIn = (InputStream) new BufferedInputStream(fin);

MemStream mem = new MemStream();

byte[] b = new byte[1024];
int read;
while ((read = xmlIn.read(b)) != -1) {
mem.write(b, 0, read);
}

XMLDocument xmlDoc = new XMLDocument();
xmlDoc.load(mem);

pdf1.bindXML(xmlDoc, null);

pdf1.save(“test.pdf”);

Please, can you tell me my mistake or what I can do to make this work? We do not want to use temporary xml files and, thus, need to create the pdfs straight from the stream.

Thank you for your help
Lennart

Hello Lennart,

I have tested the scenario using the following code snippet and I am able to convert the XML document into PDF without any problem. I think there is some issue with the code that you are using. In case you don't need to save the intermediate XML document, please save the output of Aspose.Cells into MemoryStream object and then transform it into InputStream so that it can be used with BindXML method.

[Java]

Pdf pdf = new Pdf();
pdf.bindXML("D:/pdftest/excel-xml-stream-to-pdf/test.xml", null);
pdf.save("D:/pdftest/excel-xml-stream-to-pdf/test.pdf");

As a workaround, you may even delete the intermediate XML file once Aspose.Pdf have transformed it.

We are sorry for your inconvenience.

Hello Nayyer,

thank you for your reply, but it didn’t quite help me. My problem is that I only have a simple, native java.io.InputStream from which I can read XML markup as delivered from Aspose Cells. Anything prior to that point is out of my control, so there is no intermediary file, and I do not have any direct access to the Aspose Cells workbook object where the xml data came from.

So, again, what I need a way to get the xml data from an inputstream (not a file!) into an aspose.pdf.MemStream and then use the latter to someway get the xml data into the aspose.pdf.Pdf object (via an aspose.pdf.XMLDocument I thought).

I was unable to find useful documentation on XMLDocument, so if I missed something, maybe you could send me a link to it.

Are you able to somehow get the following code work? It crashes as soon I try to bind the xmlDoc to the pdf object, and I don’t see what is wrong here.

Pdf pdf1 = new Pdf();
pdf1.setTruetypeFontDirectoryPath(“fonts”);

FileInputStream fin = new FileInputStream(“test.xml”);
InputStream xmlIn = (InputStream) new BufferedInputStream(fin);

// <-- anything above this line is out of my control! All I have is a simple InputStream

MemStream mem = new MemStream();
byte[] b = new byte[1024];
int read;
while ((read = xmlIn.read(b)) != -1) {
mem.write(b, 0, read);
}

XMLDocument xmlDoc = new XMLDocument();
xmlDoc.load(mem);

pdf1.bindXML(xmlDoc, null); // Crashes with a nullpointer!
pdf1.save(“test.pdf”);


Would be great if you can help. Thank you very much in advance.
Lennart

Hello, Lennart.

I suppose you must to set stream’s pointer to first stream’s character:
//just after while loop and before create of the xmlDoc:
mem.setSeek(0L);
XMLDocument xmlDoc = new XMLDocument();

Thank you. I have tried this additional line (source code attached), but it doesn’t fix the issue. I still get

java.lang.NullPointerException
Exception in thread “main” java.lang.NullPointerException
at aspose.pdf.e.ap.a(Unknown Source)
at aspose.pdf.e.ap.a(Unknown Source)
at aspose.pdf.Pdf.bindXML(Unknown Source)
at TestPdf.main(TestPdf.java:31)

Again, I cannot find any information or examples on the proper use of MemStream and XMLDocument in conjunction with Excel2Pdf conversions. But I need to get my code example working soon since our client needs proper PDF files we can provide only with Aspose Pdf 2.5.0.

Please, have a look at the source code attached. I need to know whether the problem is rooted somewhere in my code or in the Aspose Pdf library and InputStream’ed XML in general.

Thank you very much in advance
Lennart

Hello, Lennart.


I tested your code. It seems Aspose.Pdf 2.5 allows to use just one way to load the XML data – you should use
pdf.bindXML(pathToTempFile,null);
so, could you create temporary file and load the data to pdf with such call?

The 2.6 release allows to use InputStream as source of the XML data (i’ve tested your code with new release candidate too and it’s works well).