PDF 工具

如何在 C++ 中將 HTML 轉換為 PDF

發佈 2023年7月1日
分享:

將 HTML 文件或內容轉換為 PDF 頁面格式的功能在許多應用中都是一個有價值的功能。在 C++ 中,從頭開始構建一個應用程式來生成 HTML 到 PDF 格式的文件是相當繁瑣的工作。因此,在本文中,我們將探討如何使用 wkhtmltopdf 庫在 C++ 中將 HTML 轉換為 PDF。

WK轉換為PDF(TOPdf)庫

wkhtmltopdf 是一個開源的命令行工具,可以無縫地將 HTML 純文本頁面轉換為高質量的 PDF 文件。通過在 C++ 程序中利用其功能,我們可以輕鬆地將 HTML 字符串內容轉換為 PDF 格式。讓我們深入了解使用 wkhtmltopdf 庫在 C++ 中進行 HTML 頁面到 PDF 轉換的分步過程。

先決條件

要在C++中創建HTML到PDF文件的轉換器,我們需要檢查以下幾點:

  1. 在您的系統上安裝一個C++編譯器,例如GCC或Clang。
  2. 安裝wkhtmltopdf庫。您可以從官方下載最新版本。 wkhtmltopdf 網站 並按照作業系統的指示進行安裝。

  3. 基本的 C++ 程式設計知識。

在 Code:Blocks 中建立 C++ HtmltoPdf 專案

要在 Code::Blocks 中建立一個 C++ PDF 轉換專案,請按照以下步驟操作:

  1. 打開 Code::Blocks IDE。

  2. 進入“文件”菜單,選擇“新建”,然後選擇“專案”以打開新專案嚮導。

  3. 在新專案嚮導中,選擇“控制台應用程式”。

  4. 選擇 C++ 語言。

  5. 設定專案標題和要保存的位置,然後點選“下一步”繼續。

  6. 選擇適當的 C++ 編譯器和構建目標,如 Debug 或 Release。點擊“完成”以建立專案。

設置搜尋目錄

為了確保 Code::Blocks 能找到必要的標頭文件,我們需要設置搜尋目錄。

  1. 點擊選單欄中的「Project」選單,並選擇「Build options」。確保選擇「Debug」。

  2. 在「Build options」對話方塊中,選擇「Search directories」標籤。

  3. 在「Compiler」標籤下,點擊「Add」按鈕。

  4. 瀏覽到 wkhtmltox 標頭文件所在的目錄。 (例如,C:\Program Files\wkhtmltopdf\include)並選取它。

  5. 最後,點擊「確定」以關閉對話框。

如何在 C++ 中將 HTML 轉換為 PDF:圖 1 - 搜尋目錄

連結庫

要連結 wkhtmltox 庫,請按照以下步驟操作:

  1. 再次點擊菜單欄中的「專案」菜單,然後選擇「建置選項」。確保您選擇「除錯」。

  2. 在「建置選項」對話框中,選擇「連接器設定」選項卡。

  3. 在「連結庫」選項卡下,點擊「新增」按鈕。

  4. 瀏覽到 wkhtmltox 庫文件所在的目錄。 (例如,C:\Program Files\wkhtmltopdf\lib),並選擇適當的庫檔案。

  5. 點擊“打開”將庫添加到您的項目中。

  6. 最後,點擊“確定”關閉對話框。

如何在 C++ 中將 HTML 轉換為 PDF:圖 2 - 連接庫

輕鬆將 HTML 轉換為 PDF 的 C++ 步驟

步驟 1 包含轉換 HTML 文件的函式庫

為了開始,請在您的 C++ 程式中包含所需的標頭檔以利用 wkhtmltopdf 函式庫的功能。在 main.cpp 源碼檔案的開頭如以下範例所示包含以下標頭檔:


    #include <iostream>
    #include <fstream>
    #include <string>
    #include <wkhtmltox/pdf.h>

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <wkhtmltox/pdf.h>
#include <iostream>
	#include <fstream>
	#include <string>
	#include <wkhtmltox/pdf.h>
VB   C#

第 2 步 初始化轉換器

為了將 HTML 轉換為 PDF,我們需要初始化 wkhtmltopdf 轉換器。代碼如下:


    wkhtmltopdf_init(false);

    wkhtmltopdf_global_settings* gs = wkhtmltopdf_create_global_settings();
    wkhtmltopdf_object_settings* os = wkhtmltopdf_create_object_settings();
    wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(gs);

    wkhtmltopdf_init(false);

    wkhtmltopdf_global_settings* gs = wkhtmltopdf_create_global_settings();
    wkhtmltopdf_object_settings* os = wkhtmltopdf_create_object_settings();
    wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(gs);
wkhtmltopdf_init(False)

	wkhtmltopdf_global_settings* gs = wkhtmltopdf_create_global_settings()
	wkhtmltopdf_object_settings* os = wkhtmltopdf_create_object_settings()
	wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(gs)
VB   C#

第三步 設定 HTML 內容

現在,我們來提供需要轉換成 PDF 的 HTML 內容。您可以載入 HTML 檔案或直接提供字串。


    string htmlString = "<html><body><h1>Hello, World!</h1></body></html>";   wkhtmltopdf_add_object(converter, os, htmlString.c_str());

    string htmlString = "<html><body><h1>Hello, World!</h1></body></html>";   wkhtmltopdf_add_object(converter, os, htmlString.c_str());
Dim htmlString As String = "<html><body><h1>Hello, World!</h1></body></html>"
wkhtmltopdf_add_object(converter, os, htmlString.c_str())
VB   C#

步驟 4 將 HTML 轉換為 PDF

