0

I am using OpenHtmlToPdf for converting some html-files to PDF inside a java-based server-application.

The server-application does not have access to the internet (blocked by a firewall). When trying to convert a html file-with external references (like img src=https://example.com), the conversion-process hangs due to the firewall block.

Is there any possibility to say openhtml2pdf to ignore such external links and to accept an incomplete rendering result?

Hope for help!

Martin

Martin
  • 1

1 Answers1

0

One solution is to use the DOMMutator, which allows to modify unsupported HTML at runtime when the PDF is generated:

PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withFile(Paths.get("file.html").toFile());
FSDOMMutator domMutator = (doc) -> {
    NodeList images = doc.getElementsByTagName("img");
    for(int i = 0; i < images.getLength(); i++) {
        Element img = (Element) images.item(i);
        img.setAttribute("src", "");
    }
};
builder.addDOMMutator(domMutator);
builder.toStream(os);
builder.run();
obourgain
  • 8,856
  • 6
  • 42
  • 57