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.

5
  • @AmedeeVanGasse: what about this? mvnrepository.com/artifact/com.lowagie/itext/4.2.0 Commented Mar 17, 2018 at 18:19
  • @AmedeeVanGasse: it was published by Itext Software. See also this: mvnrepository.com/artifact/com.lowagie/itext/4.2.2 Commented Mar 17, 2018 at 18:31
  • 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. Commented Mar 17, 2018 at 18:33
  • iText 4.2.0 was published by weiyeh, who was an employee of InProTopia. Commented Mar 17, 2018 at 18:35
  • @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. Commented Mar 17, 2018 at 18:40

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.