2

I am able to generate pdf from my HTML string which I am fetching from Dataabse using openhtmltopdf-pdfbox library in my spring boot project. I would like to add image in header and footer section when the pdf is generated for every page.

Step 1: function Create Document from input HTML

public Document createWellFormedHtml(String inputHTML) throws IOException {
        org.jsoup.nodes.Document document = Jsoup.parse(inputHTML, "UTF-8");
        return new W3CDom().fromJsoup(document);
    }

Step 2: function to Generate Document

public void xhtmlToPdf(Document doc, String filePath) throws IOException {
        String str = applicationRepository.findByApplicationKey("CORRESPONDANCE_PATH").getApplicationValue();
        String baseUri = FileSystems.getDefault().getPath(str).toUri().toString();
        OutputStream os = new FileOutputStream(filePath);
        PdfRendererBuilder builder = new PdfRendererBuilder();
        builder.withUri(filePath);
        builder.toStream(os);
        builder.withW3cDocument(doc, baseUri);
        builder.run();
        os.close();
    }

Step 3: Combining step 1 and step 2

    String termConditionContents = inputHTMlString;
    Document docTC = createWellFormedHtml(termConditionContents);
    xhtmlToPdf(docTC, filePath);

eventually I am able to get the pdf. But I want to add the headers and footers on every page of the pdf before the pdf with n number of pages is generated .

May I know how do we do it? I searched and I found some ways to to insert styling inside the inputHTML for header and footer on below URL but it is not completely working.

With Flying Saucer, how do I generate a pdf with a page number and page total on every page at the footer?

mkl
  • 90,588
  • 15
  • 125
  • 265
swapnil skate
  • 83
  • 1
  • 9

0 Answers0