您的位置:首页 > Web前端 > HTML

HTML导出PDF中文文件

2014-08-20 18:39 225 查看
今天在做一个账单导出,需要导出PDF文件。于是开始满天下找解决方案。其实说实话,PDF确实了解比较少,看到园子里面很多人都说用

iTextSharp。

于是乎专门对iTextSharp是什么进行了搜索,原来这个国外的一群人开发的PDF的类库。于是根据下载了iTextSharp.DLL并引用了它,结果发现结果太不理想了。没有样式没有中文,这让人。。。。甚至,跨行的RowSpan也给我弄掉了。这完全不是我需要的东西嘛。想放弃了,换个东西来做。然后随手点了iTextSharp的开源项目。结果发现,其实它是能支持中文和样式的。只是需要引用他的另一个类库xmlworker。

于是下载了 itextsharp-all-5.5.2.zip 和 itextsharp.xmlworker-all-5.5.2.zip然后就完美实现了。

相关代码如下:

这个类文件为了实现中文转码的问题

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

/// <summary>
/// ChineseFontFactory
/// </summary>
public class ChineseFontFactory : FontFactoryImp
{

    private static readonly string SoneFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts),
    "SIMSUN.TTC,1");//宋体  这里是宋体在计算机的中文包所在位置
    public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color,
        bool cached)
    {
        //可用Arial或标楷体,自己选一个
        BaseFont baseFont = BaseFont.CreateFont(SoneFile, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        return new Font(baseFont, size, style, color);
    }

}


然后封装转换方法

/// <summary>
    /// 将Html文字 输出到PDF档里
    /// </summary>
    /// <param name="htmlText"></param>
    /// <returns></returns>
    public byte[] ConvertHtmlTextToPDF(string htmlText)
    {
        if (string.IsNullOrEmpty(htmlText))
        {
            return null;
        }
        MemoryStream outputStream = new MemoryStream();//
        byte[] data = Encoding.UTF8.GetBytes(htmlText);//
        MemoryStream msInput = new MemoryStream(data);
        Document doc = new Document();//
        PdfWriter writer = PdfWriter.GetInstance(doc, outputStream);
        PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f);
        doc.Open();
        XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8,new ChineseFontFactory());
        PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);
        writer.SetOpenAction(action);
        doc.Close();
        msInput.Close();
        outputStream.Close();
        return outputStream.ToArray();

    }

最后是方法触发,例如当点击按钮就会导出PDF文件

protected void btnPdf_Click(object sender, EventArgs e)
    {
        
       
        string fileName = DateTime.Now.ToString();
        System.Text.StringBuilder strb = new System.Text.StringBuilder();
        System.IO.StringWriter sw = new System.IO.StringWriter(strb);
        System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw);
        pointContent.RenderControl(htw);
        string htmlText = strb.ToString(); 
        byte[] pdfFile = this.ConvertHtmlTextToPDF(htmlText);
        Response.ContentType = "application/pdf";
        Response.ContentEncoding = System.Text.Encoding.UTF8;
        Response.AddHeader("content-disposition", "attachment;filename=" + fileName + ".pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.OutputStream.Write(pdfFile, 0, pdfFile.Length);
        Response.End();
    }


打完收工,不过在使用的过程中就是反应有点慢。这点让人有点无语。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: