跳至页脚内容
PDF 工具

如何在 C++ 中将 HTML 转换为 PDF

将 HTML 文件或内容转换为 PDF 页面是一项在许多应用程序中都很有价值的功能。 在 C++ 中,从头开始构建一个应用程序来生成 HTML 到 PDF 格式的文件可能相当繁琐。 在本文中,我们将探讨如何使用 wkhtmltopdf 库在 C++ 中将 HTML 转换为 PDF。

WKHTMLTOPDF 库

wkhtmltopdf 是一个开源命令行工具,可以无缝地将 HTML 纯文本页面转换为高质量的 PDF 文档。 通过在 C++ 程序中利用其功能,我们可以轻松地将 HTML 字符串内容转换为 PDF 格式。 让我们深入了解使用 wkhtmltopdf 库在 C++ 中逐步将 HTML 页面转换为 PDF 的过程。

前提条件

要在 C++ 中创建一个 HTML 到 PDF 文件转换器,请确保满足以下先决条件:

  1. 在您的系统上安装诸如 GCC 或 Clang 的 C++ 编译器。
  2. 安装 wkhtmltopdf 库。 您可以从官方的 wkhtmltopdf 网站下载最新版本并根据您的操作系统说明进行安装。
  3. 具备 C++ 编程的基础知识。

在 Code::Blocks 中创建一个 C++ HtmltoPdf 项目

要在 Code::Blocks 中创建一个 C++ PDF 转换项目,请按照以下步骤操作:

  1. 打开 Code::Blocks IDE。
  2. 转到“文件”菜单,选择“新建”,然后选择“项目”以打开新建项目向导。
  3. 在新建项目向导中,选择“控制台应用程序”。
  4. 选择 C++ 语言。
  5. 设置项目标题和您要保存的位置。 点击“下一步”继续。
  6. 选择适当的 C++ 编译器和构建目标,例如调试或发布。 点击“完成”创建项目。

设置搜索目录

要确保 Code::Blocks 能够找到必要的头文件,我们需要设置搜索目录:

  1. 点击菜单栏中的“项目”菜单,选择“构建选项”。 确保选择“调试”。
  2. 在“构建选项”对话框中,选择“搜索目录”选项卡。
  3. 在“编译器”选项卡下,点击“添加”按钮。
  4. 浏览到 wkhtmltox 头文件所在的目录(例如:C:\Program Files\wkhtmltopdf\include),并选择它。
  5. 最后,点击“确定”关闭对话框。

在 C++ 中将 HTML 转换为 PDF:图 1 - 搜索目录

链接库

要链接 wkhtmltox 库,请按照以下步骤操作:

  1. 再次点击菜单栏中的“项目”菜单,选择“构建选项”。 确保选择“调试”。
  2. 在“构建选项”对话框中,选择“链接器设置”选项卡。
  3. 在“链接库”选项卡下,点击“添加”按钮。
  4. 浏览到 wkhtmltox 库文件所在的目录(例如:C:\Program Files\wkhtmltopdf\lib),并选择适当的库文件。
  5. 点击“打开”将库添加到您的项目。
  6. 最后,点击“确定”关闭对话框。

在 C++ 中将 HTML 转换为 PDF:图 2 - 链接库

Steps to Easily Convert HTML to PDF in C++

步骤 1:包含将 HTML 文件转换到 PDF 的库

首先,在您的 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 如下所示:

在 C++ 中将 HTML 转换为 PDF:图 3 - PDF 输出

IronPDF。

HTML 文件到 PDF 文件在 C# IronPDF HTML 到 PDF 转换库 是一个强大的 .NET 和 .NET Core C# 库,允许开发人员轻松地从 HTML 内容生成 PDF 文档。

它提供了一个简单且直观的 API,使将 HTML 网页转换为 PDF 的过程变得简单,成为各种应用程序和使用情况的热门选择。 IronPDF 的一个重要优势是其多功能性。 它不仅支持简单 HTML 文档的转换,还支持具有 CSS 样式、JavaScript 交互,甚至动态内容的复杂网页。

此外,您可以通过快速访问其转换方法开发不同的 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

这是使用 IronPDF 在 C# 中将 HTML 字符串转换为 PDF 的代码示例:

PDF 输出:

在 C++ 中将 HTML 转换为 PDF:图 4 - IronPDF 输出

有关如何将不同的 HTML 文件、网页 URL 和图像转换为 PDF 的更多详细信息,请访问此 HTML 到 PDF 代码示例。 使用 IronPDF,从 HTML 内容生成 PDF 文件成为 .NET Framework 语言中的一项简单任务。 其直观的 API 和广泛的功能集使其成为需要在 C# 项目中转换 HTML 到 PDF 的开发人员的重要工具。

无论是生成报告、发票,还是任何需要精确 HTML 到 PDF 转换的文档,IronPDF 都是一个可靠且高效的解决方案。 IronPDF 可免费用于开发用途,但用于商业用途需要获得许可。 它还为商业用途提供 IronPDF 完整功能的免费试用 ,以测试其完整的功能。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。