在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
PDF(可攜式文件格式)檔案廣泛用於在不同平台間以一致的格式共享和保存文件。 在本文中,我們將探索如何使用 wkhtmltopdf 庫在 C++ 中生成 PDF 文件。 這是一個開源命令行工具,用於將HTML內容轉換為PDF文件。 透過利用這個函式庫的威力,我們可以在 C++ 應用程式中以程式化方式從 HTML 內容建立 PDF 文件。
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 網站下載最新版本,並根據您的作業系統說明進行安裝。
在 Code::Blocks 中創建一個專案,然後按照以下步驟連結庫文件夾和文件。如果您使用的是其他 IDE,選項可能會有所不同,但您需要在專案中引用已下載的庫才能使其正常運行。
為了確保 Code::Blocks 能夠找到必要的標頭檔,我們需要設置搜尋目錄。
點擊菜單欄中的「專案」選單,然後選擇「構建選項」。 確保您選擇「Debug」。
在「建置選項」對話框中,選擇「搜尋目錄」標籤。
在「編譯器」標籤下,點擊「添加」按鈕。
瀏覽到wkhtmltox標頭檔案所在的目錄(例如,C:\Program Files\wkhtmltopdf\include),然後選擇它。
最後,點擊「確定」以關閉對話框。
要鏈接到 wkhtmltox 庫,請按照以下步驟操作:
再次點擊功能表列中的「專案」選單,然後選擇「建置選項」。 確保您選擇「Debug」。
在「構建選項」對話框中,選擇「鏈接器設置」標籤。
在「鏈接庫」選項卡下,點擊「添加」按鈕。
瀏覽到 wkhtmltox 庫檔案所在的目錄(例如,C:\Program Files\wkhtmltopdf\lib),然後選擇適當的庫檔案。
點擊「開啟」以將庫添加到您的專案中。
最後,點擊「確定」以關閉對話框。
首先,我們將包含必要的標頭檔,以在我們的C++程式中利用wkhtmltopdf庫的功能。 在 main.cpp 原始碼檔案的開頭包含以下標頭檔:
#include <iostream>
#include <fstream>
#include <string>
#include <wkhtmltox/pdf.h>
#include <iostream>
#include <fstream>
#include <string>
#include <wkhtmltox/pdf.h>
如果您不使用 GUI,則需要將其禁用以避免錯誤。 代碼如下:
wkhtmltopdf_init(false);
wkhtmltopdf_init(false);
接下來,我們需要初始化全域和物件設置指標,然後初始化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);
現在,讓我們建立一個 HTML 字串,以便在我們新建立的 PDF 文件中填入一些內容。 代碼如下:
string htmlString = "<html><body><h1>Create a PDF in C++ using WKHTMLTOPDF Library</h1></body></html>";
wkhtmltopdf_add_object(converter, os, htmlString.c_str());
string htmlString = "<html><body><h1>Create a PDF in C++ using WKHTMLTOPDF Library</h1></body></html>";
wkhtmltopdf_add_object(converter, os, htmlString.c_str());
Wkhtmltopdf 提供了一個轉換方法,將 HTML 內容轉換為 PDF。 以下是代碼片段:
wkhtmltopdf_convert(converter);
wkhtmltopdf_convert(converter);
使用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);
pdfData
指針現在將擁有來自 wkhtmltopdf_converter
的字符流。 要儲存為 PDF 檔案,首先我們將指定要儲存 PDF 的檔案路徑。 然後,使用輸出文件流,我們以二進制模式開啟文件,並通過使用reinterpret_cast
函數類型轉換將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();
在成功創建 PDF 文件後,必須清理 Wkhtmltopdf 分配的資源,否則將導致記憶體洩漏。
wkhtmltopdf_destroy_converter(converter);
wkhtmltopdf_destroy_object_settings(os);
wkhtmltopdf_destroy_global_settings(gs);
wkhtmltopdf_deinit();
cout << "PDF created successfully." << endl;
wkhtmltopdf_destroy_converter(converter);
wkhtmltopdf_destroy_object_settings(os);
wkhtmltopdf_destroy_global_settings(gs);
wkhtmltopdf_deinit();
cout << "PDF created successfully." << endl;
現在,建置專案並使用 F9 執行代碼。輸出將生成並保存在專案資料夾中。 生成的 PDF 如下:
IronPDF 是一個 .NET C# PDF 程式庫,讓開發人員可以輕鬆地從 HTML 創建 PDF 文件。 它提供了直覺的 API,簡化從 HTML 創建 PDF 文件的過程。
IronPDF 在處理 PDF 時既堅固又多功能。 它不僅能從簡單的HTML文件生成PDF,還能處理帶有CSS樣式、JavaScript互動和甚至動態內容的複雜網頁。 此外,您可以開發不同的 PDF 轉換器,並快速訪問其轉換方法。
這是一個使用HTML 字串轉 PDF以IronPDF 建立 PDF的範例程式碼:
using IronPdf;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF 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 a PDF from a 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++ Library 在 C++ 中生成 PDF,並學習了如何使用 IronPDF 在 C# 中生成 PDF 文件。
使用 IronPDF,在 .NET Framework 語言中從 HTML 內容生成 PDF 檔案成為一項簡單的任務。 其廣泛的功能集使其成為開發人員在其 C# 項目中需要將 HTML 轉換為 PDF 的寶貴工具。 無論是生成報告、發票,還是任何其他需要精確HTML轉PDF轉換的文件,IronPDF都是一個可靠且高效的解決方案。
IronPDF 在開發用途上是免費的,但用於商業用途則需要授權。 它還提供免費試用以供商業用途測試其完整功能。 下載 IronPDF 並親自試用。