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 * 100mmbyte[] 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,示例内容如下:

<!doctype html>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>html打印测试</title> <style type="text/css" media="all"> * { 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; } } </style> <script type="text/javascript">function createPdf() {               window.open("CreatePdf.ashx?html=index.htm");}</script></head> <body> <div class="no-print" style="text-align:center;margin:5px"> <button onClick="createPdf()">导出pdf</button> </div> <table > <thead> <tr> <th colspan="5"> <h1>XXXX报表</h1> </th> </tr> <tr> <th> 序号 </th> <th> 栏目1 </th> <th> 栏目2 </th> <th> 栏目3 </th> <th> 栏目4 </th> </tr> </thead> <tbody> <tr> <td> 1 </td> <td> 数据1 </td> <td> 数据2 </td> <td> 数据3 </td> <td> 数据4 </td> </tr> <tr class="page-break"> <td> 2 </td> <td> 数据1 </td> <td> 数据2 </td> <td> 数据3 </td> <td> 数据4 </td> </tr> <tr> <td> 3 </td> <td> 数据1 </td> <td> 数据2 </td> <td> 数据3 </td> <td> 数据4 </td> </tr> <tr> <td> 4 </td> <td> 数据1 </td> <td> 数据2 </td> <td> 数据3 </td> <td> 数据4 </td> </tr> <tr> <td> 5 </td> <td> 数据1 </td> <td> 数据2 </td> <td> 数据3 </td> <td> 数据4 </td> </tr> </tbody> <tfoot> <tr> <th> 合计: </th> <th> </th> <th> </th> <th> 300.00 </th> <th> 300.00 </th> </tr> </tfoot> </table> </body>
</html>

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

using System;
using System.Drawing.Printing;
using System.IO;
using System.Web;
using Pechkin;
using Pechkin.Synchronized;namespace PdfWebTest
{/// <summary>/// Summary description for CreatePdf/// </summary>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 * 100mmbyte[] 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:直接在浏览器打开pdfcontext.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

Pechkin:html - pdf 利器相关推荐

  1. 慕课的原型图快速变html,分享一个html转换为pdf 利器 Pechkin

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

  2. html实现在线展示PDF,html展示 pdf 利器

    Pechkin是GitHub上的一个开源项目,可方便将html转化成pdf文档,使用也很方便,下面是winform项目中的示例代码: web项目中也可以使用: 1.  新建一个待打印的页面,比如ind ...

  3. 别总写代码,这100多个相见恨晚的网站比涨工资都重要

    来自 | CSDN    作者 | 爪白白 编辑 | 深度学习这件小事    搞学习 CSDN:https://www.csdn.net/ TED(最优质的演讲):https://www.ted.co ...

  4. 学习,工作,编程必看:130 个相见恨晚的神器网站

    来源: https://blog.csdn.net/qq_43901693/article/details/100606828 作者:爪白白 搞学习 知乎:www.zhihu.com TED(最优质的 ...

  5. 相见恨晚的超实用网站

    文章目录 130 余个相见恨晚的超实用网站 搞学习 找书籍 冷知识 / 黑科技 写代码 资源搜索 小工具 导航页(工具集) 看视频 学设计 搞文档 找图片 搞学习 CSDN: https://www. ...

  6. 130 余个相见恨晚的超实用网站,总有一个用得着

    Python实战社群 Java实战社群 长按识别下方二维码,按需求添加 扫码关注添加客服 进Python社群▲ 扫码关注添加客服 进Java社群▲ 作者丨Caesar 来源丨手机电脑双黑客 文章目录 ...

  7. 130 个令你眼前一亮的网站,总有用得着的

    " 作者:爪白白 链接blog.csdn.net/qq_43901693/article/details/100606828 " 搞学习 CSDN:https://www.csdn ...

  8. 130 余个相见恨晚的超实用网站

    文章目录 130 余个相见恨晚的超实用网站 搞学习 找书籍 冷知识 / 黑科技 写代码 资源搜索 小工具 导航页(工具集) 看视频 学设计 搞文档 找图片 搞学习 CSDN: https://www. ...

  9. 推荐 130 个令你眼前一亮的网站,总有一个用得着

    130 余个相见恨晚的超实用网站 文末没有公众号,只求 点赞 + 关注 文章目录 130 余个相见恨晚的超实用网站 搞学习 找书籍 冷知识 / 黑科技 写代码 资源搜索 小工具 导航页(工具集) 看视 ...

最新文章

  1. 云服务蓬勃发展,平均年增长率高达28%
  2. 2018,抢票大作战
  3. 【★★★★★模板专区★★★★★】
  4. CRM_REPORT_RF_AUTH_OBJ_ORD_LP
  5. 安卓动画知识总结 Animation AnimationSet LayoutAnimation
  6. python writelines_Python文件writelines()方法
  7. 实现CentOS 中的单窗口打开文件夹
  8. 如何格式化用过的磁带,让他被新磁带机重复利用
  9. C#1所搭建的核心基础
  10. 微信支付宝扫码支付相关接口
  11. java xps生成_Java 将PDF/XPS转为Word/html /SVG/PS/PCL/PNG、PDF和XPS互转(基于Spire.Cloud.SDK for Java)...
  12. 计算机禁止开机自启动,通过禁止开机启动项快速开机,提升电脑流畅度,拒绝自启...
  13. asc超级计算机题目,ASC世界大学生超级计算机竞赛赛题:单张图像超分辨率
  14. “四大神兽”拆机指北
  15. 现代C++的文艺复兴
  16. day11-单点登录系统
  17. 基于Springboot实现OA办公系统
  18. chrome浏览器不成设置成默认浏览器
  19. PMI-ACP练习题(17)
  20. java吃货联盟的实训报告_作业:吃货联盟

热门文章

  1. 华为畅享10s值得买吗_华为畅享10 Plus值不值得买?首批购买用户评价出炉
  2. 【JAVA基础篇】final、finally和finalize
  3. php不用于输出的函数,PHP常用函数和常见疑难问题解答
  4. java删除csv一行_在Java中读取CSV文件时跳过第一行
  5. tensorboard报错:ValueError Duplicate plugins for name projector 问题的出现及解决过程
  6. 条件控制与条件传送详解
  7. c语言 三个小球排排坐,关颖三个孩子排排坐 太萌啦
  8. hibernate native oracle,hibernate native 主键生成策略
  9. 太阳花图片_长寿花扔水里,光长叶不开花?赶紧加点营养液
  10. eview面板数据之混合回归模型_【视频教程】Eviews系列25|面板数据回归分析之Hausman检验及本章常见问题解答...