C#使用wkhtmltopdf,把HTML生成PDF(包含分頁)


 

最近花了2天多的時間終於把HTML生成PDF弄好了。步驟如下:

 

1、首先是技術選型。看了好多都是收費的就不考慮了。

免費的有:

  1. jsPDF(前端生成,清晰度不高,生成比較慢)
  2. iText(嚴格要求html標簽。這個好像也是收費的)
  3. wkhtmltopdf(簡單、配置選項多、生成快、支持跨平台、也支持HTML生成圖片) 

因此選擇wkhtmltopdf。

 

2、前期准備,首先需要下載wkhtmltopdf.exe(下載地址:https://wkhtmltopdf.org/downloads.html)閱讀配置參數(https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

常用參數:

  1. -T 0 :設置上下左右margin-top=0(-B 0 -L 0 -R 0 -T 0,上下左右都設置一下)
  2. -s A4:設置A4紙大小,默認A4
  3. --disable-smart-shrinking:禁止縮放(不設置這個,生成的pdf會縮放)
  4. --zoom 1:設置縮放系數,默認為1。如果--disable-smart-shrinking設置了,--zoom就不用設置了。
  5. --cookie name value:設置cookie,如果下載的url需要登錄(用cookie),那么這個參數很重要。

 

3、設置需要打印的頁面(核心是分頁)

A4紙大小:210mm×297mm,因此頁面的每個div大小也是A4紙大小。

 

這里的頁面設置很重要。另外,設置了分頁的頁碼,示例如下:

<style>
#view {
    height: 100%;
    margin: auto;
    padding: 0;
    width: 210mm;
}

/*設置A4打印頁面*/
/*備注:由於@是否特殊符號,樣式放在css文件中沒問題,放在cshtml文件就不行了,需要@@。*/
@preview-item {
    size: A4;
    margin: 0;
}
 
@media print {
    .preview-item {
        margin: 0;
        border: initial;
        border-radius: initial;
        width: initial;
        min-height: initial;
        box-shadow: initial;
        background: initial;
        page-break-after: always;
    }
}
.preview-item {
    width: 100%;
    height: 297mm;
    position: relative;
}

.page-view {
    position: absolute;
    width: 100%;
    text-align: center;
    height: 60px;
    line-height: 60px;
    bottom: 0;
}
</style>

<div id="view">
    <div class="preview-item">
        <div class="preview-item-body">這是第一頁</div>
        <div class="page-view">1/3</div>
    </div>
    <div class="preview-item">
        <div class="preview-item-body">這是第二頁</div>
        <div class="page-view">2/3</div>
    </div>
    <div class="preview-item">
        <div class="preview-item-body">這是第三頁</div>
        <div class="page-view">3/3</div>
    </div>
</div>

 

  

4、C#代碼實現(核心是Arguments的設置)

        /// <summary>
        /// HTML生成PDF
        /// </summary>
        /// <param name="url">url地址(需要包含HTTP://)</param>
        /// <param name="path">PDF存放路徑(可以是aaa.pdf,也可以用路徑,只能是絕對地址,如:D://aaa.pdf)</param>
        public static bool HtmlToPdf(string url, string path)
        {
            path = HttpContext.Current.Server.MapPath(path);string cookie = "cookieKey cookieValue";//改為為你自己的
            string Arguments = "-q  -B 0 -L 0 -R 0 -T 0 -s A4 --no-background --disable-smart-shrinking --cookie " + cookie + " " + url + " " + path; //參數可以根據自己的需要進行修改

            try
            {
                if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path))
                    return false;
                var p = new Process();
                string str = HttpContext.Current.Server.MapPath("/htmlToPDF/wkhtmltopdf.exe");
                if (!File.Exists(str))
                    return false;
                p.StartInfo.FileName = str;
                p.StartInfo.Arguments = Arguments;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = false;
                p.Start();
                p.WaitForExit();
                System.Threading.Thread.Sleep(1000);

                return true;
            }
            catch (Exception ex)
            {
                LogHelper.WriteError(ex);
            }
            return false;
        }

 

 

方法的調用:

            string url = Request.Url.AbsoluteUri.Replace("DownloadPDF", "Detail");//DownloadPDF是下載頁面,Detail是上面的HTML頁面
            string pdfDirectory = "/Data/PDF/";
            if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(pdfDirectory)))
            {
                System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(pdfDirectory));
            }
            string path = pdfDirectory + Guid.NewGuid() + ".pdf";
            HtmlToPdf(url, path);

            if (!System.IO.File.Exists(Utils.GetMapPath(path)))//如果生成失敗,重試一次
            {
                HtmlToPdfHelper.HtmlToPdf(url, path);
            }

            if (!System.IO.File.Exists(Utils.GetMapPath(path)))//如果生成失敗,重試一次
            {
                HtmlToPdfHelper.HtmlToPdf(url, path);
            }    

  

5、ok,采坑結束~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM