日韩欧美国产精品免费一二-日韩欧美国产精品亚洲二区-日韩欧美国产精品专区-日韩欧美国产另-日韩欧美国产免费看-日韩欧美国产免费看清风阁

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

如何在 C# 中使用 wkhtmltopdf 實現(xiàn) HTML 轉(zhuǎn)換成 PDF 或圖像

admin
2024年11月17日 21:48 本文熱度 1017

前言

HTML 是一種標(biāo)記語言,而每個 Web 網(wǎng)頁都是一個 HTML 文件。當(dāng)需要將HTML文件轉(zhuǎn)為PDF或圖片文件時,可以通過什么方法實現(xiàn)呢?又如何通過編程方式將HTMLPDF或圖片文件。本文將介紹 wkhtmltopdf 在 .NET 中的 C# 實現(xiàn)。

wkhtmltopdf

1、概述

wkhtmltopdf 是一個開源免費命令行工具,它使用 Webkit 將 HTML 轉(zhuǎn)換為 PDF 和圖像。由于其是使用命令行交互,所以需要通過編寫調(diào)用外部命令的方實現(xiàn)。

2、附錄

//Github 地址https://github.com/wkhtmltopdf/wkhtmltopdf// 官方網(wǎng)站下載 根據(jù)系統(tǒng)下載對應(yīng)版本https://wkhtmltopdf.org/downloads.html

3、文件

// 需將下面的文件放我們程序目錄下wkhtmltoimage.exe -- 轉(zhuǎn)圖片wkhtmltopdf.exe --轉(zhuǎn)PDFwkhtmltox.dll

實現(xiàn)

1、定義轉(zhuǎn)換接口

namespace Fountain.WinConsole.ToPDFOrImageDemo{    public interface IConverterEngine    {        /// <summary>        /// wkhtmltopdf 工具路徑        /// </summary>        string ConverterPath { get; }        /// <summary>        /// 轉(zhuǎn)換類型        /// </summary>        int EngineType { get; }        /// <summary>        /// 轉(zhuǎn)換        /// </summary>        /// <param name="htmlPath">HTML文件路徑</param>        /// <param name="outputPath">輸出文件路徑</param>        /// <returns></returns>        bool Convert(string htmlPath, string outputPath);    }}

2、實現(xiàn) PDF 轉(zhuǎn)換

using System.Diagnostics;
namespace Fountain.WinConsole.ToPDFOrImageDemo{    public class ConverterPDF:IConverterEngine    {        /// <summary>        /// wkhtmltopdf 工具路徑        /// </summary>        public string ConverterPath { get; }        /// <summary>        /// 轉(zhuǎn)換類型        /// </summary>        public int EngineType { get; } = 1;        /// <summary>        ///        /// </summary>        /// <param name="converterPath"></param>        public ConverterPDF(string converterPath)        {            ConverterPath = converterPath;        }        /// <summary>        ///        /// </summary>        /// <param name="htmlPath"></param>        /// <param name="outputPath"></param>        /// <returns></returns>        public bool Convert(string htmlPath, string outputPath)        {            try            {                var ticks = DateTime.UtcNow.Ticks;                string optionSwitches = "";
               #region 頁眉                // 在頁眉的居中部分顯示頁眉文本                optionSwitches += "--header-center 輸出文件 ";                // 在頁眉下方顯示一條直線分隔正文                optionSwitches += "--header-line ";                // 頁眉與正文之間的距離(默認(rèn)為零)                optionSwitches += "--header-spacing 1 ";                #endregion
               #region 頁面                // 使用的打印介質(zhì)類型,而不是屏幕                optionSwitches += "--print-media-type ";                // 邊距                optionSwitches += "--margin-top 40mm --margin-bottom 10mm --margin-right 10mm --margin-left 10mm ";                // 紙張大小                optionSwitches += "--page-size A4 ";                #endregion
               #region 頁腳                // 在頁腳上方顯示一條直線分隔正文                optionSwitches += "--footer-line ";                // 在頁腳的居中部分顯示頁腳文本                optionSwitches += "--footer-center \"[page] of [topage]\" ";                #endregion
               Process process = new Process();                process.StartInfo.UseShellExecute = true;                process.StartInfo.FileName = this.ConverterPath;                process.StartInfo.Arguments = $"{optionSwitches} \"{htmlPath}\" \"{outputPath}\" ";                process.Start();            }            catch (Exception ex)            {                throw new Exception("轉(zhuǎn)PDF出錯", ex);            }            return true;        }    }}

3、實現(xiàn)圖片轉(zhuǎn)換

