Skip to content

Instantly share code, notes, and snippets.

@gabrielmldantas
Created August 28, 2015 13:28
Show Gist options
  • Save gabrielmldantas/b1bd05d54b20f9c8ce7a to your computer and use it in GitHub Desktop.
Save gabrielmldantas/b1bd05d54b20f9c8ce7a to your computer and use it in GitHub Desktop.
Convert HTML to PDF using Java
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);
}
@SanjayJaiswa
Copy link

i need to download pdf from chart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment