试图将html导出为pdf [英] Trying to export html to pdf

查看:55
本文介绍了试图将html导出为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有



我想以下列方式将html导出为pdf



Dear All

I am trying to export html to pdf in following way

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.Collections;



public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    // Below code is used to convert your string HTML value to PDF
    protected void btnExportPDF_Click(object sender, EventArgs e)
    {        
        
        //Set page size as A4
        Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);

        try
        {
            PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);            
            
            //Open PDF Document to write data
            pdfDoc.Open(); 
          
            //Assign Html content in a string to write in PDF
            string contents = "<h5>EXPORT HTML CONTENT TO PDF</h5><br /><br />This content is convert from html string to PDF<br /><br /><br /><font color="red">Samples from Ravi!!!</font>";
           
            //Read string contents using stream reader and convert html to parsed conent 
            var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);

            //Get each array values from parsed elements and add to the PDF document
            foreach (var htmlElement in parsedHtmlElements)
            pdfDoc.Add(htmlElement as IElement);

            //Close your PDF
            pdfDoc.Close();

            Response.ContentType = "application/pdf";

            //Set default file Name as current datetime
            Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
            System.Web.HttpContext.Current.Response.Write(pdfDoc);

            Response.Flush();
            Response.End();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }

    // Below code is read content from HTML file and convert to PDF 

    protected void Button1_Click(object sender, EventArgs e)
    {
        //Set page size as A4
        Document pdfDoc = new Document(PageSize.A4, 10, 10, 10, 10);

        try
        {
            PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);

            //Open PDF Document to write data
            pdfDoc.Open();

            //Assign Html content in a string to write in PDF
            string contents = "";

            StreamReader sr;
            try
            {
                //Read file from server path
                sr = File.OpenText(Server.MapPath("sample.html"));
                //store content in the variable
                contents = sr.ReadToEnd();              
                sr.Close();                
            }
            catch (Exception ex)
            {
              
            }


            var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);
           // ArrayList parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);

            //Get each array values from parsed elements and add to the PDF document
            foreach (var htmlElement in parsedHtmlElements)
            pdfDoc.Add(htmlElement as IElement);




            //Close your PDF
            pdfDoc.Close();

            Response.ContentType = "application/pdf";

            //Set default file Name as current datetime
            Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
            System.Web.HttpContext.Current.Response.Write(pdfDoc);

            Response.Flush();
            Response.End();

        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
}







它适用于



以下sample.html






It works fine for

following sample.html

<html>
<head>
    <title></title>
</head>
<body>
    <h5>EXPORT HTML CONTENT TO PDF</h5><br /><br /><br />
    <b>This content is convert from html file to PDF file</b><br /><br /><br />
    <font color="red">Samples from Ravi!!!</font>
</body>
</html>









但在以下行中为以下html文件内容提供错误



var parsedHtmlElements = HTMLWorker。 ParseToList(new StringReader(contents),null);





错误:输入字符串的格式不正确。



html文件中的内容给出错误







but gives error in following line for following html file contents

var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), null);


error: Input string was not in a correct format.

contents in html file giving error

<html>
<table width="300px" border="1" style="border-collapse: collapse; background-color: #E8DCFF">
        <tr>
            <td width="40px">1</td>
            <td>Number</td>
            <td><input class="txt" type="text" name="txt" /></td>
        </tr>
        <tr>
            <td>2</td>
            <td>Number</td>
            <td><input class="txt" type="text" name="txt" /></td>
        </tr>
        <tr>
            <td>3</td>
            <td>Number</td>
            <td><input class="txt" type="text" name="txt" /></td>
        </tr>
        <tr id="summation">
            <td  colspan ="2" align="right">
                Sum :
            </td>
            <td align="center"><span id="sum">0</span></td>
        </tr>
</table>

</html>

推荐答案

您好,您可以看到这个讨论, Html至PDF [ ^ ]
Hi, You can see this discussion,Html to PDF[^]


这篇关于试图将html导出为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