在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
將 HTML 文件或內容轉換為 PDF 頁面格式的功能在許多應用中都是一個有價值的功能。在 C++ 中,從頭開始構建一個應用程式來生成 HTML 到 PDF 格式的文件是相當繁瑣的工作。因此,在本文中,我們將探討如何使用 wkhtmltopdf 庫在 C++ 中將 HTML 轉換為 PDF。
wkhtmltopdf 是一個開源的命令行工具,可以無縫地將 HTML 純文本頁面轉換為高質量的 PDF 文件。通過在 C++ 程序中利用其功能,我們可以輕鬆地將 HTML 字符串內容轉換為 PDF 格式。讓我們深入了解使用 wkhtmltopdf 庫在 C++ 中進行 HTML 頁面到 PDF 轉換的分步過程。
要在C++中創建HTML到PDF文件的轉換器,我們需要檢查以下幾點:
安裝wkhtmltopdf庫。您可以從官方下載最新版本。 wkhtmltopdf 網站 並按照作業系統的指示進行安裝。
要在 Code::Blocks 中建立一個 C++ PDF 轉換專案,請按照以下步驟操作:
打開 Code::Blocks IDE。
進入“文件”菜單,選擇“新建”,然後選擇“專案”以打開新專案嚮導。
在新專案嚮導中,選擇“控制台應用程式”。
選擇 C++ 語言。
設定專案標題和要保存的位置,然後點選“下一步”繼續。
為了確保 Code::Blocks 能找到必要的標頭文件,我們需要設置搜尋目錄。
點擊選單欄中的「Project」選單,並選擇「Build options」。確保選擇「Debug」。
在「Build options」對話方塊中,選擇「Search directories」標籤。
在「Compiler」標籤下,點擊「Add」按鈕。
瀏覽到 wkhtmltox 標頭文件所在的目錄。 (例如,C:\Program Files\wkhtmltopdf\include)並選取它。
要連結 wkhtmltox 庫,請按照以下步驟操作:
再次點擊菜單欄中的「專案」菜單,然後選擇「建置選項」。確保您選擇「除錯」。
在「建置選項」對話框中,選擇「連接器設定」選項卡。
在「連結庫」選項卡下,點擊「新增」按鈕。
瀏覽到 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>
#include <iostream>
#include <fstream>
#include <string>
#include <wkhtmltox/pdf.h>
為了將 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)
現在,我們來提供需要轉換成 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())
準備好轉換器和 HTML 內容後,我們可以開始將 HTML 轉換為 PDF 文件。使用以下代碼片段:
wkhtmltopdf_convert(converter);
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);
const unsigned Char* pdfData
Const pdfLength As Integer = wkhtmltopdf_get_output(converter, &pdfData)
一旦轉換完成,我們需要將生成的 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()
在將 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
現在,構建項目並使用 F9 執行代碼。輸出將在項目文件夾內生成並保存。生成的 PDF 如下所示:
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")
PDF 輸出:
欲了解有關如何將不同的 HTML 檔案、Web 網頁 URL 及圖像轉換為 PDF 的更多詳細資訊,請訪問此 程式碼範例頁面使用 IronPDF,從 HTML 內容生成 PDF 檔案在 .NET Framework 語言中變得簡單易行。其直觀的 API 和豐富的功能集使其成為需要進行轉換的開發人員的寶貴工具。 HTML轉PDF 在他們的 C# 專案中。無論是生成報告、發票,或任何其他需要精確 HTML 至 PDF 轉換的文件,IronPDF 是一個可靠且高效的解決方案。
IronPDF 可免費用於開發用途,但用於商業用途則需要授權。它還提供了一個 免費試用 用於商業用途,以測試其完整功能。您可以從以下鏈接下載該軟件 這裡.