栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

分享一个html转换为pdf 利器 Pechkin

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

分享一个html转换为pdf 利器 Pechkin

Pechkin 是GitHub上的一个开源项目,可方便将html转化成pdf文档,使用也很方便,下面是winform项目中的示例代码:

using System;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
using Pechkin;
using Pechkin.Synchronized;
 
namespace PdfTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void btnCreatePDF_Click(object sender, EventArgs e)
        {
 
            SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig()
                .SetMargins(new Margins() { Left = 0, Right = 0, Top = 0, Bottom = 0 }) //设置边距
                .SetPaperOrientation(true) //设置纸张方向为横向
                .SetPaperSize(ConvertToHundredthsInch(50), ConvertToHundredthsInch(100))); //设置纸张大小50mm * 100mm
 
            byte[] buf = sc.Convert(new ObjectConfig(), this.txtHtml.Text);
 
            if (buf == null)
            {
                MessageBox.Show("Error converting!");
                return;
            }
 
            try
            {
                string fn = Path.GetTempFileName() + ".pdf";
                FileStream fs = new FileStream(fn, FileMode.Create);
                fs.Write(buf, 0, buf.Length);
                fs.Close();
 
                Process myProcess = new Process();
                myProcess.StartInfo.FileName = fn;
                myProcess.Start();
            }
            catch { }
        }
 
 
        private int ConvertToHundredthsInch(int millimeter)
        {
            return (int)((millimeter * 10.0) / 2.54);
        }
    }
}

web项目中也可以使用:

1.  新建一个待打印的页面,比如index.htm,示例内容如下:



    
        
        html打印测试
        
            * { margin:0; padding:0; font-size:12px }
            table { margin:10px; border:2px solid #000; border-collapse:collapse; margin:5px auto }
            th, td { border:1px solid #000; border-collapse:collapse; padding:3px 5px }
            h1 { font-size:24px }
             @media print {
                .no-print { display: none; }
                .page-break { page-break-after: always; }
            }
        
        
    
    
        
            导出pdf
        
        
            
                
                    
                        XXXX报表
                    
                
                
                     序号 
                     栏目1 
                     栏目2 
                     栏目3 
                     栏目4 
                
            
            
                
                     1 
                     数据1 
                     数据2 
                     数据3 
                     数据4 
                
                
                     2 
                     数据1 
                     数据2 
                     数据3 
                     数据4 
                
                
                     3 
                     数据1 
                     数据2 
                     数据3 
                     数据4 
                
                
                     4 
                     数据1 
                     数据2 
                     数据3 
                     数据4 
                
                
                     5 
                     数据1 
                     数据2 
                     数据3 
                     数据4 
                
            
            
                
                     合计: 
                     
                     
                     300.00 
                     300.00 
                
            
        
    

 2、创建一个ashx来生成并输出pdf

using System;
using System.Drawing.Printing;
using System.IO;
using System.Web;
using Pechkin;
using Pechkin.Synchronized;
 
namespace PdfWebTest
{
    /// 
    /// Summary description for CreatePdf
    /// 
    public class CreatePdf : IHttpHandler
    {
 
        public void ProcessRequest(HttpContext context)
        {
 
            string htmlFile = context.Request["html"];
 
            if (!string.IsNullOrWhiteSpace(htmlFile))
            {
                string filePath = context.Server.MapPath(htmlFile);
                if (File.Exists(filePath))
                {
                    string html = File.ReadAllText(filePath);
                    SynchronizedPechkin sc = new SynchronizedPechkin(new GlobalConfig()
                                            .SetMargins(new Margins() { Left = 0, Right = 0, Top = 0, Bottom = 0 }) //设置边距
                                            .SetPaperOrientation(true) //设置纸张方向为横向
                                            .SetPaperSize(ConvertToHundredthsInch(50), ConvertToHundredthsInch(100))); //设置纸张大小50mm * 100mm
 
                    byte[] buf = sc.Convert(new ObjectConfig(), html);
 
                    if (buf == null)
                    {
                        context.Response.ContentType = "text/plain";
                        context.Response.Write("Error converting!");
                    }
 
                    try
                    {                       
                        context.Response.Clear();
                         
 
                        //方式1:提示浏览器下载pdf  
                        //context.Response.AddHeader("content-disposition", "attachment;filename=" + htmlFile + ".pdf");
                        //context.Response.ContentType = "application/octet-stream";
                        //context.Response.BinaryWrite(buf);
 
                        //方式2:直接在浏览器打开pdf
                        context.Response.ContentType = "application/pdf";
                        context.Response.OutputStream.Write(buf, 0, buf.Length);
 
                        context.Response.End();
             
                    }
                    catch(Exception e) {
                        context.Response.ContentType = "text/plain";
                        context.Response.Write(e.Message);
                    }
                }
 
            }
 
        }
 
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
 
 
        private int ConvertToHundredthsInch(int millimeter)
        {
            return (int)((millimeter * 10.0) / 2.54);
        }
    }
}

注意事项:部署web项目时,要保证bin目录下有以下相关dll文件

Common.Logging.dll
libeay32.dll
libgcc_s_dw2-1.dll
mingwm10.dll
Pechkin.dll
Pechkin.Synchronized.dll
ssleay32.dll
wkhtmltox0.dll 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/230963.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号