I am using the following code to write html to pdf using iTextSharp.text.
But my html format in pdf file is not as they are showing in the html. Design is not coming properly.
What should I do to show the proper design in pd开发者_如何学运维f?
Document document = new Document(PageSize.A4_LANDSCAPE, 0, 0, 30, 65);
PdfWriter.GetInstance(document, new FileStream(Server.MapPath("/") + "Temp/" + "parsetest1.pdf", FileMode.Create));
document.Open();
String htmlText = "<div style='margin: 20px auto; width: 300px; font-family: Arial, Helvetica, sans-serif;'><div style='background: rgb(216, 216, 216); padding: 7px;'><div style='background: rgb(255, 255, 255); padding: 10px; width: 57px; float: left;'><img alt='barcode' src='images/abc.png'></div><div style='width: 7px; height: 7px; float: left;'></div> <div style='width: 166px; float: right;'><div style='background: rgb(255, 255, 255); padding: 10px;' align='center'><img alt='img' src='images/photpath.png'><div style='clear: both;'></div></div><div style='background: rgb(255, 255, 255); padding: 10px; margin-top: 7px;' align='center'><div style='color: rgb(157, 157, 157); font-size: 12px; float: left;'>Name</div><div style='width: 146px; text-align: center; color: rgb(0, 0, 0); clear: both; font-size: 14px; margin-top: 10px; float: left;' align='center'><strong>dalvirsaini</strong></div><div style='clear: both;'></div></div><div style='background: rgb(255, 255, 255); padding: 10px; margin-top: 7px;' align='center'><div style='color: rgb(157, 157, 157); font-size: 12px; float: left;'>Description</div><div style='width: 146px; text-align: center; color: rgb(0, 0, 0); clear: both; font-size: 14px; margin-top: 10px; float: left;' align='center'><strong>Payment Status</strong></div><div style='clear: both;'></div></div><div style='background: rgb(255, 255, 255); padding: 10px; height: 89px; margin-top: 7px;' align='center'><img alt='ScanCode' src='images/abc2.png'><div style='clear: both;'></div></div><div style='clear: both;'></div></div><div style='clear: both;'></div></div><div style='clear: both;'></div></div>";
StringReader abc = new StringReader(htmlText);
List<iTextSharp.text.IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(abc, null);
foreach (object item in elements)
{
document.Add((IElement)item);
}
document.Close();
HTMLWorker is deprecated. And with good reason. It does not support the full set of HTML and CSS constructs, and no active effort is being made to update its features.
I suggest you try pdfHTML, the latest iText solution for converting HTML to PDF, which does support HTML5 and CSS3.
A small code sample:
// IO
String html = "your_html_snippet";
File pdfDest = new File(System.getProperty("user.home"), "output.pdf");
// pdfHTML specific code
ConverterProperties converterProperties = new ConverterProperties();
HtmlConverter.convertToPdf(html, new FileOutputStream(pdfDest), converterProperties);
Check out the iText website for more details:
- http://developers.itextpdf.com/content/itext-7-examples/itext-7-converting-html-pdf
精彩评论