Download PDF generated from HTML content

Hello,

The requirement is to download a PDF document on client side (from browser). The content of PDF file is generated from selected HTML components on the server side.

Currently, I am trying the code as below, but it gives exception stating com.aspose.pdf.exceptions.InvalidPdfFileFormatException: Startxref not found.
I have attached the HTML file for your reference.

Please help to resolve this issue and also, how to specify download path for the client?

Thanks for your help and earliest attention.



String overViewTabTextStr = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(“desk-icons-form:overViewTabDownload”);
InputStream stream = new ByteArrayInputStream(overViewTabTextStr.getBytes(StandardCharsets.UTF_8));
// Instantiate Document object
Document doc = new Document(stream);
doc.save(“output.pdf”);


Hi Hiren,


Thanks for contacting support.

The problem is appearing because the blank constructor of Document() class expects PDF file as an argument and as you are trying to load HTML contents, its generating exception. So in order to load HTML files, you need to pass com.aspose.pdf.HtmlLoadOptions object as second argument to Document constructor. Please take a look over following code snippet which demonstrates the process of load HTML file contents from ByteArrayInputStream and save the output in PDF format.

For your reference, I have also attached the resultant PDF generated over my end.

[Java]

//source PDF file <o:p></o:p>

java.io.File file = new java.io.File("C:\\pdftest\\aspose_html_content\\aspose_html_content.html");

java.io.FileInputStream fis = new java.io.FileInputStream(file);

//System.out.println(file.exists() + "!!");

//InputStream in = resource.openStream();

java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();

byte[] buf = new byte[1024];

try {

for (int readNum; (readNum = fis.read(buf)) != -1;) {

bos.write(buf, 0, readNum); //no doubt here is 0

//Writes len bytes from the specified byte array starting at offset off to this byte array output stream.

System.out.println("read " + readNum + " bytes,");

}

} catch (java.io.IOException ex) {

}

byte[] bytes = bos.toByteArray();

//instantiate Document Object with ByteArrayInputStream while passing byte array as argument

com.aspose.pdf.Document doc = new com.aspose.pdf.Document(new java.io.ByteArrayInputStream(bytes), new com.aspose.pdf.HtmlLoadOptions());

//get the page count of PDF file

System.out.println(doc.getPages().size());

doc.save(“C:\pdftest\aspose_html_content.pdf”);

Thanks for providing the code snippet. It worked and PDF file was generated.


But now, how do I apply the style and format to the generated content? Please point me in the right direction.

Also, the generated file needs to be downloaded on the client machine. Client is a web application user and the code that generates PDF file runs on the server.

Thanks for your help.

Hiren

Hi Hiren,

Please share some details regarding your requirement on setting styles and formatting of PDF document. Meanwhile you may consider exploring API document for further details on Working with Document Object

Now as per your requirement on rendering PDF file on client side, you need to save the output in Stream object and then pass the stream to client browser.

Hello,

Please refer to the attached documents that we want to generate using Aspose.PDF for Java tool.

Please share some examples and guidelines on how to achieve these requirements.

Thanks.

Hi Hiren,

Thanks for sharing the resource files.

As per my understanding, in order to generate required document, you need to place Image and text in Header and Footer section, create table object with Text and a graphic object to show current assets. You can also define the formatting (Font Face, Font size, Color etc) of text when placing it inside table object. Please visit the following links for required information on

In case of any further query, please feel free to contact.

PS, in order to define text formatting, you may consider adding HTML text inside PDF file where text will be rendered in native HTML tag instead it appears as static HTML tags. For more information, please visit Add HTML String using DOM