Pechkin:html -> pdf 利器
时间:2022-04-24
本文章向大家介绍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
- silverlight3中的"伪"3D
- 暴利驱动的疯狂游戏“外挂”:非法获利可达数百万;X-Agent 后门大升级,俄罗斯 APT28 间谍活动更为隐蔽
- Nginx+keepalived双机热备(主从模式)
- 即使不做程序员,也要学会像程序员一样去思考
- 这5项高科技 正在颠覆未来医疗
- xml的解析
- “微信身份证”来了!下月起全国推广!
- silverlight3的"伪"3D续--图片横向轮换
- 从MapX到MapXtreme2004[5]-自定义工具
- 米市的热潮来袭?三枚域名超六位数美金交易
- 微信新出了逆天功能,或许你用得上
- 网站系统架构梳理-解决高负载高并发
- silverlight 相册雏型
- Mysql优化系列(1)--Innodb引擎下mysql自身配置优化
- HTML 教程
- HTML 简介
- html div 标签介绍
- html span 标签介绍
- html a 超链接标签
- HTML Br换行标签介绍
- HTML P段落标签介绍
- HTML br与p标签区别
- Html H 标题标签
- html px em pt长度单位
- HTML form 标签
- HTML radio 单选框
- HTML B 加粗标签
- HTML strong加粗粗体标签
- HTML em 强调标签
- HTML i 斜体标签
- HTML u下划线标签
- HTML s 删除线标签
- Html img 图片标签
- Html上标注sup与下标注sub标签
- HTML nobr 禁止换行标签
- HTML hr 水平线标签
- HTML label 标签
- HTML input 标签
- HTML textarea 标签
- HTML select下拉列表标签
- HTML checkbox 多选框
- HTML font color 标签
- HTML iframe 框架标签
- HTML Table 表格
- HTML dl dt dd 标签
- HTML ol li有序列表标签
- HTML ul li 无序列表标签
- HTML 注释
- CSS 教程
- CSS 简介
- CSS 语法
- CSS Id 和 Class选择器
- CSS 样式的创建
- CSS background 背景介绍
- CSS 文本样式
- CSS font 字体
- CSS A 链接
- CSS ul ol列表样式
- CSS TABLE 样式
- CSS 框模型
- CSS border 边框
- CSS Outlines 轮廓
- CSS 外边距 Margin
- CSS Padding 内边距
- CSS 分组和嵌套选择器
- CSS 尺寸 (Dimension)
- CSS Display 属性
- CSS Position 定位
- CSS Float 浮动
- CSS 水平对齐(Horizontal Align)
- CSS 组合选择符
- CSS 伪类
- CSS 伪元素
- CSS 导航栏
- CSS 下拉菜单
- CSS 图片廊
- CSS 图像透明/不透明
- CSS sprite 图像拼合技术
- CSS 媒体类型
- CSS 属性选择器
- CSS 实例