c# - 使用 ASP.NET 将 HTML 内容转换为 PDF
问题描述
我正在使用ItextSharp模块将下面列出的 HTML 转换为用于电子邮件的 PDF 页面 这是 Html 的代码
<div>
<div id='div2' class="row ">
<div class="col-sm-3 box-heading" style="padding-left: 0px !important;color: #333333; background-color: #ffffff !important; margin-bottom: 5px;">
<h4 style="font-size:20px;">
<input type="checkbox" id='Checkbox2' checked="checked" />
Chief Complaints
</h4>
</div>
<div class="col-sm-9" style="padding-top: 15px !important;padding:0px">
<div class="col-sm-12" style="">
<div>
<label id="Label2" style="width: auto; font-weight: 500;">jyj, 23233, 5556565, 5555, 555, 55, iuye, jyrgtr, test2, Test, Backache, RTA, Follow-up case, Difficulty in walking, Paraplegia, Paraparesis, Quadriparesis, Left lower limb pain, </label>
</div>
</div>
</div>
<div style="clear: both;">
</div>
</div>
<div class="row">
<div class="col-sm-3 box-heading" style="padding-left: 0px !important;color: #333333; background-color: #ffffff !important; margin-bottom: 5px;">
<h4 style="font-size:20px;">
<input id="Checkbox4" type="checkbox" checked="checked" />
Diagnosis
</h4>
</div>
<div class="col-sm-8" style="padding-top: 15px !important; padding:0px; padding-left: 12px !important;" >
<span style="font-weight: bold; font-weight: 500; ">
cbc</span>,
<span>
</span>
<span style="font-weight: bold; font-weight: 500; ">
TSH</span>,
<span>
</span>
<span style="font-weight: bold; font-weight: 500; ">
hrg</span>,
<span>
</span>
<span style="font-weight: bold; font-weight: 500; ">
rg</span>,
<span>
</span>
<span style="font-weight: bold; font-weight: 500; ">
hd</span>,
<span>
</span>
<span style="font-weight: bold; font-weight: 500; ">
RFT</span>,
<span>
</span>
</div></div>
</div>
这是c#的代码
protected void btnemail_Click(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
string DCode = Convert.ToString(Session["DCode"]);
string QueryPatient = "";
DataTable dtPatient = obj.GetDataTable(QueryPatient);
if (dtPatient.Rows.Count > 0)
{
//code for bind personal Details
string companyName = "Prescription";
int orderNo = 2303;
StringBuilder sb = new StringBuilder();
//code for bind repeater
rptPrescription.RenderControl(hw);
rptInvestigation.RenderControl(hw);
rptInvestigationreport.RenderControl(hw);
rptMedicalOrder.RenderControl(hw);
sb.Append(hw.InnerWriter);
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())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
string emailto = TxtEmail.Text;
MailMessage mm = new MailMessage("****@gmail.com", emailto);
mm.Subject = "Prescription Report";
//sb.Append(sw);
//mm.Body = sb.ToString();
mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "Prescription.pdf"));
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "*****@gmail.com";
NetworkCred.Password = "*****";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
}
}
}
预期产出
但是我得到的低于输出
GridView 未以正确格式显示。如何将 Html 解析为 PDF?我要解析的 html 有问题吗?有没有更好的方法来做到这一点?
解决方案
您将需要导入以下命名空间。
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
在单击导出按钮时执行以下事件处理程序。
protected void btnExport_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
employeelistDiv.RenderControl(htmlTextWriter);
StringReader stringReader = new StringReader(stringWriter.ToString());
Document Doc = new Document(PageSize.A4, 10 f, 10 f, 100 f, 0 f);
HTMLWorker htmlparser = new HTMLWorker(Doc);
PdfWriter.GetInstance(Doc, Response.OutputStream);
Doc.Open();
htmlparser.Parse(stringReader);
Doc.Close();
Response.Write(Doc);
Response.End();
}
推荐阅读
- matlab - 在Matlab中填充3D点之间的凸区域
- json - 用nodered提取json
- asp.net - Sonarscanner MSBuild 工具未在管道中运行 - Jenkins
- java - spring rest API 的所有查询的自定义验证
- android - Jetpack Compose 中列和行的 Expanded 和 LayoutSize.Expand 的区别
- android - Room 无法验证数据完整性,但我启用了破坏性迁移
- c++ - 指针数组的 C/C++ 内联强制 (void**)
- c - c语言中的malloc函数?
- ansible - Twilio 出站呼叫已断开,没有我的消息
- matplotlib - 更改 matplotlib 中幅度刻度标签文本的位置