1. 首先去http://wkhtmltopdf.org/downloads.html 下载最新版本的安装包

2. 执行安装完成

3. CMD 命令行运行wkhtmltopdf.exe程序生成PDF

C:\Program Files\wkhtmltopdf\bin>wkhtmltopdf.exe --orientation Landscape --javascript-delay  c:\BPReport.html c:\BPReport_L.pdf
Loading pages (/)
Counting pages (/)
Resolving links (/)
Loading headers and footers (/)
Printing pages (/)
Done

参数:

--orientation Landscape 是横向导出

--javascript-delay 5000  是延时5秒导出,用于页面异步加载数据时可以导出到PDF

代码调用exe

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/*要引用以下命名空间*/
using System.Diagnostics;
using System.IO; public partial class _Default : System.Web.UI.Page
{ //Button的Click事件(把Url的网页内容转成PDF)
protected void btn_execute_Click(object sender, EventArgs e)
{ //因为Web 是多线程环境,避免甲产生的文件被乙下载去,所以档名都用唯一
string fileNameWithOutExtention = Guid.NewGuid().ToString(); //执行wkhtmltopdf.exe
Process p = System.Diagnostics.Process.Start(@"D:\wkhtmltopdf\wkhtmltopdf.exe", @"http://msdn.microsoft.com/zh-cn D:\" + fileNameWithOutExtention + ".pdf"); //若不加这一行,程序就会马上执行下一句而抓不到文件发生意外:System.IO.FileNotFoundException: 找不到文件 ''。
p.WaitForExit(); //把文件读进文件流
FileStream fs = new FileStream(@"D:\" + fileNameWithOutExtention + ".pdf", FileMode.Open);
byte[] file = new byte[fs.Length];
fs.Read(file, , file.Length);
fs.Close(); //Response给客户端下载
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=" + fileNameWithOutExtention + ".pdf");//强制下载
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(file); }
}

如果要在Web项目中导入Pechkin的话,有许多很雷的注意事项,以下是这几天导入项目的经验…

实作

1.首先在ASP.net MVC项目里,项目的建置平台目标维持预设的「Any CPU」即可,虽说WkHtmlToPdf.exe是32位应用程序,但之后布署在IIS上的相关32位设定并不是从Web项目设定的

如果要在Web项目中导入Pechkin的话,有许多很雷的注意事项,以下是这几天导入项目的经验…

实作

1.首先在ASP.net MVC项目里,项目的建置平台目标维持预设的「Any CPU」即可,虽说WkHtmlToPdf.exe是32位应用程序,但之后布署在IIS上的相关32位设定并不是从Web项目设定的


 

2.要加入Pechkin套件的话,不能从NuGet或官网(https://github.com/gmanny/Pechkin)下载使用

因为Pechkin原始作者释出来的套件在Web项目中使用的话会有DLL档案Lock住的问题,如果产过一次PDF档,之后Web项目就再也Build不过


 

网络上已有人释出修正后的版本:https://github.com/tuespetre/Pechkin

建议直接下载这个:https://pechkinwebtest.codeplex.com/downloads/get/729855

3.将上述的载点档案PechkinDLLs.zip下载解压后,会有以下几个档

Web项目加入参考「Common.Logging.dll」、「Pechkin.dll」


 

然后把剩下的五个.dll复制到Web项目根目录下,不然会无法产PDF檔


 

再对着那五个.dll设定属性>复制到输出目录> 永远复制


 

4..dll参考都准备完毕,接下来是程序代码实作

注意点1:产生PDF对象的Url,须是Http开头的绝对路径URL,而不是直接用Url.Action()方法

注意点2:因为是另一条执行绪另一个工作阶段发出Request产出PDF,所以Session不共享,如果要把需要登入才可以看得到的页面产出PDF档的话,要另外套不用登入也可以看得到的画面给Pechkin呼叫

HomeController.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Pechkin;
using System.IO;


namespace WkHtmlToPdf
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}


protected void button1_Click(object sender, EventArgs e)
{
string url = "http://www.cnblogs.com";

var config = new GlobalConfig();
config //.SetPaperOrientation(true)//设置纸张方向为横向
.SetMargins(new System.Drawing.Printing.Margins(0, 0, 0, 0));
using (IPechkin pechkin = Factory.Create(config))

{


ObjectConfig oc = new ObjectConfig();
oc.SetPrintBackground(true)
.SetLoadImages(true)
.SetScreenMediaType(true)
.SetPageUri(url)
.SetRunJavascript(true) //允许javaScript
.SetRenderDelay(5000);//延时5秒;
byte[] pdf = pechkin.Convert(oc);
File.WriteAllBytes("c:\\BPReport-news.pdf", pdf);
}


}
}
}

 

※注意此套件不支持Gif图片

※如果执行过程中发生Common.Logging错误

Could not load file or assembly 'Common.Logging' or one of its dependencies.


 

要先看加入参考的Common.Logging.dll档案版本(2.1.1.0)

再确保Web.config里的assemblyBinding区段设定也是一样的档案版本即可


 

6.接下来如果直接将网站布署到IIS上的话,会出现错误

无法加载档案或组件 'Pechkin' 或其相依性的其中之一。 试图加载格式错误的程序。

Could not load file or assembly ‘Pechkin’ or one of its dependencies. An attempt was made to load a program with an incorrect format.


 

这要看网站使用的是哪个应用程序集区,再设定启用32位应用程序


 

※即使部署的机器操作系统是64位,有把应用程序集区「启用32位应用程序」的话,网站也是可以正常执行。

到这边,Pechkin在Web项目上的设定才算全部完成~

最新文章

  1. 图解集合4:HashMap
  2. SqlServer中——查找杀死阻塞进程
  3. mfc截图
  4. SQL 显示所有表所占存储空间
  5. oracle 如何恢复误删的表记录数据
  6. LightOJ 1138 Trailing Zeroes (III) 打表
  7. How to select a OptionSet on ms sqlserver database for Microsoft Dynamics CRM
  8. logstash 通过mysql 慢日志了解(?m)
  9. 探秘ReSharper8.1版本中Architecture(架构工具)的改进
  10. strictmode
  11. GUI记事本+切换面板1.1版
  12. 菜鸟详细解析Cookie注入原理
  13. [USACO11DEC]牧草种植Grass Planting
  14. java第十次笔记
  15. Debian root登录设置
  16. 【maven】Maven根据Profile读取不同配置环境配置文件
  17. Vue 表格内容根据后台返回状态位填充文字
  18. LigerUI 树状列表折叠显示
  19. C#笔记 -- 协变、逆变
  20. 常见企业IT支撑【4、gitlab代码管理工具】

热门文章

  1. fiddler 拦截小结
  2. Tomcat_启动多个tomcat时,会报StandardServer.await: Invalid command '' received错误
  3. List相关知识点.......课堂加整理
  4. ArcGIS Javascript 异常之No 'Access-Control-Allow-Origin' header
  5. Winform程序以Icon的形式显示在任务栏右下角
  6. background-size扫盲
  7. ajax异步验证用户名密码,提示路径错误
  8. asp.Net2.0中TextBox设置只读后后台获取不到值的解决方法
  9. Xstream(对象和xml转换)
  10. Sharepoint 2013 发布功能(Publishing features)