Charan
Charan

Reputation: 1

Creating a dynamic PDF in Java

This is not a duplicate question. I had searched and tried many options before posting this question.

We have a web page, in which user should be able to input data in text boxes, text areas, images and also Rich Text editors. This data has to be filled in an existing report, like filling the blanks.

I was able to achieve the functionality using Apache FOP when the user input is simple text. But Apache FOP doesn't work if the user input is Rich Text(html format). FOP will not render html, and it just pushes the html code(ex: <strong> XYZ /strong>) into the pdf. I tried using iText, but the setback here is that even though iText supports rendering of html to pdf, it is not able to place the images, that are included in <img> tags, in the pdf file.

I can try to create a pdf using iText api block by block, but the problem is rich text data entered by the user can not be embedded between the code since building pdf block by block and html to pdf can not be done together in iText. Or at least that is what I think from my experience.

Is there any other way to create a pdf file from java with images, rich text rendering as it is, headers and footers?

Upvotes: 0

Views: 3081

Answers (3)

Charan
Charan

Reputation: 1

I am not trying to market or promote this product. This api actually solved our problem so thought of mentioning it as it might help fellow developers. please let me know if this is against your policy.

I finally realized that the solution for my requirement can not be achieved with either FOP, iText, Aspose, Flying Saucer, JODConverter.

I found a paid api Sferyx. This api allows to render a very complex html to pdf almost preserving the original style. It also renders the images included in the html. We are still exploring this api and will post what other features this api provides.

Upvotes: 0

Nausherwan Aslam
Nausherwan Aslam

Reputation: 458

I am working as Social Media Developer for Aspose and to add rich text to a form field in PDF file, you can try our Aspose.Pdf for Java API. Check the following sample code:

// Open a PDF document                                                                         
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("c:\\data\\input.pdf");      

//Find Rich TextBox field using Field Name                                                     
RichTextBoxField textBoxField1 = (RichTextBoxField)pdfDocument.getForm().get("textbox1");      

//Set the field value                                                                          
textBoxField1.setValue("<strong> XYZ </strong>");                                              

// Save the modified PDF                                                                       
pdfDocument.save("c:\\data\\output2.pdf");   

Upvotes: 0

Kakarot
Kakarot

Reputation: 4252

iText provides the capability to convert HTML Data to Pdf. Below is the snippet to do it :

 Lets assume the html data is available as Input Stream (If its a String then we can convert it to InputStream using Apache Commons - IOUtils)
 InputStream htmlData; // Html Data that needs to converted to Pdf

 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 Document document = new Document();
 PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
 document.open();

 // convert the HTML with the built-in convenience method
 XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, htmlData);

 document.close();
// outputStream now has the required pdf data

Upvotes: 1

Related Questions