通过 C++ 将 HTML 合并到 PDF

在 C++ 上将 HTML 文档合并为单一格式 PDF。程序员可以使用此示例代码将 HTML 与 VC++、GNU C++ 结合到各种应用程序中。

使用 C++ 将 HTML 合并到 PDF

如何将 HTML 合并到 PDF?使用 Aspose.PDF for C++ 库,你可以轻松地以编程方式将 HTML 合并到 PDF。Aspose 的 PDF 软件非常适合个人以及小型或大型企业。由于它能够处理大量信息,因此可以快速高效地进行连接并保护您的数据。Aspose.PDF 的一个特殊功能是用于将 HTML 合并到 PDF 的 API。

打开 NuGet 包管理器,搜索 Aspose.PDF 并安装。在 文档 页面上查看安装库的详细信息。要验证该库的优点,请尝试使用将 HTML 转换为 PDF 的代码片段。

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

如何通过 C++ 将 HTML 合并到 PDF


C++ 开发人员只需几行代码即可轻松地将 HTML 文件加载并合并到 PDF。

<% ld-json %>

以下示例演示了如何在 C++ 中将 HTML 合并到 PDF。轻松地将多个文档合并为一个文件。如果您在 C++ 中开发代码,则此任务可能比听起来更简单。您可以使用完全限定的文件名进行 HTML 读取和 PDF 写入。请看这个 C++ 示例,该示例说明如何使用 C++ 将相同或不同文件类型的多个文档合并为一个文件。

使用 C++ 合并 HTML 文件并另存为 PDF

示例 C++:此示例代码显示了 HTML 到 PDF 的连接

Input file:

File not added

File not added

Output format:

PDF

Output file:


    String pathSource1 = u"../../TestData/test.html";
	String pathSource2 = u"../../TestData/Second/test.html";
	auto opt1 = MakeObject<HtmlLoadOptions>();
	// set html encoding
	opt1->set_InputEncoding(u"UTF-8");
	// render all html to single large pdf page
	opt1->set_IsRenderToSinglePage(true);
	// html files can be parsed and loaded as Aspose Document
	System::SharedPtr<Document> firstDoc = MakeObject<Document>(pathSource1, opt1);
	auto opt2 = MakeObject<HtmlLoadOptions>();
	// set html encoding
	opt2->set_InputEncoding(u"UTF-8");
	// split html content to pdf pages
	opt2->set_IsRenderToSinglePage(false);
	auto secondDoc = MakeObject<Document>(pathSource2, opt2);
	// create empty pdf document
	auto outputDoc = MakeObject<Document>();
	// set less memory usage with unload instead of fast performance
	outputDoc->set_EnableObjectUnload(true);
	for (auto const& page : firstDoc->get_Pages())
	{
		// add page from one document to another directly
		outputDoc->get_Pages()->CopyPage(page);
	}
	for (auto const& page : secondDoc->get_Pages())
	{
		// add page from one document to another directly
		outputDoc->get_Pages()->CopyPage(page);
	}
	// save result pdf to file
	outputDoc->Save(u"Merger_html_pdf.pdf", SaveFormat::Pdf);

C++ 可将 HTML 合并到 PDF 的库

Aspose.PDF for C++ 是一个强大的处理库,它使开发人员无需使用 Adobe Acrobat 即可创建、读取和操作 PDF 文档。它提供了广泛的功能,例如创建表单、添加/编辑文本、操作 PDF 页面、添加注释、处理自定义字体等等。

Aspose.PDF for C++ 是一个允许开发人员向其应用程序添加 PDF 处理功能的库。API 可用于构建任何类型的 32 位和 64 位应用程序,无需使用 Adobe Acrobat 即可生成或读取、转换和操作 PDF 文件。

你可以在 API 参考的 Aspose.PDF for C++ 库 中找到每个类和方法的详细解释和示例。还建议查看 文档