PDF工具 如何在C++中創建PDF文件 Curtis Chau 更新日期:6月 22, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article PDF(可攜式文檔格式)文件廣泛用於在不同平台上以一致的格式共享和保存文檔。 在本文中,我們將探討如何在 C++ 中使用 wkhtmltopdf 庫生成 PDF 文件。 這是一個開源命令行工具,用於將 HTML 內容轉換為 PDF 文件。 通過利用此庫的強大功能,我們可以在 C++ 應用程序中以編程方式從 HTML 內容創建 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 headers C++ 2. 禁用 GUI 組件 如果您不使用 GUI,則需要禁用它以避免錯誤。 代碼如下: wkhtmltopdf_init(false); // Initialize the wkhtmltopdf library in non-GUI mode wkhtmltopdf_init(false); // Initialize the wkhtmltopdf library in non-GUI mode C++ 3. 初始化設置和轉換器 接下來,我們需要初始化全局和對象設置指針,然後初始化 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); C++ 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()); C++ 5. 將 HTML 字符串轉換為 PDF 文件 Wkhtmltopdf 提供了一個轉換器方法來將 HTML 內容轉換為 PDF。 下面是代碼片段: wkhtmltopdf_convert(converter); // Converts HTML content to PDF wkhtmltopdf_convert(converter); // Converts HTML content to PDF C++ 6. 獲取作為記憶體緩衝區的輸出 使用 wkhtmltopdf_get_output 函數,我們可以獲取作為記憶體緩衝區流的 PDF 數據。 它還返回 PDF 的長度。 以下示例將執行此任務: const unsigned char* pdfData; int pdfLength = wkhtmltopdf_get_output(converter, &pdfData); // Get PDF data and length const unsigned char* pdfData; int pdfLength = wkhtmltopdf_get_output(converter, &pdfData); // Get PDF data and length C++ 7. 保存 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 file 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 file C++ 8. 釋放內存 成功創建 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 user 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 user C++ 9. 執行代碼並生成 PDF 文件 現在,構建項目並使用 F9 執行代碼。輸出生成並保存在項目文件夾中。 生成的 PDF 如下所示: 在 C# 中創建 PDF 文件 IronPDF IronPDF 是一個 .NET C# PDF 庫,允許開發人員輕鬆地從 HTML 創建 PDF 文件。 它提供了一個直觀的 API,可以簡化從 HTML 創建 PDF 文件的過程。 IronPDF 在處理 PDF 時是強大而多功能的。 它不僅可以從簡單的 HTML 文檔創建 PDF,還可以從具有 CSS 樣式、JavaScript 交互和動態內容的複雜網頁創建 PDF。 此外,您可以快速訪問其轉換方法來開發不同的 PDF 轉換器。 Here's an example code to create PDF with IronPDF using HTML string to PDF conversion: 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"); Imports IronPdf ' Instantiate Renderer Private renderer = New ChromePdfRenderer() ' Create a PDF from an HTML string using C# Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>") ' Export to a file or Stream pdf.SaveAs("output.pdf") $vbLabelText $csharpLabel 我們創建一個 ChromePdfRenderer 的實例,以將 HTML 內容渲染為 PDF。 我們在 renderer 對象上調用 RenderHtmlAsPdf 方法,並傳遞 HTML 字符串。 這樣就生成了 PDF 文檔,然後我們使用 SaveAs 方法保存 PDF 文檔。 For more details on how to create a PDF from various resources with IronPDF, please visit this IronPDF code examples page. 結論 在本文中,我們探索了使用 wkhtmltopdf C++ 庫 在 C++ 中生成 PDF,並學習了如何使用 IronPDF 在 C# 中生成 PDF 文檔。 使用 IronPDF,從 HTML 內容生成 PDF 文件在 .NET 框架語言中變得簡單。 其豐富的功能集使其成為開發人員在其 C# 項目中需要 將 HTML 轉換為 PDF 時的寶貴工具。 無論是生成報告、發票,還是需要精確 HTML 到 PDF 轉換的其他文檔,IronPDF 是一個可靠且高效的解決方案。 IronPDF 用於開發目的免費,但商業用途需要獲得許可。 它還為商業用途提供免費試用 ,以測試其完整功能。 下載 IronPDF 並親自試用。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 相關文章 更新日期 6月 22, 2025 發現2025年最佳PDF刪除軟件 探索2025年最佳PDF刪除解決方案,包括Adobe Acrobat Pro DC、Nitro PDF Pro、Foxit PDF Editor和PDF-XChange Editor。了解IronPDF如何自動化.NET中的刪除以增強安全性和合規性。 閱讀更多 更新日期 6月 22, 2025 iPhone上最佳PDF閱讀器(免費和付費工具比較) 在本文中,我們將探索一些適合iPhone的最佳PDF閱讀器,並總結為何IronPDF脫穎而出成為最佳選擇。 閱讀更多 更新日期 6月 26, 2025 Windows上最佳免費PDF編輯器(免費和付費工具比較) 本文探討2025年可用的頂級免費PDF編輯器,並總結出最強大和靈活的選擇:IronPDF。 閱讀更多 如何在C++中查看PDF文件如何在C++中讀取PDF文件
更新日期 6月 22, 2025 發現2025年最佳PDF刪除軟件 探索2025年最佳PDF刪除解決方案,包括Adobe Acrobat Pro DC、Nitro PDF Pro、Foxit PDF Editor和PDF-XChange Editor。了解IronPDF如何自動化.NET中的刪除以增強安全性和合規性。 閱讀更多
更新日期 6月 22, 2025 iPhone上最佳PDF閱讀器(免費和付費工具比較) 在本文中,我們將探索一些適合iPhone的最佳PDF閱讀器,並總結為何IronPDF脫穎而出成為最佳選擇。 閱讀更多