準備好轉換器和 HTML 內容後,我們可以開始將 HTML 轉換為 PDF 文件。使用以下代碼片段:


    wkhtmltopdf_convert(converter);

    wkhtmltopdf_convert(converter);
wkhtmltopdf_convert(converter)
VB   C#

第五步 獲取作為內存緩衝區的輸出

使用 wkhtmltopdf_get_output 函數,我們可以將現有的 PDF 數據獲取為內存緩衝區流。它還會返回 PDF 的長度。以下範例將執行此任務:


    const unsigned char* pdfData;
    const int pdfLength = wkhtmltopdf_get_output(converter, &pdfData);

    const unsigned char* pdfData;
    const int pdfLength = wkhtmltopdf_get_output(converter, &pdfData);
const unsigned Char* pdfData
	Const pdfLength As Integer = wkhtmltopdf_get_output(converter, &pdfData)
VB   C#

第六步:保存 PDF 檔案

一旦轉換完成,我們需要將生成的 PDF 檔案保存到磁碟。請指定您要保存 PDF 的檔案路徑。然後使用輸出檔案流,在二進位模式下打開該檔案並將 pdfData 寫入其中。最後,關閉檔案。以下是代碼示例:


    const char* outputPath = "file.pdf";
    ofstream outputFile(outputPath, ios::binary);
    outputFile.write(reinterpret_cast<const char*>(pdfData), pdfLength);
    outputFile.close();

    const char* outputPath = "file.pdf";
    ofstream outputFile(outputPath, ios::binary);
    outputFile.write(reinterpret_cast<const char*>(pdfData), pdfLength);
    outputFile.close();
const Char* outputPath = "file.pdf"
	ofstream outputFile(outputPath, ios:=:=binary)
	outputFile.write(reinterpret_cast<const Char*>(pdfData), pdfLength)
	outputFile.close()
VB   C#

步驟 7 清理

在將 HTML 轉換為 PDF 之後,清理由 wkhtmltopdf 分配的資源是非常重要的。使用以下代碼片段:


    wkhtmltopdf_destroy_converter(converter);
    wkhtmltopdf_destroy_object_settings(os);
    wkhtmltopdf_destroy_global_settings(gs);
    wkhtmltopdf_deinit();

    cout << "PDF saved successfully." << endl;

    wkhtmltopdf_destroy_converter(converter);
    wkhtmltopdf_destroy_object_settings(os);
    wkhtmltopdf_destroy_global_settings(gs);
    wkhtmltopdf_deinit();

    cout << "PDF saved successfully." << endl;
wkhtmltopdf_destroy_converter(converter)
	wkhtmltopdf_destroy_object_settings(os)
	wkhtmltopdf_destroy_global_settings(gs)
	wkhtmltopdf_deinit()

	cout << "PDF saved successfully." << endl
VB   C#

第8步 執行代碼並生成 PDF 文件

現在,構建項目並使用 F9 執行代碼。輸出將在項目文件夾內生成並保存。生成的 PDF 如下所示:

如何在 C++ 中將 HTML 轉換為 PDF:圖 3 - PDF 輸出

將 HTML 檔案轉換為 PDF 檔案在 C# 中

IronPDF

IronPDF 是一個強大的 .NET 和 .NET Core C# 庫,使開發人員能夠輕鬆地從 HTML 內容生成 PDF 文件。它提供了一個簡單且直觀的 API,簡化了將 HTML 網頁轉換為 PDF 的過程,成為許多應用和使用案例的流行選擇。

IronPDF 的一個主要優勢在於其多功能性。它不僅支持簡單 HTML 文件的轉換,還支持具有 CSS 樣式、JavaScript 交互甚至動態內容的複雜網頁轉換。此外,您可以快速訪問其轉換方法以開發不同的 PDF 轉換器。

以下是將 HTML 轉換為 PDF 的程式碼示例: HTML字串轉PDF 在 C# 中使用 IronPDF:


    using IronPdf;

    // Instantiate Renderer
    var renderer = new ChromePdfRenderer();

    // Create PDF content from a HTML string using C#
    var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");

    // Export to a file or Stream
    pdf.SaveAs("output.pdf");

    using IronPdf;

    // Instantiate Renderer
    var renderer = new ChromePdfRenderer();

    // Create PDF content from a HTML string using C#
    var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");

    // Export to a file or Stream
    pdf.SaveAs("output.pdf");
Imports IronPdf

	' Instantiate Renderer
	Private renderer = New ChromePdfRenderer()

	' Create PDF content from a HTML string using C#
	Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")

	' Export to a file or Stream
	pdf.SaveAs("output.pdf")
VB   C#

PDF 輸出:

如何在 C++ 中將 HTML 轉換為 PDF:圖 4 - IronPDF 輸出

欲了解有關如何將不同的 HTML 檔案、Web 網頁 URL 及圖像轉換為 PDF 的更多詳細資訊,請訪問此 程式碼範例頁面使用 IronPDF,從 HTML 內容生成 PDF 檔案在 .NET Framework 語言中變得簡單易行。其直觀的 API 和豐富的功能集使其成為需要進行轉換的開發人員的寶貴工具。 HTML轉PDF 在他們的 C# 專案中。無論是生成報告、發票,或任何其他需要精確 HTML 至 PDF 轉換的文件,IronPDF 是一個可靠且高效的解決方案。

IronPDF 可免費用於開發用途,但用於商業用途則需要授權。它還提供了一個 免費試用 用於商業用途,以測試其完整功能。您可以從以下鏈接下載該軟件 這裡.

< 上一頁
如何在C++中讀取PDF檔案
下一個 >
如何使用 Puppeteer 在 Node.js 中將 HTML 轉換為 PDF

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 10,993,239 查看許可證 >