wkhtmltopdf converts a url page to pdf

To share a tool wkhtmltopdf that quickly converts url pages into pdf files,
we need to answer three questions

What is wkhtmltopdf?

Official website address: https://wkhtmltopdf.org/
According to the description on the official website, wkhtmltopdf is an open source (LGPLv3) command line tool that can use the Qt WebKit rendering engine to render HTML to PDF . They operate completely "headless" and do not require display or display services.

What is the use?

You can render HTML to pdf or convert the html page pointed to by url to pdf

how to use?

1. Install wkhtmltopdf
  1. Download from the official website and install it. I won't explain too much here. I installed it on the D drive.
  2. Configure environment variables: add after the environment variable "Path": ;D:\wkhtmltopdf\bin is the directory you installed. Restart the computer after installation.
  3. Test effect: open cmd. Input: wkhtmltopdf http://www.baidu.com/ D:website1.pdf Test whether the pdf of Baidu content appears in the D drive
2. C# operation wkhtmltopdf to convert a url page to pdf

Software: vs2015
Operating environment: win10
Let's look at the code directly:

        /// <summary>
        /// wkhtmltopdf 将url页面转为pdf文件
        /// </summary>
        /// <param name="htmlPath">url路径</param>
        /// <param name="savePath">pdf文件保存的地址</param>
        /// <returns></returns>
        public static bool HtmlConvertToPdf(string htmlPath, string savePath)
        {
    
    
            bool flag = false;
            ///这个路径为程序集的目录,因为我把应用程序 wkhtmltopdf.exe 放在了程序集同一个目录下
            string exePath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "wkhtmltopdf.exe";
            if (!File.Exists(exePath))
            {
    
    
                throw new Exception("No application wkhtmltopdf.exe was found.");
            }

            try
            {
    
    
                ProcessStartInfo processStartInfo = new ProcessStartInfo();
                processStartInfo.FileName = exePath;//应用程序的位置
                processStartInfo.WorkingDirectory = Path.GetDirectoryName(exePath);
                processStartInfo.UseShellExecute = false;
                processStartInfo.CreateNoWindow = true;
                processStartInfo.RedirectStandardInput = true;
                processStartInfo.RedirectStandardOutput = true;
                processStartInfo.RedirectStandardError = true;
                processStartInfo.Arguments = GetArguments(htmlPath, savePath);//关键:要执行的命令行参数

                Process process = new Process();
                process.StartInfo = processStartInfo;
                process.Start();
                process.WaitForExit();

                //用于查看是否返回错误信息
                StreamReader srone = process.StandardError;
                StreamReader srtwo = process.StandardOutput;
                string ss1 = srone.ReadToEnd();
                string ss2 = srtwo.ReadToEnd();
                srone.Close();
                srone.Dispose();
                srtwo.Close();
                srtwo.Dispose();

                process.Close();
                process.Dispose();

                flag = true;
            }
            catch(Exception ex)
            {
    
    
                Console.WriteLine(ex.Message);
                flag = false;
            }
            return flag;

            
        }
        /// <summary>
        /// 获取命令行参数
        /// </summary>
        /// <param name="htmlPath"></param>
        /// <param name="savePath"></param>
        /// <returns></returns>
        private static string GetArguments(string htmlPath, string savePath)
        {
    
    
            if (string.IsNullOrEmpty(htmlPath))
            {
    
    
                throw new Exception("HTML local path or network address can not be empty.");
            }

            if (string.IsNullOrEmpty(savePath))
            {
    
    
                throw new Exception("The path saved by the PDF document can not be empty.");
            }

            StringBuilder stringBuilder = new StringBuilder();
            //stringBuilder.Append(" --page-height 100 ");        //页面高度100mm
            //stringBuilder.Append(" --page-width 100 ");         //页面宽度100mm
            stringBuilder.Append(" --header-center 我是页眉 ");  //设置居中显示页眉
            stringBuilder.Append(" --header-line ");         //页眉和内容之间显示一条直线
            stringBuilder.Append(" --footer-center \"Page [page] of [topage]\" ");    //设置居中显示页脚
            stringBuilder.Append(" --footer-line ");       //页脚和内容之间显示一条直线
            //stringBuilder.Append("  --javascript-delay 20000  ");//增加延迟20秒,用于等待页面js加载完成
            stringBuilder.Append(" " + htmlPath + " ");       //本地 HTML 的文件路径或网页 HTML 的URL地址
            stringBuilder.Append(" " + savePath + " ");       //生成的 PDF 文档的保存路径
                                                              //stringBuilder.Append(" --javascript-delay <msec> 2000 ");
            return stringBuilder.ToString();
        }

[Reference document]
Full content reference: https://www.cnblogs.com/xiangxiong/p/7645679.html
About the pits encountered in using wkhtml and their solutions:
https://blog.csdn.net/qq_39541254 /article/details/107541497

Guess you like

Origin blog.csdn.net/qq_39541254/article/details/107689561