狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

[點(diǎn)晴永久免費(fèi)OA]C#將頁(yè)面生成pdf文件(CSS樣式不缺失)

admin
2022年12月22日 16:18 本文熱度 1933

將html文本或者h(yuǎn)tml網(wǎng)頁(yè)生成pdf,我常用的2種方式:TuesPechkin 或 wkhtmltopdf。

一、使用TuesPechkin轉(zhuǎn)pdf

首先引用TuesPechkin.dll,我使用的是1.0.3,也可以使用最新穩(wěn)定版

     /// <summary>
        /// 將網(wǎng)頁(yè)轉(zhuǎn)為pdf文件
        /// </summary>
        /// <param name="filePath">文件路徑/目錄</param>
        /// <param name="fileName">文件名</param>
        /// <param name="url">網(wǎng)頁(yè)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網(wǎng)頁(yè)內(nèi)容的寬高
                double width = 100; double.TryParse(txt_width.Text, out width);
                double height = 120; double.TryParse(txt_height.Text, out height);
                //設(shè)置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);
                //設(shè)置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轉(zhuǎn)pdf

wkhtmltopdf是一個(gè)插件,首先要下載https://wkhtmltopdf.org/downloads.html

下載后有使用說(shuō)明:需要使用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也能完美支持。

都是通過(guò)請(qǐng)求網(wǎng)頁(yè)url,將整個(gè)網(wǎng)頁(yè)生成pdf。


該文章在 2022/12/22 16:18:33 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved