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. Windows下好用到必须开机自启的小工具

    折腾过linux,黑苹果,最后还是回到了盖茨大叔的windows.得出的结论是,日常使用的话,折腾Linux还不如把精力去拿去折腾windows.分享下折腾的成果,介绍下一些很不错的小工具.     ...

  2. (ios)MPMoviePlayerController首次播放视频的时候,没有控制条

    问题: 在视频播放时,现在控制条采用磨砂的效果,会遮罩部分视频 解决思路 1 播放器直接设置不带控制条,在app在 Foreground状态,默认播放器暂停,这样需要在获得Foreground事件,进 ...

  3. zookeeper barrier和queue应用实例

    package org.windwant.zookeeper; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper. ...

  4. 前端构建工具gulp介绍

    2016年3月3日 10:46:08     晴 前端构建工具gulpjs的使用介绍及技巧 gulpjs是一个前端构建工具,与gruntjs相比,gulpjs无需写一大堆繁杂的配置参数,API也非常简 ...

  5. 在eclipse中使用maven创建springMVC项目

    一.在eclipse中创建maven-archetype-webapp项目: 1.新建项目选择maven项目 2.默认,下一步 3.选择maven-archetype-webapp,其他保持默认即可 ...

  6. js有关时间日期的操作

    var myDate = new Date();var date_string = myDate.getFullYear()+""+((myDate.getMonth()+1)&l ...

  7. Linux和开源已经在2013年开始悄悄主宰世界?

    提到Linux,很多人觉得它依旧不温不火,实际上在这个即将过去的一年,Linux和开源软件已经悄悄主宰了计算领域的各方面,这具体表现在开源项目遍地、Chromebook崛起、SteamOS备受瞩目、A ...

  8. 作为程序员之 Vim(一)

    开始使用 Vim(一) vim被称为是编辑器之神,如果可以学好vim的话,就可以在键盘上 “健指如飞” 了,可以完全摆脱鼠标来进行文本的定位编辑. 当然,vim还可以进行各种配置,装上各种插件,做成 ...

  9. 在Ubuntu上安装docker常见问题

    安装完docker之后,发现docker是装好了,但是运行docker就会报“Segmentation Fault or Critical Error encountered. Dumping cor ...

  10. SQL Server With 递归 日期 循环

    要实现的效果:查询从Date From 到 To 之间的 所有日期: 示例代码如下: DECLARE @DATE_FROM DATETIME = N'2016-05-16';--N'2015-05-1 ...