HTML转PDF(C#---itextsharp--zt

一、 需求:将HTML转PDF打印。Web项目中总是有这样的需求,很是让人苦恼。
二、 分析:如何完成这个工作?
1、 需要解析HTML,然后利用itextsharp绘制pdf文档。解析HTML是一个很难完成的任务,各个浏览器对HTML的解析五花八门就知道这不好解决。这条路不好走,但也是可以完成的,通过webbrowser类可以解析HTML获取各对象的位置,然后绘制到pdf中。方法是可行,其中的复杂度不小。
2、 将HTML保存为图片,然后插入到pdf文档中,这里将介绍这种方法。随之而来的问题:如何将HTML保存为图片呢?
三、 解决办法:
1、 将HTML保存为图片:需要用到webbrower类,将指定的HTML载入webbrower,然后调用其DrawToBitmap方法获取图片。代码如下:
private System.Drawing.Bitmap bitmap;
    private string url;
    private int w = 760, h = 900;//A4纸张对应的分辨率大概就是760*900
    public void setBitmap()
    {
        using (WebBrowser wb = new WebBrowser())
        {
            wb.Width = w;
            wb.Height = h;
            wb.ScrollBarsEnabled = false;
            wb.Navigate(url);
            //确保页面被解析完全
            while (wb.ReadyState != WebBrowserReadyState.Complete)
            {
                System.Windows.Forms.Application.DoEvents();
            }
            bitmap = new System.Drawing.Bitmap(w, h);
            wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, w, h));
            wb.Dispose();
        }
    }
2、 如何调用WebBrowser?直接在web页面用的话,会出现错误:“当前线程不在单线程单元中,因此无法实例化 ActiveX 控件”。这需要创建一个单线程单元,在此单元内调用,方法如下while循环确保线程执行完成,也就是确保bitmap已经被初始化了:
            url = Server.MapPath("s.html");
            Thread thread = new Thread(new ThreadStart(setBitmap));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            while (thread.IsAlive)
                Thread.Sleep(100);
            bitmap.Save(Server.MapPath("t.bmp"));
3、 插入图片到PDF:
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bitmap, System.Drawing.Imaging.ImageFormat.Bmp);
            img.ScalePercent(75);//pdf分辨率要大一些,需要缩小以恢复正常PDF是96DPI,IE是72DPI
            doc.Add(img);
四、 完整代码如下:
using System;
using System.Data;
using System.Web;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;
using System.Windows.Forms;
using System.Threading;
public partial class HTML2PDF : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CreatPdf();
    }
    private System.Drawing.Bitmap bitmap;
    private string url;
    private int w = 760, h = 900;
    public void setBitmap()
    {
        using (WebBrowser wb = new WebBrowser())
        {
            wb.Width = w;
            wb.Height = h;
            wb.ScrollBarsEnabled = false;
            wb.Navigate(url);
            //确保页面被解析完全
            while (wb.ReadyState != WebBrowserReadyState.Complete)
            {
                System.Windows.Forms.Application.DoEvents();
            }
            bitmap = new System.Drawing.Bitmap(w, h);
            wb.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, w, h));
            wb.Dispose();
        }
    }
    private void CreatPdf()
    {
        Document doc = new Document(PageSize.A4, 9, 18, 36, 36);//左右上下
        MemoryStream ms = new MemoryStream();
        try
        {
            PdfWriter writer = PdfWriter.GetInstance(doc, ms);
            writer.CloseStream = false;
            doc.Open();
            url = Server.MapPath("s.html");
            Thread thread = new Thread(new ThreadStart(setBitmap));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            while (thread.IsAlive)
                Thread.Sleep(100);
            bitmap.Save(Server.MapPath("t.bmp"));
            
            iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bitmap, System.Drawing.Imaging.ImageFormat.Bmp);
            img.ScalePercent(75);//560 630
            doc.Add(img);
        }
        catch (Exception err)
        {
            throw new Exception(err.Message);
        }
        finally
        {
            doc.Close();
            using (FileStream fs = new FileStream(Server.MapPath("out.pdf"), FileMode.Create))
            {
                ms.Position = 0;
                byte[] bit = new byte[ms.Length];
                ms.Read(bit, 0, (int)ms.Length);
                fs.Write(bit, 0, bit.Length);
            }
            ViewPdf(ms);
        }
    }

    private void ViewPdf(Stream fs)
    {
        Response.Clear();
        //中文名的话
        //Response.AppendHeader("Content-Disposition", "attachment;filename=" +
        //             HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ";charset=GB2312");
        Response.AddHeader("Content-Disposition", "attachment;FileName=out.pdf");
        Response.AddHeader("Content-Length", fs.Length.ToString());
        Response.ContentType = "application/pdf";
        long fileLength = fs.Length;
        int size = 10240;//10K一--分块下载,10K为1块
        byte[] readData = new byte[size];
        if (size > fileLength)
            size = Convert.ToInt32(fileLength);
        long fPos = 0;
        bool isEnd = false;
        while (!isEnd)
        {
            if ((fPos + size) >= fileLength)
            {
                size = Convert.ToInt32(fileLength - fPos);
                isEnd = true;
            }
            readData = new byte[size];
            fs.Position = fPos;
            fs.Read(readData, 0, size);
            Response.BinaryWrite(readData);
            Response.OutputStream.Flush();
            fPos += size;
        }
        fs.Close();
        Response.OutputStream.Close();
        Response.End();//非常重要,没有这句的话,页面的HTML代码将会保存到文件中
        Response.Close();
    }
}

五、至此工作完成,转载注明出处,谢谢。 2009-9-17

六、2009-12-23补充说明:增加pdf打印清晰度,对于图片,要想在pdf上打印的更清晰需要生成大图,然后等比例缩放存放在pdf里面,这样pdf打印出来的图会很清晰,基本是缩放比例越大越清晰,对于图形报表特别有效。对于本文介绍的方法,也可以生成大图缩放到pdf里面。

你可能感兴趣的:(itext)