PDF工具 如何在C++中將HTML轉PDF Curtis Chau 更新日期:7月 28, 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 將 HTML 文件或內容轉換為 PDF 頁面是在許多應用程式中非常有價值的功能。 在 C++ 中,從頭開始建立一個能夠生成 HTML 到 PDF 格式文件的應用程式可能相當繁瑣。 在本文中,我們將探討如何使用 wkhtmltopdf 庫將 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。 轉到 "File" 菜單並選擇 "New",然後選擇 "Project" 以打開新項目向導。 在新項目向導中,選擇 "Console Application"。 選擇 C++ 語言。 設定項目標題和您想保存的位置。 點擊 "Next" 繼續。 選擇適當的 C++ 編譯器和構建目標,例如 Debug 或 Release。 點擊 "Finish" 創建項目。 設置搜索目錄 為了確保 Code::Blocks 可以找到必要的頭文件,我們需要設置搜索目錄: 單擊菜單欄中的“項目”菜單並選擇“構建選項”。 請確保選擇“調試”。 在 "Build options" 對話框中,選擇 "Search directories" 標籤。 在“編譯器”選項卡下,單擊“添加”按鈕。 瀏覽到 wkhtmltox 頭文件所在的目錄(例如,C:\Program Files\wkhtmltopdf\include),並選擇它。 最後,點擊 "OK" 關閉對話框。 連接庫 要鏈接 wkhtmltox 庫,請按照以下步驟操作: 再次點擊菜單欄中的 "Project" 菜單,然後選擇 "Build options"。 請確保選擇“調試”。 在 "Build options" 對話框中,選擇 "Linker settings" 標籤。 在“鏈接庫”選項卡下,單擊“添加”按鈕。 瀏覽到 wkhtmltox 庫文件所在的目錄(例如,C:\Program Files\wkhtmltopdf\lib),並選擇適當的庫文件。 單擊“打開”以將庫添加到您的項目中。 最後,點擊 "OK" 關閉對話框。 Steps to Easily Convert HTML to PDF in C++ 步驟 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> C++ 步驟 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); C++ 步驟 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()); C++ 步驟 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; } C++ 步驟 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); C++ 步驟 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(); C++ 步驟 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; C++ 步驟 8:執行代碼並生成 PDF 文件 現在,構建項目並使用 F9 執行代碼。輸出生成並保存在項目文件夾中。 生成的 PDF 如下所示: HTML 文件到 PDF 文件在 C IronPDF IronPDF HTML-to-PDF Conversion Library 是一個功能強大的 .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"); Imports IronPdf ' Instantiate Renderer Private renderer = New ChromePdfRenderer() ' Create PDF content 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 PDF 輸出: 有關如何將不同的 HTML 文件、網頁 URL 和圖像轉換為 PDF 的更多詳細信息,請訪問此 HTML to PDF Code Examples。 使用 IronPDF,從 HTML 內容生成 PDF 文件在 .NET 框架語言中變得簡單。 其直觀的 API 和廣泛的功能集使其成為開發人員在其 C# 項目中需要將 HTML 轉換為 PDF 的寶貴工具。 無論是生成報告、發票,還是需要精確 HTML 到 PDF 轉換的其他文檔,IronPDF 是一個可靠且高效的解決方案。 IronPDF 可免費用於開發目的,但商業用途需要許可。 它還提供一個 IronPDF的完整功能的免費試用版 以商業用途之目的測試其完整功能。 您可以從 Download 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文件如何在Node.js中使用Puppeteer...
更新日期 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脫穎而出成為最佳選擇。 閱讀更多