如何在 C++ 中將 HTML 轉換為 PDF
在許多應用程式中,將 HTML 檔案或內容轉換為 PDF 頁面是一項非常有價值的功能。 在 C++ 中,從頭開始建立一個用於產生 HTML 轉 PDF 格式檔案的應用程式可能非常繁瑣。 在本文中,我們將探討如何使用 wkhtmltopdf 函式庫在 C++ 中將 HTML 轉換為 PDF。
WKHTMLTOPDF庫
wkhtmltopdf是一個開源命令列工具,可將 HTML 純文字頁面無縫轉換為高品質的 PDF 文件。 利用 C++ 程式中的對應功能,我們可以輕鬆地將 HTML 字串內容轉換為 PDF 格式。 讓我們深入了解使用 wkhtmltopdf 庫在 C++ 中將 HTML 頁面轉換為 PDF 的逐步過程。
先決條件
若要在 C++ 中建立 HTML 到 PDF 檔案轉換器,請確保符合以下先決條件:
- 您的系統上已安裝 C++ 編譯器,例如 GCC 或 Clang。
- 已安裝wkhtmltopdf 庫。 您可以從wkhtmltopdf 官方網站下載最新版本,並按照您的作業系統說明進行安裝。
- 具備 C++ 程式設計基礎。
在 Code::Blocks 中建立一個 C++ HtmltoPdf 專案。
若要在 Code::Blocks 中建立 C++ PDF 轉換項目,請依照下列步驟操作:
- 開啟 Code::Blocks IDE。
- 前往"檔案"選單,選擇"新建",然後選擇"專案"以開啟"新建專案"精靈。
- 在"新建專案"精靈中,選擇"控制台應用程式"。
- 選擇 C++ 語言。
- 設定項目標題和儲存位置。 點選"下一步"繼續。
- 選擇適當的 C++ 編譯器和建置目標,例如 Debug 或 Release。 點擊"完成"創建項目。
設定搜尋目錄
為了確保 Code::Blocks 能夠找到必要的頭文件,我們需要設定搜尋目錄:
- 點選選單列中的"項目"選單,然後選擇"建置選項"。 請務必選擇"調試"。
- 在"建置選項"對話方塊中,選擇"搜尋目錄"標籤。
- 在"編譯器"標籤下,按一下"新增"按鈕。
- 瀏覽至wkhtmltox頭檔所在的目錄(例如, C:\Program Files\wkhtmltopdf\include ),然後選擇它。
- 最後,按一下"確定"關閉對話框。
圖書館連結
要連結到wkhtmltox庫,請按照以下步驟操作:
- 再次點選選單列中的"項目"選單,然後選擇"建置選項"。 請務必選擇"調試"。
- 在"產生選項"對話方塊中,選擇"連結器設定"標籤。
- 在"連結庫"標籤下,按一下"新增"按鈕。
- 瀏覽至wkhtmltox庫檔案所在的目錄(例如, C:\Program Files\wkhtmltopdf\lib ),然後選擇對應的庫檔案。
- 點擊"開啟"將庫新增到您的專案中。
- 最後,按一下"確定"關閉對話方塊。
如何在 C++ 中將 HTML 轉換為 PDF:圖 2 - 連結庫
如何在 C++ 中輕鬆將 HTML 轉換為 PDF
步驟 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>步驟 2:初始化轉換器
要將 HTML 轉換為 PDF,我們需要初始化 wkhtmltopdf 轉換器。 代碼如下:
// Initialize the wkhtmltopdf library
wkhtmltopdf_init(false);
// Create global settings object
wkhtmltopdf_global_settings* gs = wkhtmltopdf_create_global_settings();
// Create object settings object
wkhtmltopdf_object_settings* os = wkhtmltopdf_create_object_settings();
// Create the PDF converter with global settings
wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(gs);// Initialize the wkhtmltopdf library
wkhtmltopdf_init(false);
// Create global settings object
wkhtmltopdf_global_settings* gs = wkhtmltopdf_create_global_settings();
// Create object settings object
wkhtmltopdf_object_settings* os = wkhtmltopdf_create_object_settings();
// Create the PDF converter with global settings
wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(gs);步驟 3:設定 HTML 內容
現在,讓我們提供需要轉換為 PDF 的 HTML 內容。 您可以載入 HTML 文件,也可以直接提供字串。
std::string htmlString = "<html><body><h1>Hello, World!</h1></body></html>";
// Add the HTML content to the converter
wkhtmltopdf_add_object(converter, os, htmlString.c_str());std::string htmlString = "<html><body><h1>Hello, World!</h1></body></html>";
// Add the HTML content to the converter
wkhtmltopdf_add_object(converter, os, htmlString.c_str());步驟 4:將 HTML 轉換為 PDF
轉換器和 HTML 內容準備好後,我們就可以開始將 HTML 轉換為 PDF 檔案了。請使用以下程式碼片段:
// Perform the actual conversion
if (!wkhtmltopdf_convert(converter)) {
std::cerr << "Conversion failed!" << std::endl;
}// Perform the actual conversion
if (!wkhtmltopdf_convert(converter)) {
std::cerr << "Conversion failed!" << std::endl;
}步驟 5:將輸出獲取為記憶體緩衝區
透過wkhtmltopdf_get_output函數,我們可以將現有的 PDF 資料作為記憶體緩衝區流來取得。 它也會傳回 PDF 的長度。 以下範例將執行此任務:
// Retrieve the PDF data in memory buffer
const unsigned char* pdfData;
int pdfLength = wkhtmltopdf_get_output(converter, &pdfData);// Retrieve the PDF data in memory buffer
const unsigned char* pdfData;
int pdfLength = wkhtmltopdf_get_output(converter, &pdfData);步驟 6:儲存 PDF 文件
轉換完成後,我們需要將產生的 PDF 檔案儲存到磁碟。 指定要儲存 PDF 檔案的檔案路徑。 然後使用輸出文件流,以二進位模式開啟該文件,並將 pdfData 寫入其中。 最後,關閉文件:
const char* outputPath = "file.pdf";
std::ofstream outputFile(outputPath, std::ios::binary);
// Write the PDF data to the file
outputFile.write(reinterpret_cast<const char*>(pdfData), pdfLength);
outputFile.close();const char* outputPath = "file.pdf";
std::ofstream outputFile(outputPath, std::ios::binary);
// Write the PDF data to the file
outputFile.write(reinterpret_cast<const char*>(pdfData), pdfLength);
outputFile.close();步驟 7:清理
將 HTML 轉換為 PDF 後,必須清理 wkhtmltopdf 指派的資源:
// Clean up the converter and settings
wkhtmltopdf_destroy_converter(converter);
wkhtmltopdf_destroy_object_settings(os);
wkhtmltopdf_destroy_global_settings(gs);
// Deinitialize the wkhtmltopdf library
wkhtmltopdf_deinit();
std::cout << "PDF saved successfully." << std::endl;// Clean up the converter and settings
wkhtmltopdf_destroy_converter(converter);
wkhtmltopdf_destroy_object_settings(os);
wkhtmltopdf_destroy_global_settings(gs);
// Deinitialize the wkhtmltopdf library
wkhtmltopdf_deinit();
std::cout << "PDF saved successfully." << std::endl;步驟 8:執行程式碼並產生 PDF 文件
現在,建立專案並使用 F9 執行程式碼。輸出結果將產生並保存在專案資料夾中。 產生的PDF文件如下:
使用 C# 將 HTML 文件轉換為 PDF 文件
IronPDF。
IronPDF HTML 到 PDF 轉換庫是一個強大的 .NET 和 .NET Core C# 程式庫,它允許開發人員輕鬆地從 HTML 內容產生 PDF 文件。 它提供了一個簡單直覺的 API,簡化了將 HTML 網頁轉換為 PDF 的過程,使其成為各種應用程式和用例的熱門選擇。
IronPDF 的主要優勢之一是其多功能性。 它不僅支援轉換簡單的 HTML 文件,還支援轉換帶有 CSS 樣式、JavaScript 互動甚至動態內容的複雜網頁。 此外,您還可以開發不同的 PDF 轉換器,並快速存取其轉換方法。
以下是使用 IronPDF 在 C# 中將 HTML 字串轉換為 PDF 的程式碼範例:
using IronPdf;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create PDF content 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 PDF content from an HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
// Export to a file or Stream
pdf.SaveAs("output.pdf");PDF 輸出結果:
如何在 C++ 中將 HTML 轉換為 PDF:圖 4 - IronPDF 輸出
有關如何將不同的 HTML 文件、網頁 URL 和圖像轉換為 PDF 的更多詳細信息,請訪問此HTML 轉 PDF 程式碼範例。
使用 IronPDF,在 .NET Framework 語言中,從 HTML 內容產生 PDF 檔案變得非常簡單。 它直覺的 API 和豐富的功能集使其成為需要在 C# 專案中將HTML 轉換為 PDF 的開發人員的寶貴工具。 無論是產生報告、發票,或是任何其他需要精確的 HTML 轉 PDF 的文檔,IronPDF 都是一個可靠且有效率的解決方案。
IronPDF 可免費用於開發用途,但用於商業用途則需要獲得許可。 它還提供IronPDF 全部功能的免費試用版,供商業用途測試其全部功能。 您可以從IronPDF下載該軟體。






