How to convert HTML to Pdf with OpenPdf
Asked Answered
C

2

6

How can I convert an HTML to PDF with OpenPDF?

For what I know, OpenPdf is a fork of Itext 4. Unluckily I can't find Itext 4 documentation.

Conquest answered 16/3, 2018 at 16:48 Comment(5)
@AmedeeVanGasse: what about this? mvnrepository.com/artifact/com.lowagie/itext/4.2.0Conquest
@AmedeeVanGasse: it was published by Itext Software. See also this: mvnrepository.com/artifact/com.lowagie/itext/4.2.2Conquest
iText 4.2.2 was put there by me personally. It is a redirection POM that redirects to the iText 5 release that was current at the time. It's how iText Software regained control over the hijacked groupId, as described in the article linked above.Hyperform
iText 4.2.0 was published by weiyeh, who was an employee of InProTopia.Hyperform
@AmedeeVanGasse And this means that "Itext 4 does not exists" is not true. And anyway, definitively you're not helping me, furthermore for personal reasons.Conquest
C
19

Ok,it seems you can't do it directly with only OpenPDF, you have to use Flying Saucer: get flying-saucer-pdf-openpdf and then use it. An example:

String inputFile = "my.xhtml";
String outputFile = "generated.pdf";

String url = new File(inputFile).toURI().toURL().toString();

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();

try (OutputStream os = Files.newOutputStream(Paths.get(outputFile))) {
    renderer.createPDF(os);
}

Source.

PS: FlyingSaucer expects XHTML syntax. If you have some problems with yout HTML file, you could use Jsoup:

String inputFile = "my.html";
String outputFile = "generated.pdf";

String html = new String(Files.readAllBytes(Paths.get(inputFile)));
final Document document = Jsoup.parse(html);
document.outputSettings().syntax(Document.OutputSettings.Syntax.xml);

ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(document.html());
renderer.layout();

try (OutputStream os = Files.newOutputStream(Paths.get(outputFile))) {
    renderer.createPDF(os);
}
Conquest answered 17/3, 2018 at 18:47 Comment(0)
C
0

Here's a simple Kotlin HTML to PDF. Jsoup is not required.

fun pdfFromHtml(ostream: OutputStream, html: String) {
    val renderer = ITextRenderer()
    val sharedContext = renderer.sharedContext
    sharedContext.isPrint = true
    sharedContext.isInteractive = false
    renderer.setDocumentFromString(html)
    renderer.layout()
    renderer.createPDF(ostream)
}

Here's one with Jsoup.

fun pdfFromHtml(ostream:OutputStream, html: String) {
    val renderer = ITextRenderer()
    val sharedContext = renderer.sharedContext
    sharedContext.isPrint = true
    sharedContext.isInteractive = false
    val document = Jsoup.parse(html)
    document.outputSettings().syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml)
    renderer.setDocumentFromString(document.html())
    renderer.layout()
    renderer.createPDF(ostream)
}
Chadchadabe answered 10/10, 2022 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.