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 * 100mmbyte[] 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 * 100mmbyte[] 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:直接在浏览器打开pdfcontext.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 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/547482.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

packer build 报错 无任何输出 一直报“skipping line: 1 skipping line: 2 skipping line: 3.....”

最近使用packer build 报错 无任何输出 一直报“skipping line: 1 skipping line: 2 skipping line: 3.....” &#xff0c; 解决方法如下&#xff1a; # Install wget and unzip before executing the below steps wget https://releases.hashicorp.com/packer/1.0.0/packer_…

使用ZeroClipboard解决跨浏览器复制到剪贴板的问题

Zero Clipboard的实现原理Zero Clipboard 利用透明的Flash让其漂浮在复制按钮之上&#xff0c;这样其实点击的不是按钮而是 Flash &#xff0c;这样将需要的内容传入Flash&#xff0c;再通过Flash的复制功能把传入的内容复制到剪贴板。 Zero Clipboard的安装方法首先需要下载 …

android inflate 三个参数的含义,LayoutInflater.inflate 3个参数的含义

LayoutInflater.inflate 天天都在用但对它 3 个参数的含义没有很好的理解之前 root 一直传 null , 现在才知道传 null 在多数情况下都是不对的public View inflate(LayoutRes int resource, Nullable ViewGroup root, boolean attachToRoot)第一个参数是布局ID 没什么说的第二个…

详解UML中的聚合,关联,泛化等关系

1. Overview UML设计类中&#xff0c;类的关系分为Generalization(泛化)&#xff0c;Dependency(依赖关系)、Association(关联关系)、Aggregation(聚合关系)、Composition(组合关系)五种! 2. Generalization(泛化) Generalization(泛化)表现为继承或实现关系(is a)。具体形式为…

CentOS 8 安装 Docker  报错  requires containerd.io >= 1.4.1, but none of the providers can be installed

CentOS 8.1安装 Docker 官方参考地址&#xff1a;https://docs.docker.com/install/linux/docker-ce/centos/ 里面包含包下载地址&#xff1a;https://download.docker.com/linux/centos/8/x86_64/stable/Packages/containerd.io-1.4.3-3.1.el8.x86_64.rpm 一。确认CentOS 版…

PHP获取图片宽度高度、大小尺寸、图片类型、用于布局的img属性

//php自带函数 getimagesize()$img_info getimagesize(tomener.jpg); echo <pre>; print_r($img_info); 输出&#xff1a; Array ( [0] > 170 [1] > 254 [2] > 2 [3] > width"170" height"254" [bits] > 8 [channels] > 3 […

CentOS 8安装并配置NFS服务

先决条件 我们假设您有一台运行CentOS 8的服务器&#xff0c;我们将在该服务器上设置NFS服务器和其他充当NFS客户端的计算机。服务器和客户端应该能够通过专用网络相互通信。如果您的托管服务提供商不提供私有IP地址&#xff0c;则可以使用公共IP地址并配置服务器防火墙&#…

android.support.v7 fragme,打造最强RecyclerView侧滑菜单,长按拖拽Item,滑动删除Item

前几天写了一片关于RecyclerView滑动删除Item&#xff0c;RecyclerView长按拖拽Item的博客&#xff0c;本来很简单一个使用&#xff0c;阅读量还挺高的&#xff0c;原博客传送门。今天介绍一个RecyclerView Item侧滑菜单&#xff0c;RecyclerView滑动删除Item&#xff0c;Recyc…

有关PHP、HTML单引号、双引号转义以及转成HTML实体的那些事!

一、单引号和双引号转义在PHP的数据存储过程中用得比较多&#xff0c;即往数据库里面存储数据时候需要注意转义单、双引号&#xff1b; 先说几个PHP函数&#xff1a; 1、addslashes — 使用反斜线引用&#xff08;转义&#xff09;字符串&#xff1b; 返回字符串&#xff0c;…

设为首页 和 收藏本站js代码 兼容IE,chrome,ff

设为首页 和 收藏本站js代码 兼容IE,chrome,ff//设为首页function SetHome(obj,url){try{obj.style.behaviorurl(#default#homepage);obj.setHomePage(url);}catch(e){if(window.netscape){try{netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect&q…

