【发布时间】:2019-07-21 00:25:34
【问题描述】:
以下是我尝试从中生成 pdf 的代码。 附加 Html 字符串并转换为 Pdf, Pdf 正在生成,但样式未应用。
protected void Demo()
{
StringBuilder sb = new StringBuilder();
sb.Append("<header class='clearfix'>");
sb.Append("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\">");
sb.Append("<tbody>");
sb.Append("<tr>");
sb.Append("<td style=\"height:47px; width:175px;\">");
sb.Append("<p>");
sb.Append(" <style=\"width:205px; height:35px;\"></p>");
sb.Append("</td>");
sb.Append("<td colspan=\"4\" style=\"height:47px; width:553px; background:#0d4580; color:aliceblue;\">");
sb.Append("<p style='margin-left:76.45pt'><strong>JOB DESCRIPTION</strong ></p>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("<td colspan='5' style='height:19px; width:729px;'>");
sb.Append("<p> </p>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("<td colspan='2' style=\"height:38px; width:182px; background: #0d4580;color: aliceblue;\" >");
sb.Append("<p style=\"margin-left:5.35pt\" ><strong> Job Title / Role:</ strong ></p>");
sb.Append("</td>");
sb.Append("<td colspan='3' style='height:38px; width:547px;' >");
sb.Append("<p style='margin-left:5.3pt' > Quality Assurance Executive</ p >");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("<td colspan='2' style='height:38px; width:182px; background: #0d4580;color: aliceblue;' >");
sb.Append("<p style='margin-left:5.35pt' ><strong> Department:</ strong ></p>");
sb.Append("</td>");
sb.Append("<td style='height:38px; width:215px' >");
sb.Append("<p style='margin-left:5.3pt' > Training & amp; Quality </ p >");
sb.Append("</td>");
sb.Append("<td style='height:38px; width:120px; background: #0d4580;color: aliceblue;' >");
sb.Append("<p style='margin-left:5.3pt' >< strong > Shift Timings:</strong ></p>");
sb.Append("</td>");
sb.Append("<td style='height:38px; width:211px' >");
sb.Append("<p style='margin-left:5.3pt' > Flexible(7AM & ndash; 10PM)</p>");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("<td colspan = '2' style = 'height:39px; width:182px; background: #0d4580;color: aliceblue;' >");
sb.Append("<p style = 'margin-left:5.35pt'><strong> Reporting To:</strong></p>");
sb.Append("</ td >");
sb.Append("<td style = 'height:39px; width:215px' >");
sb.Append("<p style ='margin-left:5.3pt' > AM & ndash; Quality </p>");
sb.Append("</td >");
sb.Append("<td style='height:39px; width: 128px; background:#0d4580;color:aliceblue;' >");
sb.Append("<p style='margin-left:5.3pt'><strong> No.of position:</strong></p>");
sb.Append("</td >");
sb.Append("<td style='height:39px; width:211px' >");
sb.Append("<p style='margin-left:5.3pt' > 01(One) </p>");
sb.Append("</td >");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("<td colspan='2' style='height:38px; width:182px; background: #0d4580;color: aliceblue;' >");
sb.Append("<p style='margin-left:5.35pt' >< strong > Work Location:</ strong ></p >");
sb.Append("</td>");
sb.Append("<td style='height:38px; width:215px' >");
sb.Append(" <p style='margin-left:5.3pt' > Bhayander, Mumbai </p>");
sb.Append("</td>");
sb.Append("<td style ='height:38px; width:120px; background: #0d4580;color: aliceblue;' >");
sb.Append("<p style ='margin-left:5.3pt' ><strong> Level / Grade:</strong></p>");
sb.Append("</td>");
sb.Append("<td style='height:38px; width:211px' >");
sb.Append("<p style='margin-left:5.3pt' > 5 </ p >");
sb.Append("</td>");
sb.Append("</tr>");
sb.Append("<tr>");
sb.Append("</tr>");
sb.Append("</tbody>");
sb.Append("</table>");
//string strOutput = sb.ToString();
//StringReader reader = new StringReader(strOutput);
StringReader sr = new StringReader(sb.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
//HtmlConverter.ConvertToPdf(sb.ToString(), memoryStream);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
HtmlConverter.ConvertToPdf(sb.ToString(), memoryStream);
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=Invoice.pdf");
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}
使用字符串生成器将 Htmlstring 转换为 PDF 时不应用样式, 它显示没有样式,语法是否适合应用样式? 还是我需要使用不同的技术来使用样式工具生成 pdf?
【问题讨论】:
-
您的标签说您使用 iText 7,但 iText 7 中不存在
HTMLWorker。HTMLWorker已弃用,您必须改用 pdfHTML。 -
如果您的意思是
HTMLWorker- 该类仅支持非常有限的部分 html 选项。如果你一定要使用它,你必须大大简化你的 html。
标签: c# css asp.net itext pdfhtml