zoukankan      html  css  js  c++  java
  • .net将html转换PDF

    需要使用iTextSharp库文件

    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Web;
    using System.Web.Mvc;
    using iTextSharp.text;
    using iTextSharp.text.html.simpleparser;
    using iTextSharp.text.pdf;
     
    string path = Server.MapPath("~/Views/ExportExcel/ExcelHtml.html");
    WebClient wc = new WebClient();
    string htmlText = wc.DownloadString(path);
    
    
    // string imgpath = Server.MapPath("~/Content/logo.png");
    //string htmlText = @"<html>
    //<body>
    //<div>
    //添加圖片
    //<img src='" + imgpath + @"' />
    //<table style='100%' class='tdCollapse'>
    //<tr>
    //<td class='td1'>safs</td>
    //<td class='td2' class='tdBorder'>DSA</td>
    //<td class='td3'>dfsaf</td>
    //<td class='td4' class='tdBorder'>sdfsfsf</td>
    //<td class='td5'>dfsaf</td>
    //<td class='td5'>dfsafsfsf</td>
    //<td class='td5'>gfdgdg</td>
    //</tr></table></div></body></html>";
    a.方法一:
    MemoryStream m = new MemoryStream();
    Document document = new Document();
    HTMLWorker worker = new HTMLWorker(document);
    try
    {
    //解决 导出的PDF 中文不显示  将C:/Windows/Fonts/simhei.ttf文件考到本地Fonts文件夹中
    string Fontpath = Server.MapPath("~/Fonts/");
    string fontFilePath = Path.Combine(Fontpath, "simhei.ttf");//C:/Windows/Fonts/simhei.ttf
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(System.DateTime.Now + ".pdf", Encoding.UTF8));
    
    PdfWriter.GetInstance(document, m);
    document.Open();
    StyleSheet styles = new StyleSheet();
    FontFactory.Register(fontFilePath);
    styles.LoadTagStyle("td", "face", "SIMHEI");
    styles.LoadTagStyle("td", "encoding", "Identity-H");
    styles.LoadTagStyle("td", "leading", "18,0");
    styles.LoadTagStyle("body", "face", "SIMHEI");
    styles.LoadTagStyle("body", "encoding", "Identity-H");
    styles.LoadTagStyle("body", "leading", "10,0");
    worker.SetStyleSheet(styles);
    TextReader fileReader = new StringReader(htmlText);
    worker.Parse(fileReader);
    }
    catch (DocumentException ex)
    {
    Console.Error.WriteLine(ex.StackTrace);
    Console.Error.WriteLine(ex.Message);
    }
    document.Close();
    Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
    Response.OutputStream.Flush();
    Response.OutputStream.Close();
    上述方法导出的是单页PDF,若是超出会自动连到下一页。
    若要将PDF分页导出,可将上面蓝字部分修改成下面代码:
    string htmlText = wc.DownloadString(path) + "," + wc.DownloadString(path);
    string[] htmlTextList= htmlText.Split(',');
    foreach (var item in htmlTextList)
    {
    document.NewPage();
    TextReader fileReader = new StringReader(item);
    worker.Parse(fileReader);
    }
  • 相关阅读:
    51nod数字1的数量
    bzoj3669: [Noi2014]魔法森林 lct版
    【NOI2014】起床困难综合症 位运算+贪心
    bzoj2631: tree lct
    bzoj2002 弹飞绵羊 lct版
    codevs1245最小的N个和 小根堆
    RTSP/GB28181/SDK协议视频融合平台EasyCVR接口获取协议平台接入参数的调用方法
    RTSP/GB28181/SDK协议视频融合平台EasyCVR上传通道数据库不显示怎么解决?
    基于视频协议融合平台EasyCVR开发的视频综合管理监控平台EasyCVS通道流检查功能的实现
    RTSP/GB28181/HIKSDK/大华SDK协议安防视频云平台EasyCVR新增告警功能介绍
  • 原文地址:https://www.cnblogs.com/zhangweidong/p/5168865.html
Copyright © 2011-2022 走看看