Adding Header or Footer on every page using ITextRenderer from HTML - java

I'm creating an HTML report usgin freemarker, and i produce a PDF from that HTML using ITextRenderer.
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
renderer.createPDF(baosPDF);
I have a table in that html, with a header that successfully shows on every page using css classes:
thead { display:table-header-group }
Is it possible to do the same trick for an arbitrary section of my document? (let say, a div) I'ld like to keep my html vanilla, and identify the "header" and "footer" i want to see on every page using css.
Is it possible, only with css?

Perhaps you should have a look at
http://developers.itextpdf.com/content/itext-7-examples/converting-html-pdf
It gives a few examples of converting html to pdf. Including loading an external stylesheet.

Related

Add page header and footer to pdf using itext xmlworker

i want to add a page header and page footer to my generated pdf. I use xmlworker for convert my html content to pdf.
I want to add a page header and page footer in every page of generated pdf, i have problems to do this task. How can i do this?
You have to implement an HeaderFooter class for your document which implements itexts PdfPageEventHelper class.
Just take a look at this example.
You want to create a header on each page, so you will just need to override the onEndPage() method.
Adding a footer can be done with this methode aswell. You can do so by adding content to the directContentByte using coordinates for the pages bottom like this:
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase("test"),
rect.getRight()-35, rect.getBottom()-25, 0);

HTML -> PDF rendering issues

I'm generating pdfs from HTML pages with an application. Sometimes, the pdf is formatted correctly (with styles); other times, it lacks style elements.
In the log file I can see the "Error in rendering".
We are using HTML tags and using string buffer we are converting html tag to pdf file. Not sure why we are getting this missing format issues while generating the pdf file.
So sometimes the CSS file (style) does convert with the HTML file, and sometimes the CSS doesn't convert with the HTML file.
I'm guessing that you use an external CSS file. If I were you, I would try to type your CSS code inside your HTML file, under the header element, like this:
<style>
body {background-color:#fff}
h1 {color:#eee}
</style>

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?
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
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");
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.

using JSF PrimeFaces' text editor, how to add text in PDF using iText

We are using JSF PrimeFaces' text editor. When we receive String from text editor in backing bean, it also includes HTML tags. Following image might help in understanding this problem.
Following is what we wrote:
Following is what we received:
Next thing we want to do is, to write what was written in text editor, as it is, into PDF using iText. But we do not know how to convert this string (with HTML tags) into only data.
Following was the code:
You can go for XMLWorker in iText. Below code will give you the content in Orange color
document.open();
String finall= "<style>h1{color:orange;} </style><body><h1>This is a Demo</h1></body>";
InputStream is = new ByteArrayInputStream(finall.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter,document, is);
document.close();
Whatever HTML content we are giving it will create it as PDF. The only thing to take care is it will work for XHTML means all the opening tags should have a end tag.
For example in HTML for break we will use <br> but here it should be <br/>Hope this will help you.
Use JSoup to achieve this. Jsoup
Then in your code
Jsoup.parse(textRecievedFromEditor).text();
This will return text without HTML Tags.
e.g.
For example, given HTML {#code <p>Hello <b>there</b> now!</p>},
{#code p.text()} returns {#code "Hello there now!"}

Convert entire JSP to PDF [duplicate]

I have a webpage with a export option to PDF. I have to display the contents of the page in the PDF. Currently I use iText PDF Library to generate PDFs. The problem is creating PDF with iText is quite a challenge. Moreover we get frequent layout/UI changes for the webpage, so we have make the same changes to PDF.
Is there any way i can convert my JSP output to PDF. Like for example "if we set the content type to contentType="application/vnd.ms-excel", a JSP table can be rendered as Excel document.
Have you checked Jasper Reports ? It has the concept of XML templates. Also same template can be used to generate Word / XLS / PDF/ CSV / XML output.
You don't need to change the iText code generation if you use it in combination with Flying Saucer (a.k.a. XhtmlRenderer). It's then basically as simple as:
String inputPath = new File("/file.xhtml").toURI().toURL().toString();
OutputStream outputStream = new FileOutputStream("/file.pdf");
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(inputPath);
renderer.layout();
renderer.createPDF(outputStream);
outputStream.close();
You can find a blog with more code samples here.
You should check wkhtmltopdf.

Categories

Resources