Pechkin:html -> pdf 利器
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 利器的更多相关文章
- HTML轉PDF - 使用Pechkin套件
剛好跟人討論到HTML轉PDF需求,便對工具進行簡單評估以備不時之需. 網路上比較多人推的是WkHtmlToPdf,如果是用.NET開發,已經有人包成NuGet套件,直接搜尋pechkin就可找到,它 ...
- 使用Pechkin将HTML网页转换为PDF
Pechkin开源组件使用wkhtmlbox,可以解析CSS样式,将网页转换为PDF文件, 支持URL,或者HTML字符串 1, 从NuGet程序管理器中获得Pechkin GlobalConfig ...
- 《决战大数据:驾驭未来商业的利器》【PDF】下载
内容简介 大数据时代的来临,给当今的商业带来了极大的冲击,多数电商人无不"谈大数据色变",并呈现出一种观望.迷茫.手足无措的状态.车品觉,作为一名经验丰富的电商人,在敬畏大数据的同 ...
- C#导出HTML到PDF组件 Pechkin
C#导出PDF功能是开发中经常遇到的功能,我们采用第三方的组件,比如 iTextSharp, aspose等,还能搜到一些开源的类库, 但是对于一些内容复杂样式丰富的PDF,我们希望通过传入一个URL ...
- C#使用Pechkin与CPechkin生成PDF
http://blog.sina.com.cn/s/blog_5a52cec70102wpcf.html 1. Pechkin 从NuGet程序管理器中获得Pechkin,代码示例如下: ...
- C#导出HTML到PDF组件Pechkin
http://www.knowsky.com/898441.html C#导出PDF功能是开发中经常遇到的功能,我们采用第三方的组件,比如 iTextSharp, aspose等,还能搜到一些开源的类 ...
- HTML to PDF pechkin
1. Goto Nuget 下载 Pechkin 控件 2. 创建需要打印的的PDF controller 和 Action, 这里会调用其他页面的内容进行打印. public ActionResul ...
- 使用Pechkin与CPechkin生成PDF
1. Pechkin 从NuGet程序管理器中获得Pechkin,代码示例如下: GlobalConfig config = new GlobalConfig(); ...
- 办公利器!用Python快速将任意文件转为PDF
痛点: 相信大家都会遇到一种场景.老师/上司要求你把某个文件转为pdf,并且是一批(不止一个,一个的话手动就可以搞定),并且这种是枯燥无聊的工作,既没有什么技术含量又累. 试想一下,如果我把这些文件放 ...
随机推荐
- LAMP配置虚拟目录
1. httpd.conf中添加 Listen 81 2. 1 <VirtualHost 127.0.0.2:81> 2 DocumentRoot E:\ws\2011\DiscuzSp ...
- mysql substring_index substring left right方法
函数简介: SUBSTRING(str,pos) , SUBSTRING(str FROM pos) SUBSTRING(str,pos,len) , SUBSTRING(str FROM pos F ...
- vmware克隆虚拟机eth0网卡无法启动
概述: 通过vmware克隆安装好的虚拟机之后,出现了网卡未启动的问题. vmware安装虚拟机请看:<vmware快速安装linux虚拟机>. 定位过程: 1.通过ifocnfig命令只 ...
- Sqlite学习笔记(五)&&SQLite封锁机制
概述 SQLite虽然是一个轻量的嵌入式数据库,但这并不影响它支持事务.所谓支持事务,即需要在并发环境下,保持事务的ACID特性.事务的原子性,隔离性都需要通过并发控制来保证.那么Sqlite的并发控 ...
- mysql online ddl
大家知道,互联网业务是典型的OLTP(online transaction process)应用,这种应用访问数据库的特点是大量的短事务高并发运行.因此任何限制高并发的动作都是不可接受的,甚至 ...
- Javascript刷新页面的几种方法
Javascript刷新页面的几种方法: window.navigate(location)location.reload()location=locationlocation.assign(loca ...
- x01.BitmapHelper:图像处理
“所有致我于死地的,也激发我胆魄”,姚贝娜的<心火>,是我近年来听过最好的歌,特此推荐一下. 图像处理,大概分三步:1.LockBits():2.进行处理:3.UnlockBits():这 ...
- android Bitmap类方法属性 详细说明
(转:http://blog.csdn.net/ymangu666/article/details/37729109) 1. BitMap类public void recycle()——回收位图占用 ...
- /usr/include/sys/types.h:62: error: conflicting types for ‘dev_t’
/usr/include/sys/types.h:62: error: conflicting types for ‘dev_t’/usr/include/linux/types.h:13: erro ...
- 必须知道的八大种排序算法【java实现】(三) 归并排序算法、堆排序算法详解
一.归并排序算法 基本思想: 归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的.然后再把有序子序列合并为整体有序序列. 归并 ...