剛好跟人討論到HTML轉PDF需求,便對工具進行簡單評估以備不時之需。

網路上比較多人推的是WkHtmlToPdf,如果是用.NET開發,已經有人包成NuGet套件,直接搜尋pechkin就可找到,它有兩個版本: Pechkin適用單執行緒,如要非同步執行請選用Pechkin.Synchronized。

安裝NuGet套件後,相關Unmanage DLL也會一併下載並加入專案,不用額外安裝HkHtmlToPdf就可開始寫程式,十分方便。但由於Unmanaged部分為32位元,記得要將專案目標平台切成x86。

參考Pechkin作者在GitHub的FAQ,我寫了一個簡單範例,分別將Google新聞首頁及自己產生的HTML轉成PDF:

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)
        {
            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);
 
            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);
        }
 
    }
}

實測效果挺不錯,跟實際網站所看到的很接近: (但不完全相同)

在自製HTML測試中,我使用<script>docuemt.write(…)</script>在網頁中插入內容,在PDF中也正確顯示。

但在某些狀況下,產生的PDF會與實際網頁差異頗大(例如: 我的Blog首頁轉PDF後排版就亂了),有可能是網頁的某些Style寫法或複雜JavaScript超出WkHtmlToPdf預期,也有可能是元件的Bug,感覺眉角還很多,留待實際遇到時再深究。


Comments

# by sidihu

你好 我用nuget下的Pechkin并部署在程序中,仿照你的代码写了获取一个站点页面的代码,但是程序运行的时候报错:无法加载 DLL“wkhtmltox0.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。 网上也没有找到解决方案,请问你知道该如何解决吗?

# by Jeffrey

to sidihu, 專案目標平台是否設成x86? 檢查wkhtmltox0.dll是否有被複製到bin目錄下? 理論上它應該會被自動複製過去,網路上查到一則類似Issue回報[https://github.com/gmanny/Pechkin/issues/1],但久在兩年前,應該已修復。

# by ykk

關於sidihu的提問,我也遇到相同的問題,以下回覆記載解決方式: (1)將packages --> Pechkic 版本號的資料夾 ---> content 中的5 個檔案複製貼上到專案的bin資料夾,即可解決,至少我是這樣解 決的。

# by Jeffrey

to ykk, 感謝分享藥方。

# by 独立观察员

挺好用的

# by Alex

個人測試 使用 Pechkin 效果不佳 回頭使用 WKHtmlToPdf (不是 同名的 nuget package) 效果還不錯

Post a comment