Created
August 28, 2015 13:28
-
-
Save gabrielmldantas/b1bd05d54b20f9c8ce7a to your computer and use it in GitHub Desktop.
Convert HTML to PDF using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void convertHtmlToPdf(String html, OutputStream out) throws DocumentException { | |
Document doc = Jsoup.parse(html); | |
doc.outputSettings().escapeMode(EscapeMode.xhtml); | |
Element head = doc.head(); | |
Element style = new Element(Tag.valueOf("style"), doc.baseUri()); | |
style.text("img { -fs-fit-images-to-width: 100% }"); | |
head.appendChild(style); | |
DefaultUserAgent userAgent = new DefaultUserAgent(); | |
ITextRenderer renderer = new ITextRenderer(userAgent); | |
renderer.setDocument(DOMBuilder.jsoup2DOM(doc), doc.baseUri()); | |
renderer.layout(); | |
renderer.createPDF(out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i need to download pdf from chart