android listview 数据数组制作,android – 从对象的数组列表中填充listview

在你的活动AdapterPerson adbPerson;ArrayList myListItems new ArrayList();//then populate myListItemsadbPerson new AdapterPerson (youractivity.this, 0, myListItems);listview.setAdapter(adbPerson);适配器public class AdapterPerson extends ArrayAdapter {privat…

centos 8 安装使用配置

服务端安装nfs 1、使用yum安装nfs yum install nfs-utils nfs-utils-lib -y 如果出现上述错误请安装lvm2 yum install -y lvm2 2、编辑文件exports vim /etc/exports 加入代码&#xff0c;如&#xff1a; /home *(insecure,rw,sync,no_root_squash) #参数详解 ro #只读共享…

linux内核笔记-内核同步

linux内核就相当于不断对请求进行响应的服务器&#xff0c;这些请求可能来自CPU&#xff0c;可能来自发出中断的外部设备。我们将内核看作两种请求的侍者。 &#xff08;1&#xff09;老板提出请求&#xff0c;侍者如果空闲&#xff0c;为老板服务。&#xff08;系统调用或异常…

ECshop 快捷登录插件 支持QQ 支付宝 微博

亲自测试可以使用&#xff0c;分享给大家。(承接各种EcShop改版&#xff0c;二次开发等相关项目 QQ:377898650) 安装的时候按照里面说明。安装即可。 代码下载&#xff1a;http://pan.baidu.com/s/1c0kUYIk -------------------------------- 代码修改过程-------------首先a…

Net连接mysql的公共Helper类MySqlHelper.cs带MySql.Data.dll下载

MySqlHelper.cs代码如下&#xff1a; using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text;using System.Data;using System.Collections;using MySql.Data.MySqlClient;using MySql.Data.Types;using System.Configuration;…

有史以来最详细 安装部署Kubernetes Dashboard (补充解决官方出现的一些RBAC CERT等问题)

安装部署Kubernetes Dashboard (补充解决官方出现的一些RBAC CERT等问题) 官方文档&#xff1a;https://github.com/kubernetes/dashboard 参考文章&#xff1a;https://kuboard.cn/install/install-k8s-dashboard.html# 前言 Kubernetes Dashboard 是 Kubernetes 的官方 W…

技术者的好奇心和惯性

这回老孙讲讲好奇心。本杰明富兰克林&#xff0c;十八世纪美国最伟大的科学家和发明家&#xff0c;著名的政治家、外交家、哲学家、文学家和航海家以及美国独立战争的伟大领袖。有人称其为“资本主义精神最完美的代表”。富兰克林这个人或许衬不上这等称誉&#xff0c;但绝对是…

2s相机 android6,Android Camera2 使用总结

最近在做自定义相机相关的项目&#xff0c;网上查了资料都是有关android.hardware.Camera的资料&#xff0c;开始使用的才发现这个类已经废弃了。Android 5.0(21)之后android.hardware.Camera就被废弃了&#xff0c;取而代之的是全新的android.hardware.Camera2 。Android 5.0对…

简单的小工具wordlight——让VS变量高亮起来

前段时间一直在使用matlab&#xff0c;今天需要使用vs2008&#xff0c;而用惯了matlab&#xff0c;习惯了其中一项选中变量高亮的设置&#xff0c;突然回来使用VS&#xff0c;感到各种不适应&#xff0c;顿时想到了一个词&#xff1a;矫情 呵呵&#xff0c;于是在网上找各种插…

CentOS 7上搭建Spark3.0.1+ Hadoop3.2.1分布式集群

CentOS 7上搭建Spark3.0.1 Hadoop3.2.1分布式集群 VMWare 安装CentOS 7使用Xshell连接虚拟机集群设置安装JDK 1.8SSH 免密登陆安装hadoop 3.2安装Spark 3.0.1总结VMWare 安装CentOS 7 推荐使用VMware Workstation Pro 16&#xff0c;下载安装即可。下载最新的CentOS 7 Minimal…