C#通過chrome插件將HTML網(wǎng)頁轉(zhuǎn)換為PDF
將HTML網(wǎng)頁內(nèi)容轉(zhuǎn)換為 PDF 格式能方便文檔的后續(xù)打印、存檔和分享等。之前介紹過如果通過QT插件將HTML轉(zhuǎn)為PDF文件,本文將介紹另一個新的轉(zhuǎn)換方法,通過谷歌瀏覽器Chrome插件將HTML網(wǎng)頁轉(zhuǎn)PDF文件。
首先需要安裝Spire.PDF for .NET 庫(10.7.21版本及以上)??梢酝ㄟ^此鏈接下載產(chǎn)品包后手動添加引用,或者直接通過NuGet安裝。
https://www.e-iceblue.cn/Downloads/Spire-PDF-NET.html此外還需要用到谷歌瀏覽器插件,請確保系統(tǒng)中安裝了chrome.exe。
C# 通過Chrome插件將HTML網(wǎng)頁轉(zhuǎn)換為PDF文件
Spire.PDF for .NET新增了 ChromeHtmlConverter.ConvertToPdf()
方法,支持使用 Chrome 瀏覽器插件將 HTML 網(wǎng)頁轉(zhuǎn)換為 PDF。該方法中的 3 個參數(shù)為:
string input
:輸入 HTML 文件路徑string output
:輸出 PDF 文件路徑ConvertOptions
:轉(zhuǎn)換設(shè)置,可自定義設(shè)置轉(zhuǎn)換超時、PDF 紙張大小和頁邊距等
示例代碼如下:
using Spire.Additions.Chrome; namespace ConvertHtmlToPdfUsingChrome { internal class Program { static void Main(string[] args) { // 指定輸入輸出文檔路徑 string inputUrl = @"https://www.e-iceblue.cn/about-us.html"; string outputFile = @"HtmlToPDF.pdf"; // 指定Chrome插件的路徑 string chromeLocation = @"C:\Program Files\Google\Chrome\Application\chrome.exe"; // 創(chuàng)建 ChromeHtmlConverter 對象 ChromeHtmlConverter converter = new ChromeHtmlConverter(chromeLocation); // 創(chuàng)建 ConvertOptions 對象 ConvertOptions options = new ConvertOptions(); // 設(shè)置轉(zhuǎn)換超時 options.Timeout = 10 * 3000; // 設(shè)置轉(zhuǎn)換后PDF頁面的紙張大小和頁邊距 options.PageSettings = new PageSettings() { PaperWidth = 8.27, PaperHeight = 11.69, MarginTop = 0, MarginLeft = 0, MarginRight = 0, MarginBottom = 0 }; // 將HTML網(wǎng)頁轉(zhuǎn)換為PDF converter.ConvertToPdf(inputUrl, outputFile, options); } } }
如果你想要在轉(zhuǎn)換過程中輸出日志,可以調(diào)用ChromeHtmlConverter.Logger
屬性。
示例代碼如下:
using Spire.Additions.Chrome; namespace ConvertHtmlToPdfUsingChrome { internal class Program { static void Main(string[] args) { //指定輸入輸出文檔路徑 string inputUrl = @"https://www.e-iceblue.cn/about-us.html"; string outputFile = @"HtmlToPDF.pdf"; // 指定日志文件路徑 string logFilePath = @"Logs.txt"; // 指定Chrome插件的路徑 string chromeLocation = @"C:\Program Files\Google\Chrome\Application\chrome.exe"; // 創(chuàng)建ChromeHtmlConverter對象 ChromeHtmlConverter converter = new ChromeHtmlConverter(chromeLocation); // 啟用日志記錄 converter.Logger = new Logger(logFilePath); // 創(chuàng)建ConvertOptions對象 ConvertOptions options = new ConvertOptions(); // 設(shè)置轉(zhuǎn)換超時 options.Timeout = 10 * 3000; // 設(shè)置轉(zhuǎn)換后PDF頁面的紙張大小和頁邊距 options.PageSettings = new PageSettings() { PaperWidth = 8.27, PaperHeight = 11.69, MarginTop = 0, MarginLeft = 0, MarginRight = 0, MarginBottom = 0 }; // 將HTML網(wǎng)頁轉(zhuǎn)換為PDF converter.ConvertToPdf(inputUrl, outputFile, options); } } }
到此這篇關(guān)于C#通過chrome插件將HTML網(wǎng)頁轉(zhuǎn)換為PDF的文章就介紹到這了,更多相關(guān)C# chrome插件將HTML轉(zhuǎn)PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#自動類型轉(zhuǎn)換與強制類型轉(zhuǎn)換的講解
今天小編就為大家分享一篇關(guān)于C#自動類型轉(zhuǎn)換與強制類型轉(zhuǎn)換的講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01WCF基礎(chǔ)介紹并創(chuàng)建簡單應(yīng)用程序
這篇文章介紹了WCF基礎(chǔ)并創(chuàng)建簡單WCF應(yīng)用程序,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01C#在復(fù)雜多線程環(huán)境下使用讀寫鎖同步寫入文件
這篇文章介紹了C#在復(fù)雜多線程環(huán)境下使用讀寫鎖同步寫入文件的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04C# TextBox 擴展方法數(shù)據(jù)驗證詳細說明
C# TextBox 擴展方法數(shù)據(jù)驗證詳細說明,需要的朋友可以參考一下2013-03-03C#遍歷得到checkboxlist選中值和設(shè)置選中項的代碼
這篇文章主要介紹了C#遍歷得到checkboxlist選中值和設(shè)置選中項的代碼,代碼簡單易懂,具有參考借鑒價值,需要的朋友可以參考下2016-08-08