using System.Diagnostics;
namespace Fountain.WinConsole.ToPDFOrImageDemo{    public class ConverterImage:IConverterEngine    {        /// <summary>        /// wkhtmltopdf 工具路徑        /// </summary>        public string ConverterPath { get; }        /// <summary>        /// 轉(zhuǎn)換類型        /// </summary>        public int EngineType { get; } = 2;        /// <summary>        ///        /// </summary>        /// <param name="converterPath"></param>        public ConverterImage(string converterPath)        {            ConverterPath = converterPath;        }        /// <summary>        ///        /// </summary>        /// <param name="htmlPath"></param>        /// <param name="outputPath"></param>        /// <returns></returns>        public bool Convert(string htmlPath, string outputPath)        {            try            {                Process process = new Process();                process.StartInfo.UseShellExecute = true;                process.StartInfo.FileName = this.ConverterPath;                process.StartInfo.Arguments = $"\"{htmlPath}\" \"{outputPath}\" ";                process.Start();            }            catch (Exception ex)            {                throw new Exception("轉(zhuǎn)換圖片出錯", ex);            }            return true;        }    }}

4、轉(zhuǎn)換調(diào)用

namespace Fountain.WinConsole.ToPDFOrImageDemo{    internal class Program    {        static void Main(string[] args)        {
           var ticks = DateTime.UtcNow.Ticks;
           string outputpdf = $"{AppDomain.CurrentDomain.BaseDirectory}{ticks}.pdf";            string htmlPath = $"{AppDomain.CurrentDomain.BaseDirectory}test.html";            string convertPath= $"{AppDomain.CurrentDomain.BaseDirectory}wkhtmltopdf.exe";
           ConverterPDF converter = new ConverterPDF(convertPath);            converter?.Convert(htmlPath, outputpdf);            Thread.Sleep(1000);
           string outputpng = $"{AppDomain.CurrentDomain.BaseDirectory}{ticks}.png";            string convertImagePath = $"{AppDomain.CurrentDomain.BaseDirectory}wkhtmltoimage.exe";
           ConverterImage converterImage = new ConverterImage(convertImagePath);            converterImage.Convert(htmlPath, outputpng);            Console.ReadKey();        }    }}

5、HTML文件

<!DOCTYPE HTML><html>   <head>   <meta charset="gbk">   <title>測式文件</title>           </head>   <body>      <div id="sse">      <input id="url" size=200 value="ws://127.0.0.1:8080/service" /><button id="btn1" onclick="changewebsocket(this)" tt=1>打開連接</button><br>    <input id="msg" size=200 value='測試內(nèi)容'/>    <button onclick="sendmsg()">發(fā)送數(shù)據(jù)</button><br>    <textarea id="onmsg" rows="10" cols="30"></textarea>      </div>   </body></html>

小結(jié)

本文通過實現(xiàn)示例,描述了 wkhtmltopdf 在 C# 中的實現(xiàn)方式。通過示例,根據(jù)您的需求,修改對應(yīng)代碼使其適應(yīng)各種文檔格式。


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

主站蜘蛛池模板: 午夜亚洲福利在线老司机 | 中文字幕精品一区二区三区在线 | 国产区二区 | 午夜家庭影院 | 国产福利91精品在线观看 | 成年人网站 | 午夜国产在线一区二区三区 | 粗大挺进 | 亚洲国产精品一区二区九九 | 欧美日本到一区二区三区 | 黄三级高清在线播放 | 亚洲精品乱码电影在线观看 | 国产乱码精品 | 国产黄大片在线观看 | 91九色蝌蚪熟女 | 99夜夜夜精品一区二区 | 一区二区国产 | 免费精品99| 欧美午夜性刺激在线观看免费 | 一区二区三区精 | 国产剧情对白刺激在线 | 国产一区二区三区 | 国产午夜福利精品一区二区三区 | 日本一区二区三区四区公司 | 午夜男女爽爽爽免费播放 | 亚洲欧美中文v日韩v在线 | 日本一区视频在线播放 | 亚洲一区高清 | 91大神大战丝袜美女在线观看 | 精品夜恋影院亚洲欧洲 | 国产啪视频免费观看视频 | 成人福利在线视频免费观看 | 亚洲国产日韩a在线播放 | 亚洲午夜福利 | 国产中文字幕亚洲 | 日本人浓密bbw | 在线视频 | 蜜桃网址| 国产精品精品国产一区二区 | 亚洲日韩欧美一区二区三区在线 | 欧美性受xxxx黑人猛交免费 |