码迷,mamicode.com
首页 > Web开发 > 详细

WkHtmlToPdf 生成 PDF

时间:2016-08-29 19:34:17      阅读:1303      评论:0      收藏:0      [点我收藏+]

标签:

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

2. 执行安装完成

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

C:\Program Files\wkhtmltopdf\bin>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:\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, 0, file.Length); 
        fs.Close();

        //Response给客户端下载 
        Response.Clear(); 
        Response.AddHeader("content-disposition", "attachment; filename=" + fileNameWithOutExtention + ".pdf");//强制下载 
        Response.ContentType = "application/octet-stream"; 
        Response.BinaryWrite(file);


    } 
}

 

如果是用.NET开发,已经有人包成NuGet套件,直接搜寻pechkin就可找到,它有两个版本: Pechkin适用单执行绪,如要非同步执行请选用Pechkin.Synchronized。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Pechkin;
 
namespace Html2Pdf
{
    class Program
    {
        static void Main(string[] args)
        {

//读取HTML
            var config = new GlobalConfig();
            var pechkin = new SimplePechkin(config);
            ObjectConfig oc = new ObjectConfig();
            oc.SetPrintBackground(true)
                .SetLoadImages(true)
                .SetPageUri("http://news.google.com.tw/");
            byte[] pdf = pechkin.Convert(oc);
            File.WriteAllBytes("d:\\temp\\google-news.pdf", pdf);
 
//自制HTML
            string html = @"
<html><head><style>
body { background: #ccc; }
div { 
width: 200px; height: 100px; border: 1px solid red; border-radius: 5px;
margin: 20px; padding: 4px; text-align: center;
}
</style></head>
<body>
<div>Jeffrey</div>
<script>document.write(‘<span>Generated by JavaScript</span>‘);</script>
</body></html>
";
            pdf = pechkin.Convert(oc, html);
            File.WriteAllBytes("d:\\temp\\myhtml.pdf", pdf);
        }
 
    }
}

 

WkHtmlToPdf 生成 PDF

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!