Skip to content Skip to sidebar Skip to footer

Itext Style Parsing Html To Pdf

I've a problem with iText. I've followed this link: How to export html page to pdf format? My snippet: String str = '

Solution 1:

iText isn't the best Html Parser, but you can use Flying-Saucer for this. Flying-Saucer is build on top of iText but has a capable Xml / (X)Html parser. Short: Flying Saucer is perfect if you want html -> Pdf.

Here's how to generate the pdf from your string:

/*
 * Note: i filled something in the title-tag and fixed the head tag (the whole body-tag was in the head)
 */
String str = "<html><head></head><body><divstyle=\"width:100%;height:100%;\"><h3style=\"margin-left:5px;margin-top:40px\">First</h3><divstyle=\"margin-left:15px;margin-top:15px\"><title>t</title><p>sdasdasd shshshshdffgdfgd</p></div><h3style=\"margin-left:5px;margin-top:40px\">The dream</h3><divstyle=\"margin-left:15px;margin-top:15px\"></div></div></body></html>";

OutputStream os = new FileOutputStream(new File("example.pdf"));

ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(str);
renderer.layout();
renderer.createPDF(os);

os.close();

But: FS supports only valid Html / Xhtml / xml, so make shure it is.

Post a Comment for "Itext Style Parsing Html To Pdf"