简体   繁体   English

使用c#和asp.net将HTML内容转换为Pdf

[英]Convert HTML content to Pdf using c# and asp.net

Here iam trying to convert the content inside html div tag to pdf i found the following error: 在这里,我试图将html div标签内的内容转换为pdf,我发现了以下错误:

Input string was not in a correct format is occur

Here is the code i tried using c#: 这是我尝试使用c#的代码:

    divexcel.Visible = true;
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    divexcel.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 80f, 80f, -2f, 35f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    //head.WriteSelectedRows(0, -1, page.Width / 8, page.Height - pdfDoc.TopMargin + head.TotalHeight, writer.DirectContent);
    //pdfDoc.Add(headerTbl);   
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
------------------------------------------

Here is my html code 这是我的HTML代码

<div id="divexcel" runat="server">
    <table id="tbDetails" width="800px">
    <thead >
    <tr>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    </thead>
    <tbody>
    </tbody>
    </table>
    </div>

Try to replace width="800px" on width="800". 尝试在width =“ 800”上替换width =“ 800px”。 Should work 应该管用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM