欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#通過chrome插件將HTML網(wǎng)頁轉(zhuǎn)換為PDF

 更新時間:2025年03月12日 17:08:22   作者:Eiceblue  
這篇文章主要介紹了C#通過chrome插件將HTML網(wǎng)頁轉(zhuǎn)換為PDF,將HTML網(wǎng)頁內(nèi)容轉(zhuǎn)換為 PDF 格式能方便文檔的后續(xù)打印、存檔和分享等,文中有相關(guān)的代碼示例供大家參考,需要的朋友可以參考下

將HTML網(wǎng)頁內(nèi)容轉(zhuǎn)換為 PDF 格式能方便文檔的后續(xù)打印、存檔和分享等。之前介紹過如果通過QT插件將HTML轉(zhuǎn)為PDF文件,本文將介紹另一個新的轉(zhuǎn)換方法,通過谷歌瀏覽器Chrome插件將HTML網(wǎng)頁轉(zhuǎn)PDF文件。

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)文章

最新評論