2

We have header and footer String as HTML content type but how to append both on every page. In below java method we are passing three parameter htmlcontent, HeaderContent, FooterContent and return number of pages created in html, But where do we have to attach the header and footer content?

public static int generatePDF(String strFileName, String htmlContent,String headerHtml,String footerHtml) throws PDFNetException {
  PDFDoc doc = new PDFDoc();
  HTML2PDF converter = new HTML2PDF();
  int nPages = 0;
  try {
      converter = new HTML2PDF();
      doc = new PDFDoc();
      converter.insertFromHtmlString(htmlContent);
      try {
            if (converter.convert(doc)) {
                  doc.save(strFileName, SDFDoc.e_linearized, null);
                  nPages = doc.getPageCount();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } catch (Exception e) {
            ex.printStackTrace();
        } finally {
            converter.destroy();
            doc.close();
    }
    return nPages;
}
Dipak Prajapati
  • 404
  • 3
  • 11

1 Answers1

0

One option is to post-process the PDF, by using the Stamper class, to add headers/footers.

See the following sample code on how to use Stamper call https://www.pdftron.com/documentation/samples/#stamper

The HTML2PDF converter appends pages to the PDFDoc object passed in, so you can do the following.

call HTML2PDF.InsertFromURL(url)
call HTML2PDF.Convert(pdfdoc) 
run Stamper on pages x-y stamp

and repeat to keep appending pages to pdfdoc.

Ryan
  • 2,200
  • 1
  • 8
  • 11
  • Stample takes PDF as input. Is it possible to utilize HTML's / to add repeated header/footer same as chrome is doing while print preview. – Kanti Oct 04 '18 at 12:57
  • Currently no, it would be post processing action on the PDF. Is that an issue for you? If so, could you elaborate. – Ryan Oct 04 '18 at 17:19
  • Yes I have issue, as I'm appending multiple HTMLs in one PDF. So if it do at a time of converting from HTML to PDF and than PDF would have different page header/footer as per thead/tfoot in each given HTML. – Kanti Oct 05 '18 at 11:36
  • You could merge the different PDF files at the end, so the pages from each HTML source retain their own headers/footers. I'll add sample code for merging to my answer. – Ryan Oct 05 '18 at 23:26
  • Thanks for reply @Ryan, but I want to make generic utility in which it will add page header/footer from directly HTML tfoot/thead tag without proving it externally and make it consistent with chrome's print-to-pdf feature.In above example I have to create stamp for each HTML that I want to convert. – Kanti Oct 10 '18 at 05:27