Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
The ability to convert HTML files or content to PDF pages format is a valuable feature in many applications. In C++, it is quite a tedious task to build an application from scratch to generate HTML to PDF format files. So in this article, we will explore how to convert HTML to PDF in C++ using the wkhtmltopdf library.
wkhtmltopdf is an open-source command-line tool that seamlessly transforms HTML plain text pages into high-quality PDF documents. By leveraging its functionalities in C++ programs, we can easily convert HTML string content to PDF format. Let's dig into the step-by-step process of HTML page to PDF conversion in C++ using the wkhtmltopdf library.
To create HTML to PDF files converter in C++, we need to check the following things are in place:
To create a C++ PDF conversion project in Code::Blocks, follow these steps:
To ensure that Code::Blocks can find the necessary header files, we need to set up the search directories.
To link against the wkhtmltox library, follow these steps:
To get started, include the necessary header files to utilize the functionalities of the wkhtmltopdf library in your C++ program. Include the following header files at the beginning of the main.cpp source code file as shown in the following example:
#include <iostream>
#include <fstream>
#include <string>
#include <wkhtmltox/pdf.h>
#include <iostream>
#include <fstream>
#include <string>
#include <wkhtmltox/pdf.h>
#include <iostream>
#include <fstream>
#include <string>
#include <wkhtmltox/pdf.h>
To convert HTML to PDF, we need to initialize the wkhtmltopdf converter. The code goes as follows:
wkhtmltopdf_init(false);
wkhtmltopdf_global_settings* gs = wkhtmltopdf_create_global_settings();
wkhtmltopdf_object_settings* os = wkhtmltopdf_create_object_settings();
wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(gs);
wkhtmltopdf_init(false);
wkhtmltopdf_global_settings* gs = wkhtmltopdf_create_global_settings();
wkhtmltopdf_object_settings* os = wkhtmltopdf_create_object_settings();
wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(gs);
wkhtmltopdf_init(False)
wkhtmltopdf_global_settings* gs = wkhtmltopdf_create_global_settings()
wkhtmltopdf_object_settings* os = wkhtmltopdf_create_object_settings()
wkhtmltopdf_converter* converter = wkhtmltopdf_create_converter(gs)
Now, let's provide the HTML content that needs to be converted to a PDF. You can either load an HTML file or provide the string directly.
string htmlString = "<html><body><h1>Hello, World!</h1></body></html>"; wkhtmltopdf_add_object(converter, os, htmlString.c_str());
string htmlString = "<html><body><h1>Hello, World!</h1></body></html>"; wkhtmltopdf_add_object(converter, os, htmlString.c_str());
Dim htmlString As String = "<html><body><h1>Hello, World!</h1></body></html>"
wkhtmltopdf_add_object(converter, os, htmlString.c_str())
With the converter and HTML content ready, we can proceed to convert the HTML to a PDF file. Use the following code snippet:
wkhtmltopdf_convert(converter);
wkhtmltopdf_convert(converter);
wkhtmltopdf_convert(converter)
With wkhtmltopdf_get_output function, we can get the existing PDF data as memory buffer stream. It also returns the length of the PDF. The following example will perform this task:
const unsigned char* pdfData;
const int pdfLength = wkhtmltopdf_get_output(converter, &pdfData);
const unsigned char* pdfData;
const int pdfLength = wkhtmltopdf_get_output(converter, &pdfData);
const unsigned Char* pdfData
Const pdfLength As Integer = wkhtmltopdf_get_output(converter, &pdfData)
Once the conversion is complete, we need to save the generated PDF file to disk. Specify the file path where you want to save the PDF. Then using an output file stream, open the file in binary mode and write the pdfData to it. Finally, close the file. Here's a code example:
const char* outputPath = "file.pdf";
ofstream outputFile(outputPath, ios::binary);
outputFile.write(reinterpret_cast<const char*>(pdfData), pdfLength);
outputFile.close();
const char* outputPath = "file.pdf";
ofstream outputFile(outputPath, ios::binary);
outputFile.write(reinterpret_cast<const char*>(pdfData), pdfLength);
outputFile.close();
const Char* outputPath = "file.pdf"
ofstream outputFile(outputPath, ios:=:=binary)
outputFile.write(reinterpret_cast<const Char*>(pdfData), pdfLength)
outputFile.close()
After converting HTML to PDF, it's essential to clean up the resources allocated by wkhtmltopdf. Use the following code snippet:
wkhtmltopdf_destroy_converter(converter);
wkhtmltopdf_destroy_object_settings(os);
wkhtmltopdf_destroy_global_settings(gs);
wkhtmltopdf_deinit();
cout << "PDF saved successfully." << endl;
wkhtmltopdf_destroy_converter(converter);
wkhtmltopdf_destroy_object_settings(os);
wkhtmltopdf_destroy_global_settings(gs);
wkhtmltopdf_deinit();
cout << "PDF saved successfully." << endl;
wkhtmltopdf_destroy_converter(converter)
wkhtmltopdf_destroy_object_settings(os)
wkhtmltopdf_destroy_global_settings(gs)
wkhtmltopdf_deinit()
cout << "PDF saved successfully." << endl
Now, build the project and execute the code using F9. The output is generated and saved in the project folder. The resulting PDF is as follows:
IronPDF HTML-to-PDF Conversion Library is a robust .NET and .NET Core C# library that allows developers to generate PDF documents from HTML content effortlessly. It provides a straightforward and intuitive API that simplifies the process of converting HTML web pages to PDF, making it a popular choice for various applications and use cases.
One of the key advantages of IronPDF is its versatility. It supports not only the conversion of simple HTML documents but also complex web pages with CSS styling, JavaScript interactions, and even dynamic content. Moreover, you can develop different PDF converters with quick access to its conversion methods.
Here's the code example to convert HTML String to PDF using IronPDF in C#:
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")
The PDF output:
For more details on how to convert different HTML files, Web page URLs, and Images to PDF, please visit this HTML to PDF Code Examples.
With IronPDF, generating PDF files from HTML content becomes a straightforward task in .NET Framework languages. Its intuitive API and extensive feature set make it a valuable tool for developers needing to convert HTML to PDF in their C# projects. Whether it's generating reports, invoices, or any other document that requires precise HTML-to-PDF conversion, IronPDF is a reliable and efficient solution.
IronPDF is free for development purposes, but for commercial use, it needs to be licensed. It also provides a Free Trial of IronPDF's Full Functionality for commercial use to test out its complete functionality. You can download the software from Download IronPDF.
9 .NET API products for your office documents