LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

[点晴永久免费OA]C#将页面生成pdf文件(CSS样式不缺失)

admin
2022年12月22日 16:18 本文热度 1129

将html文本或者html网页生成pdf,我常用的2种方式:TuesPechkin 或 wkhtmltopdf。

一、使用TuesPechkin转pdf

首先引用TuesPechkin.dll,我使用的是1.0.3,也可以使用最新稳定版

     /// <summary>
        /// 将网页转为pdf文件
        /// </summary>
        /// <param name="filePath">文件路径/目录</param>
        /// <param name="fileName">文件名</param>
        /// <param name="url">网页url</param>
        public bool ConvertWebpageToPDF(string filePath, string fileName, string url)
        {
            if (Directory.Exists(filePath) == false)
                Directory.createDirectory(filePath);
            try
            {
                HtmlToPdfDocument htmlToPdfDocument = new HtmlToPdfDocument();
                GlobalSettings globalSettings = new GlobalSettings();
                globalSettings.ProduceOutline = new bool?(true);
                //html网页内容的宽高
                double width = 100; double.TryParse(txt_width.Text, out width);
                double height = 120; double.TryParse(txt_height.Text, out height);
                //设置pdf宽高
                double proportion = 1.26;
                width /= proportion;
                height = Math.Floor(height / proportion);
                string Width = width.ToString(); string Height = height.ToString();
                globalSettings.PaperSize = new PechkinPaperSize(Width, Height);
                //设置pdf文档四周空白边距
                globalSettings.Margins.Top = 0;
                globalSettings.Margins.Right = 0;
                globalSettings.Margins.Bottom = 0;
                globalSettings.Margins.Left = 0;
                globalSettings.Margins.Unit = Unit.Centimeters;
                string pageUrl = url;
                ObjectSettings objectSettings = new ObjectSettings();
                double value = 1;
                objectSettings.HtmlText = string.Empty;
                objectSettings.PageUrl = pageUrl;
                objectSettings.LoadSettings.BlockLocalFileAccess = true;
                objectSettings.LoadSettings.ZoomFactor = new double?(value);
                objectSettings.WebSettings.PrintMediaType = new bool?(true);
                objectSettings.WebSettings.PrintBackground = new bool?(false);
                htmlToPdfDocument.GlobalSettings = globalSettings;
                htmlToPdfDocument.Objects.Add(objectSettings);
                IPechkin pechkin = Factory.create();
                byte[] array = pechkin.Convert(htmlToPdfDocument);
                string path = string.Empty;
                if (array == null)
                {
                    return false;
                }
                path = Path.Combine(filePath, fileName);
                using (FileStream fileStream = new FileStream(path, FileMode.create))
                {
                    fileStream.Write(array, 0, array.Length);
                    fileStream.Flush();
                    fileStream.Close();
                    FileStream fs = new FileStream(path, FileMode.Open);
                    byte[] file = new byte[fs.Length];
                    fs.Read(file, 0, file.Length);
                    fs.Close();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }  


二、使用wkhtmltopdf转pdf

wkhtmltopdf是一个插件,首先要下载https://wkhtmltopdf.org/downloads.html

下载后有使用说明:需要使用cmd安装到本地目录

public void wkhtmltopdf(string filePath, string fileName, string url)
        {
            Process p = new Process();
            //wkhtmltopdf插件安装的地址
            string dllstr = AppDomain.CurrentDomain.BaseDirectory + "wkhtmltopdf\\wkhtmltopdf.exe";
            //string dllstr = "C:\\Windows\\System32\\wkhtmltopdf.exe";
            if (System.IO.File.Exists(dllstr))
            {
                string savepath = Path.Combine(filePath, fileName);
                p.StartInfo.FileName = dllstr;
                StringBuilder paramsBuilder = new StringBuilder();
                paramsBuilder.Append("--page-width " + txt_width.Text + "mm ");
                paramsBuilder.Append("--zoom 1.2 ");
                paramsBuilder.Append("--disable-smart-shrinking ");
                paramsBuilder.Append("--page-height " + txt_height.Text + "mm ");
                paramsBuilder.Append("--margin-bottom 0mm ");
                paramsBuilder.Append("--margin-left 0mm ");
                paramsBuilder.Append("--margin-right 0mm ");
                paramsBuilder.Append("--margin-top 0mm ");
                paramsBuilder.AppendFormat("\"{0}\" \"{1}\"", url, savepath);
                p.StartInfo.Arguments = paramsBuilder.ToString();
                //p.StartInfo.Arguments = " \"" + url + "\"  \"" + savepath + "\"";

                p.StartInfo.UseShellexecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.createNoWindow = true;
                p.Start();
                p.WaitForExit();
            }
        }

备注: 

以上2种方法生成pdf效果都非常好,css也能完美支持。

都是通过请求网页url,将整个网页生成pdf。


该文章在 2022/12/22 16:18:33 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved