使用ASP.NET将HTML内容转换为PDF

时间:2018-07-26 10:48:10

标签: c# html asp.net itext

我正在使用 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);
            }
        }
    }
}

预期产量

enter image description here

但是我得到的输出低于

enter image description here

GridView没有以正确的格式显示。如何将HTML解析为PDF? 我要解析的html有什么问题吗?有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

您将需要导入以下名称空间。

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();  
}