如何在 C++ 中建立 PDF 文件
PDF(便攜式文件格式)文件廣泛用於在不同平台上以一致的格式共用和保存文件。 在本文中,我們將探討如何使用 wkhtmltopdf 函式庫在 C++ 中產生 PDF 檔案。 這是一個開源的命令列工具,用於將 HTML 內容轉換為 PDF 文件。 通過利用該庫的強大功能,我們可以編程地從HTML內容創建C++應用程序中的PDF文檔。
WkhtmltoPdf 函式庫
wkhtmltopdf是一個開源的命令列工具和渲染引擎,可以將 HTML 網頁或 HTML 內容轉換為 PDF 文件。 它利用 WebKit 渲染引擎解析和渲染 HTML、CSS 和 JavaScript,然後根據渲染的內容產生 PDF 檔案。
" wkhtmltopdf C++ 函式庫"(也稱為"wkhtmltopdf C API")。 該函式庫為wkhtmltopdf命令列工具提供了一個 C++ 接口,讓您可以直接在 C++ 應用程式中從 HTML 內容建立 PDF 檔案。 讓我們深入了解使用 Wkhtmltopdf 函式庫在 C++ 中將 HTML 轉換為 PDF 的逐步過程。
先決條件
要在 C++ 中建立 PDF 文件,我們需要以下工具:
- 您的系統上已安裝 C++ 編譯器,例如 GCC 或 Clang。 您可以使用任何支援 C++ 程式設計的整合開發環境 (IDE)。
- wkhtmltopdf 庫已下載並安裝。 您可以從wkhtmltopdf 官方網站下載最新版本,並按照您的作業系統說明進行安裝。
- 具備 C++ 程式設計基礎。
在 Code::Blocks 中建立 C++ PDF 項目
在 Code::Blocks 中建立一個項目,然後按照以下步驟連結庫資料夾和檔案。如果您使用的是其他 IDE,操作步驟可能有所不同,但您都需要在專案中引用下載的程式庫才能使其正常運作。
1. 在搜尋目錄中新增包含資料夾
為了確保 Code::Blocks 能夠找到必要的頭文件,我們需要設定搜尋目錄。
- 點選選單列中的"項目"選單,然後選擇"建置選項"。 請務必選擇"調試"。
- 在"建置選項"對話方塊中,選擇"搜尋目錄"標籤。
- 在"編譯器"標籤下,按一下"新增"按鈕。
- 瀏覽至wkhtmltox頭檔所在的目錄(例如, C:\Program Files\wkhtmltopdf\include ),然後選擇它。
- 最後,按一下"確定"關閉對話框。
2. 在連結器設定中連結庫
要連結到wkhtmltox庫,請按照以下步驟操作:
- 再次點選選單列中的"項目"選單,然後選擇"建置選項"。 請務必選擇"調試"。
- 在"建置選項"對話方塊中,選擇"連結器設定"標籤。
- 在"連結庫"標籤下,按一下"新增"按鈕。
- 瀏覽至wkhtmltox庫檔案所在的目錄(例如, C:\Program Files\wkhtmltopdf\lib ),然後選擇對應的庫檔案。
- 點擊"開啟"將庫新增到您的專案中。
- 最後,按一下"確定"關閉對話方塊。
使用 Wkhtmltopdf 在 C++ 中建立 PDF 的步驟
1. 包含必要的庫
首先,我們將包含必要的頭文件,以便在我們的 C++ 程式中使用 wkhtmltopdf 庫的功能。 在main.cpp原始碼檔案的開頭包含以下頭檔:
#include <iostream>
#include <fstream>
#include <string>
#include <wkhtmltox/pdf.h> // Include wkhtmltopdf library headers#include <iostream>
#include <fstream>
#include <string>
#include <wkhtmltox/pdf.h> // Include wkhtmltopdf library headers2. 停用 GUI 元件
如果您未使用圖形使用者介面 (GUI),則需要停用它以避免出錯。 代碼如下:
wkhtmltopdf_init(false); // Initialize the wkhtmltopdf library in non-GUI modewkhtmltopdf_init(false); // Initialize the wkhtmltopdf library in non-GUI mode3. 初始化設定和轉換器
接下來,我們需要初始化全域和物件設定指針,然後是wkhtmltopdf轉換器。
wkhtmltopdf_global_settings* gs = wkhtmltopdf_create_global_settings();
wkhtmltopdf_object_settings* os = wkhtmltopdf_create_object_settings();
wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(gs);wkhtmltopdf_global_settings* gs = wkhtmltopdf_create_global_settings();
wkhtmltopdf_object_settings* os = wkhtmltopdf_create_object_settings();
wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(gs);4. 新增 PDF 內容
現在,讓我們建立一個 HTML 字串,將一些內容填入我們新建立的 PDF 文件中。 代碼如下:
std::string htmlString = "<html><body><h1>Create a PDF in C++ using WKHTMLTOPDF Library</h1></body></html>";
wkhtmltopdf_add_object(converter, os, htmlString.c_str());std::string htmlString = "<html><body><h1>Create a PDF in C++ using WKHTMLTOPDF Library</h1></body></html>";
wkhtmltopdf_add_object(converter, os, htmlString.c_str());5. 將 HTML 字串轉換為 PDF 文件
Wkhtmltopdf 提供了一種將 HTML 內容轉換為 PDF 的轉換方法。 以下是程式碼片段:
wkhtmltopdf_convert(converter); // Converts HTML content to PDFwkhtmltopdf_convert(converter); // Converts HTML content to PDF6. 將輸出獲取為記憶體緩衝區
透過wkhtmltopdf_get_output函數,我們可以將 PDF 資料作為記憶體緩衝區流取得。 它也會傳回 PDF 的長度。 以下範例將執行此任務:
const unsigned char* pdfData;
int pdfLength = wkhtmltopdf_get_output(converter, &pdfData); // Get PDF data and lengthconst unsigned char* pdfData;
int pdfLength = wkhtmltopdf_get_output(converter, &pdfData); // Get PDF data and length7. 儲存PDF文件
pdfData指標現在將包含來自wkhtmltopdf_converter的字元流。 要儲存為 PDF 文件,首先,我們需要指定要儲存 PDF 文件的文件路徑。 然後,使用輸出檔案流,我們以二進位模式開啟文件,並使用reinterpret_cast函數進行類型轉換,將pdfData寫入該檔案。 最後,我們關閉文件。
const char* outputPath = "file.pdf";
std::ofstream outputFile(outputPath, std::ios::binary); // Open file in binary mode
outputFile.write(reinterpret_cast<const char*>(pdfData), pdfLength); // Write PDF data
outputFile.close(); // Close the fileconst char* outputPath = "file.pdf";
std::ofstream outputFile(outputPath, std::ios::binary); // Open file in binary mode
outputFile.write(reinterpret_cast<const char*>(pdfData), pdfLength); // Write PDF data
outputFile.close(); // Close the file8. 釋放記憶體
成功建立 PDF 檔案後,必須清理 Wkhtmltopdf 分配的資源,否則會導致記憶體洩漏。
wkhtmltopdf_destroy_converter(converter); // Destroy the converter
wkhtmltopdf_destroy_object_settings(os); // Destroy object settings
wkhtmltopdf_destroy_global_settings(gs); // Destroy global settings
wkhtmltopdf_deinit(); // Deinitialize the library
std::cout << "PDF created successfully." << std::endl; // Inform the userwkhtmltopdf_destroy_converter(converter); // Destroy the converter
wkhtmltopdf_destroy_object_settings(os); // Destroy object settings
wkhtmltopdf_destroy_global_settings(gs); // Destroy global settings
wkhtmltopdf_deinit(); // Deinitialize the library
std::cout << "PDF created successfully." << std::endl; // Inform the user9. 執行程式碼並產生 PDF 文件
現在,建立專案並使用 F9 執行程式碼。輸出結果將產生並保存在專案資料夾中。 產生的PDF文件如下:
使用 C# 建立 PDF 文件
IronPDF。
IronPDF 是一個 .NET C# PDF 文件庫,它允許開發人員輕鬆地從 HTML 建立 PDF 文件。 它提供了一個直覺的 API,簡化了從 HTML 建立 PDF 檔案的過程。
IronPDF 在處理 PDF 文件方面功能強大且用途廣泛。 它不僅可以從簡單的 HTML 文件建立 PDF,還可以從包含 CSS 樣式、JavaScript 互動甚至動態內容的複雜網頁建立 PDF。 此外,您還可以開發不同的 PDF 轉換器,並快速存取其轉換方法。
以下是使用 IronPDF透過HTML 字串轉 PDF建立 PDF 的範例程式碼:
using IronPdf;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from an 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 a PDF from an HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
// Export to a file or Stream
pdf.SaveAs("output.pdf");我們建立ChromePdfRenderer實例,將 HTML 內容渲染為 PDF。 我們呼叫renderer物件的RenderHtmlAsPdf方法,並傳入 HTML 字串。 這樣就產生了 PDF 文檔,然後我們使用SaveAs方法來儲存 PDF 文檔。
有關如何使用 IronPDF 從各種資源創建 PDF 的更多詳細信息,請訪問此IronPDF 程式碼範例頁面。
結論
在本文中,我們探討如何使用wkhtmltopdf C++ 函式庫在 C++ 中產生 PDF,並學習如何使用IronPDF在 C# 中產生 PDF 文件。
使用 IronPDF,在 .NET Framework 語言中,從 HTML 內容產生 PDF 檔案變得非常簡單。 它豐富的功能使其成為開發人員在 C# 專案中將HTML 轉換為 PDF 的寶貴工具。 無論是產生報告、發票,或是任何其他需要精確的 HTML 轉 PDF 的文檔,IronPDF 都是一個可靠且有效率的解決方案。
IronPDF 可免費用於開發用途,但用於商業用途則需要獲得許可。 它還提供免費的商業試用版,供用戶測試其全部功能。 下載 IronPDF並親自體驗。






