【问题标题】:Html Written to pdf is not in proper format写入 pdf 的 Html 格式不正确
【发布时间】:2011-06-13 10:07:41
【问题描述】:

我正在使用以下代码使用 iTextSharp.text 将 html 写入 pdf。

但我在 pdf 文件中的 html 格式与它们在 html 中显示的不同。设计不正确。

我应该怎么做才能在 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();

【问题讨论】:

  • 您的 HTML 不是有效的全格式 HTML - 有效的 HTML 必须以 HTML 标记开头。尝试使用简单、有效的 HTML 作为初学者。
  • 你找到解决方法了吗?

标签: asp.net html itextsharp


【解决方案1】:

HTMLWorker 已弃用。并且有充分的理由。它不支持全套 HTML 和 CSS 结构,并且没有积极努力更新其功能。

我建议你试试 pdfHTML,这是最新的将 HTML 转换为 PDF 的 iText 解决方案,它确实支持 HTML5 和 CSS3。

一个小代码示例:

// 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);

查看 iText 网站了解更多详情:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-07
    • 2019-03-18
    • 2016-12-30
    相关资源
    最近更新 更多