【发布时间】:2018-07-26 10:48:10
【问题描述】:
我正在使用 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 有问题吗?有没有更好的方法来做到这一点?
【问题讨论】: