跳過到頁腳內容
PDF工具

如何在C++中創建PDF文件

PDF(便攜式文件格式)文件廣泛用於在不同平台上以一致的格式共用和保存文件。 在本文中,我們將探討如何使用 wkhtmltopdf 函式庫在 C++ 中產生 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 文件,我們需要以下工具:

  1. 您的系統上已安裝 C++ 編譯器,例如 GCC 或 Clang。 您可以使用任何支援 C++ 程式設計的整合開發環境 (IDE)。
  2. wkhtmltopdf 庫已下載並安裝。 您可以從wkhtmltopdf 官方網站下載最新版本,並按照您的作業系統說明進行安裝。
  3. 具備 C++ 程式設計基礎。

在 Code::Blocks 中建立 C++ PDF 項目

在 Code::Blocks 中建立一個項目,然後按照以下步驟連結庫資料夾和檔案。如果您使用的是其他 IDE,操作步驟可能有所不同,但您都需要在專案中引用下載的程式庫才能使其正常運作。

1. 在搜尋目錄中新增包含資料夾

為了確保 Code::Blocks 能夠找到必要的頭文件,我們需要設定搜尋目錄。

  1. 點選選單列中的"項目"選單,然後選擇"建置選項"。 請務必選擇"調試"。
  2. 在"建置選項"對話方塊中,選擇"搜尋目錄"標籤。
  3. 在"編譯器"標籤下,按一下"新增"按鈕。
  4. 瀏覽至wkhtmltox頭檔所在的目錄(例如, C:\Program Files\wkhtmltopdf\include ),然後選擇它。
  5. 最後,按一下"確定"關閉對話框。

如何在C++中建立PDF檔案:圖1

2. 在連結器設定中連結庫

要連結到wkhtmltox庫,請按照以下步驟操作:

  1. 再次點選選單列中的"項目"選單,然後選擇"建置選項"。 請務必選擇"調試"。
  2. 在"建置選項"對話方塊中,選擇"連結器設定"標籤。
  3. 在"連結庫"標籤下,按一下"新增"按鈕。
  4. 瀏覽至wkhtmltox庫檔案所在的目錄(例如, C:\Program Files\wkhtmltopdf\lib ),然後選擇對應的庫檔案。
  5. 點擊"開啟"將庫新增到您的專案中。
  6. 最後,按一下"確定"關閉對話方塊。

如何在C++中建立PDF檔案:圖2

使用 Wkhtmltopdf 在 C++ 中建立 PDF 的步驟

1. 包含必要的庫

首先,我們將包含必要的頭文件,以便在我們的 C++ 程式中使用 wkhtmltopdf 庫的功能。 請在原始碼檔案的開頭包含以下頭檔:

#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檔案:圖3

Create PDF File in C

IronPDF

IronPDF是一個.NET C# PDF 文件庫,它允許開發人員輕鬆地從 HTML 建立 PDF 文件。 它提供了一個直覺的 API,簡化了從 HTML 建立 PDF 檔案的過程。

IronPDF在處理 PDF 文件方面功能強大且用途廣泛。 它不僅可以從簡單的 HTML 文件中建立 PDF,還可以從包含 CSS 樣式、 JavaScript互動甚至動態內容的複雜網頁中建立 PDF。 此外,您還可以開發不同的 PDF 轉換器,並快速存取其轉換方法。

以下是使用IronPDF透過HTML 字串轉 PDF建立 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");
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");
$vbLabelText   $csharpLabel

我們建立 ChromePdfRenderer 的實例,以將 HTML 內容渲染為 PDF。 我們呼叫 RenderHtmlAsPdf 物件上的方法,並傳入 HTML 字串。 這樣就產生了 PDF 文檔,然後我們使用 SaveAs 方法來儲存 PDF 文件。

如何在C++中建立PDF檔案:圖4

有關如何使用IronPDF從各種資源建立 PDF 的更多詳細信息,請訪問此IronPDF程式碼範例頁面。

結論

在本文中,我們探討如何使用wkhtmltopdf C++ 函式庫在 C++ 中產生 PDF,並學習如何使用IronPDF在 C# 中產生 PDF 文件。

使用IronPDF,在.NET Framework語言中,從 HTML 內容產生 PDF 檔案變得非常簡單。 它豐富的功能使其成為開發人員在 C# 專案中將HTML 轉換為 PDF 的寶貴工具。 無論是產生報告、發票,或是任何其他需要精確的 HTML 轉 PDF 的文檔, IronPDF都是一個可靠且有效率的解決方案。

IronPDF可免費用於開發用途,但用於商業用途則需獲得許可。 它還提供免費的商業試用版,供用戶測試其全部功能。 下載IronPDF並親自體驗。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me