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,示例内容如下:

<!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 * 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

Pechkin:html -> pdf 利器的更多相关文章

  1. HTML轉PDF - 使用Pechkin套件

    剛好跟人討論到HTML轉PDF需求,便對工具進行簡單評估以備不時之需. 網路上比較多人推的是WkHtmlToPdf,如果是用.NET開發,已經有人包成NuGet套件,直接搜尋pechkin就可找到,它 ...

  2. 使用Pechkin将HTML网页转换为PDF

    Pechkin开源组件使用wkhtmlbox,可以解析CSS样式,将网页转换为PDF文件, 支持URL,或者HTML字符串 1, 从NuGet程序管理器中获得Pechkin  GlobalConfig ...

  3. 《决战大数据:驾驭未来商业的利器》【PDF】下载

    内容简介 大数据时代的来临,给当今的商业带来了极大的冲击,多数电商人无不"谈大数据色变",并呈现出一种观望.迷茫.手足无措的状态.车品觉,作为一名经验丰富的电商人,在敬畏大数据的同 ...

  4. C#导出HTML到PDF组件 Pechkin

    C#导出PDF功能是开发中经常遇到的功能,我们采用第三方的组件,比如 iTextSharp, aspose等,还能搜到一些开源的类库, 但是对于一些内容复杂样式丰富的PDF,我们希望通过传入一个URL ...

  5. C#使用Pechkin与CPechkin生成PDF

    http://blog.sina.com.cn/s/blog_5a52cec70102wpcf.html 1. Pechkin     从NuGet程序管理器中获得Pechkin,代码示例如下:   ...

  6. C#导出HTML到PDF组件Pechkin

    http://www.knowsky.com/898441.html C#导出PDF功能是开发中经常遇到的功能,我们采用第三方的组件,比如 iTextSharp, aspose等,还能搜到一些开源的类 ...

  7. HTML to PDF pechkin

    1. Goto Nuget 下载 Pechkin 控件 2. 创建需要打印的的PDF controller 和 Action, 这里会调用其他页面的内容进行打印. public ActionResul ...

  8. 使用Pechkin与CPechkin生成PDF

    1. Pechkin     从NuGet程序管理器中获得Pechkin,代码示例如下:             GlobalConfig config = new GlobalConfig();   ...

  9. 办公利器!用Python快速将任意文件转为PDF

    痛点: 相信大家都会遇到一种场景.老师/上司要求你把某个文件转为pdf,并且是一批(不止一个,一个的话手动就可以搞定),并且这种是枯燥无聊的工作,既没有什么技术含量又累. 试想一下,如果我把这些文件放 ...

随机推荐

  1. JavaWeb 的学习一

    JavaWeb学习总结(一)——JavaWeb开发入门 一.基本概念 1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Inte ...

  2. oracle 分析函数的使用(1)

    LISTAGG(columnName,'拼接符') WITHIN GROUP(ORDER BY clause) --order by 子句决定拼接内容的顺序 LISTAGG(columnName,'' ...

  3. Oracle SQL Developer如何配置TNS

    安装了ORACLE的SQL Developer 4.0.3.16,但是连接数据库时,如果选择连接类型为"TNS",无法获取网络别名,那么要如何设置,才能访问到TNS文件呢? 此时需 ...

  4. JavaScript(四)——DOM操作——Window.document对象

    一.找到元素: docunment.getElementById("id"):根据id找,最多找一个:    var a =docunment.getElementById(&qu ...

  5. Swift实现截屏并保存相册

    func saveToLocal() { //截屏 let screenRect = UIScreen.mainScreen().bounds UIGraphicsBeginImageContext( ...

  6. 关于《Windows程序设计(第五版)》中一个实例程序的疑问

    最近一直在看Charlse Petzold的<Windows程序设计>,作为一个新得不能再新的新手,只能先照着书的抄抄源码了,之前的例子一直都很正常,但昨天遇到一个很诡异的BUG. 先看实 ...

  7. js 读取 cookie

    (新摘未验证)// 将document.cookie的值以名/值对组成的一个对象返回 // 假设储存cookie的值的时候是采用encodeURIComponent()函数编码的 function g ...

  8. 003.同时Ping多个IP(select实现IO复用,信号计时),ping程序升级版

    写这个的目的主要是为了以后的方便: 1.信号计时函数的使用 2.ip头的构建和icmp头的构建 3.selec函数t的用法 代码实现: /src/ping.h /* * ping.h * * Crea ...

  9. java 生产者消费者问题 并发问题的解决

    引言 生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,如下图所示,生产者向空间里存放数据,而消费者取用数据,如果不加以协调可能会出现以下情况: 生产者消费者图 ...

  10. MT7620a openwrt barrier_breaker编译后wan口dhcp无法获得地址

    前言 我司准备使用openwrt barrier_breaker版本做二次开发.在烧入固件后发现wan口,dhcp无法获得地址.经如下修改后,mt7620a的路由器可以正常获得地址. 修改dts文件 ...