Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html Written to pdf is not in proper format

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 pdf?

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();
like image 758
Dalvir Saini Avatar asked Dec 20 '25 22:12

Dalvir Saini


1 Answers

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
like image 114
Jake Frederix Avatar answered Dec 23 '25 23:12

Jake Frederix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!