0

I am having an already existing implementation of drawing PDF-s in SpringBoot with Apache PdfBox library.

However, now there is a requirement to add text with HTML tags to an existing document.

From what I have researched, PDFBox does not recognize HTML tags, so I reached for openhtmltopdf library. But, I am having issues doing so, don't know how to pass the current location of the document I want the text to be appended to. Also, I need to extract the location to where the next text should be appended to.

Is it possible to do it with this library, or you suggest some other option?

PdfBoxRenderer renderer = new PdfRendererBuilder()
            .withProducer(htmlText)
            .usePDDocument(document)
            .buildPdfRenderer();
    renderer.layout();
    renderer.createPDFWithoutClosing();
    renderer.close();

1 Answers1

0
private static void htmlToPdf(String inputHTML, String outputPdf) throws IOException {

       
       // ByteArrayOutputStream actual = new ByteArrayOutputStream();
        File Outputfile = new File(outputPdf);
        try {
            PdfRendererBuilder builder = new PdfRendererBuilder();
            builder.defaultTextDirection(BaseRendererBuilder.TextDirection.LTR);
            PDDocument document = PDDocument.load(Outputfile);
             float POINTS_PER_INCH = 72;
            float POINTS_PER_MM = 1 / (10 * 2.54f) * POINTS_PER_INCH;
            PDPage page1 = new PDPage(PDRectangle.A4);
            page1.setMediaBox(new PDRectangle(297 * POINTS_PER_MM, 210 * POINTS_PER_MM));
            
            //document.addPage(page1);
            builder.usePDDocument(document);
            builder.withHtmlContent(inputHTML, "")
                .usePageSupplier((doc, pageWidth, pageHeight, pageNumber, shadowPageNumber) -> {
                PDPage page;
                if (pageNumber == 0) {
                    //First appending page is last original page.
                    page = doc.getPage(doc.getNumberOfPages() -1);
                    System.out.println(0);
                } else {
                    doc.addPage(page = page1);
                    System.out.println(1);
                }
                return page;
            });

            //builder.toStream(actual);
            builder.addDOMMutator(doc -> {
                Element style = doc.createElement("style");
                Node node = doc.createTextNode(
                        "@page:first { margin-top: 400px; size: landscape; } " 
                            + "@page { margin-: 0mm; size: landscape; }" + ".MainTable > *{ margin-left: 150px; }"+" .MainTable {page-break-inside: avoid;}");
                style.appendChild(node);
                doc.getElementsByTagName("head").item(0).appendChild(style);
            });
            builder.useFastMode();
            builder.testMode(true);
            //builder.run();
            PdfBoxRenderer renderer = builder.buildPdfRenderer();
            renderer.createPDFWithoutClosing();
           
            document.save(outputPdf);
           // actual.close();
            System.out.println("Finished");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103