WkHtmlToPdf 生成 PDF

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

2. 执行安装完成

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

C:Program Fileswkhtmltopdfin>wkhtmltopdf.exe --orientation Landscape --javascript-delay 5000 c:BPReport.html c:BPReport_L.pdf
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
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:wkhtmltopdfwkhtmltopdf.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, 0, 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项目上的设定才算全部完成~

原文地址:https://www.cnblogs.com/colder/p/5819181.html