zoukankan      html  css  js  c++  java
  • C# Html转pdf文件

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net;
    using iTextSharp.tool.xml;
    
    namespace WebApplication1
    {
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
                string htmlText = "<html><body><div>testtest</div></body></html>";
    
    
    
                //避免当htmlText无任何html tag标签的纯文字时,转PDF时会挂掉,所以一律加上<p>标签
                htmlText = "<p>" + htmlText + "</p>";
    
                MemoryStream outputStream = new MemoryStream();//要把PDF写到哪个串流
                byte[] data = Encoding.UTF8.GetBytes(htmlText);//字串转成byte[]
                MemoryStream msInput = new MemoryStream(data);
                Document doc = new Document();//要写PDF的文件,建构子没填的话预设直式A4
                PdfWriter writer = PdfWriter.GetInstance(doc, outputStream);
                //指定文件预设开档时的缩放为100%
    
                PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 1f);
                //开启Document文件 
                doc.Open();
    
                //使用XMLWorkerHelper把Html parse到PDF档里
                // XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8, new UnicodeFontFactory());
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msInput, null, Encoding.UTF8);
    
                //将pdfDest设定的资料写到PDF档
                PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);
                writer.SetOpenAction(action);
                doc.Close();
                msInput.Close();
                outputStream.Close();
                //回传PDF档案 
                var bytes = outputStream.ToArray();
    
                var ret = Convert.ToBase64String(bytes);
                try
                {
                    string strbase64 = ret;
                    strbase64 = strbase64.Replace(' ', '+');
                    System.IO.MemoryStream stream = new System.IO.MemoryStream(Convert.FromBase64String(strbase64));
                    System.IO.FileStream fs = new System.IO.FileStream("D:\证件1.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write);
                    byte[] b = stream.ToArray();
                    //byte[] b = stream.GetBuffer();
                    fs.Write(b, 0, b.Length);
                    fs.Close();
    
                }
                catch (Exception ex)
                {
    
                }
            }
        }
    }
  • 相关阅读:
    一行代码搞定Dubbo接口调用
    测试周期内测试进度报告规范
    jq 一个强悍的json格式化查看工具
    浅析Docker容器的应用场景
    HDU 4432 Sum of divisors (水题,进制转换)
    HDU 4431 Mahjong (DFS,暴力枚举,剪枝)
    CodeForces 589B Layer Cake (暴力)
    CodeForces 589J Cleaner Robot (DFS,或BFS)
    CodeForces 589I Lottery (暴力,水题)
    CodeForces 589D Boulevard (数学,相遇)
  • 原文地址:https://www.cnblogs.com/parkerchen/p/12864928.html
Copyright © 2011-2022 走看看