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,并且是一批(不止一个,一个的话手动就可以搞定),并且这种是枯燥无聊的工作,既没有什么技术含量又累. 试想一下,如果我把这些文件放 ...
随机推荐
- 【转】面向对象设计的SOLID原则
S.O.L.I.D是面向对象设计和编程(OOD&OOP)中几个重要编码原则(Programming Priciple)的首字母缩写. SRP The Single Responsibility ...
- php示例代码之 使用PHP的MySQL标准函数
<?php //连接参数 $host="localhost"; $user="root"; $pwd="111111"; $db=&q ...
- git入门学习(一):github for windows上传本地项目到github
Git是目前最先进的分布式版本控制系统,作为一个程序员,我们需要掌握其用法.Github发布了Github for Windows 则大大降低了学习成本和使用难度,他甚至比SVN都简单. 一.首先在g ...
- XML语言基础1
这学期选修了XML技术这门课,没有发课本,于是参考了W3school教程,整理一下上课的内容. 1.XML简介 XML是一种标记语言,很类似HTML,它不是对HTML的替代,而是对HTML的补充.在大 ...
- apache 虚拟主机详细配置:http.conf配置详解
apache 虚拟主机详细配置:http.conf配置详解 Apache的配置文件http.conf参数含义详解 Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd. ...
- 揭开GrowingIO无埋点的神秘面纱
揭开GrowingIO无埋点的神秘面纱 早在研究用户行为分析的时候,就发现国内的GrowingIO在宣传无埋点技术,最近正好抽出时间来研究一下所谓的无埋点到底是什么样的. 我分六部分来分析一下无埋 ...
- Python基础-day2
1.Python模块python 中导入模块使用import语法格式:import module_name示例1: 导入os模块system('dir')列出当前目录下的所有文件 # _*_ codi ...
- Macbook无法上网,访问不了appstore、safria、网易云等,但QQ、谷歌浏览器可以用--解决方案
---------------------我是分割线 update 2016-09-22 20:55:22----------------------------- 发现之前那个方法也是不稳定,后来 ...
- 发现struct proc_dir_entry内核3.10.17移到internal中去了,倒
struct proc_dir_entry 原:2.6.38.8 在#include <linux/proc_fs.h> 现:3.10.17 在fs/proc/internal.h:str ...
- FTP定时批量下载文件(SHELL脚本及使用方法 )
1. 脚本实例 将以下脚本保存为 getftp.sh #!/bin/bash datesign=`date -d -95day +%Y%m%d` ftp -nv 12.2.2.28 << ...