Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
PDF (Portable Document Format) files are widely used for sharing and preserving documents in a consistent format across different platforms. In this article, we will explore how to generate PDF files in C++ using the wkhtmltopdf library. It is an open source command-line tool, which is used to convert HTML content into PDF files. By leveraging the power of this library, we can programmatically create PDF documents from HTML content in our C++ applications.
wkhtmltopdf is an open-source command-line tool and rendering engine that converts HTML web pages or HTML content into PDF documents. It utilizes the WebKit rendering engine to parse and render HTML, CSS, and JavaScript, and then generates a PDF file based on the rendered content.
"wkhtmltopdf C++ Library" (also known as "wkhtmltopdf C API"). This library provides a C++ interface to the wkhtmltopdf
command-line tool, allowing you to create PDF files from HTML content directly in your C++ applications. Let's dig into the step-by-step process of HTML to PDF conversion in C++ using the Wkhtmltopdf library.
To create PDF files in C++, we need to the following things in place:
Create a project in Code::Blocks and then follow the below steps to link the library folder and file. If you are using some other IDE the options might be different, but you need to reference the downloaded library in your project to make it work.
To ensure that Code::Blocks can find the necessary header files, we need to set up the search directories.
Finally, click "OK" to close the dialog-box.
To link against the wkhtmltox library, follow these steps:
Finally, click "OK" to close the dialog-box.
To get started, first we will include the necessary header files to utilize the functionalities of the wkhtmltopdf library in our C++ program. Include the following header files at the beginning of main.cpp source code file:
#include <iostream>
#include <fstream>
#include <string>
#include <wkhtmltox/pdf.h>
#include <iostream>
#include <fstream>
#include <string>
#include <wkhtmltox/pdf.h>
If you are not working with GUI then you need to disable it to avoid errors. The code goes as follows:
wkhtmltopdf_init(false);
wkhtmltopdf_init(false);
Next, we need to initialize global and object setting pointers and then the wkhtmltopdf
converter.
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);
Now, let's create an HTML string to fill in some content in our newly created PDF document. The code goes as follows:
string htmlString = "<html><body><h1>Create a PDF in C++ using WKHTMLTOPDF Library</h1></body></html>";
wkhtmltopdf_add_object(converter, os, htmlString.c_str());
string htmlString = "<html><body><h1>Create a PDF in C++ using WKHTMLTOPDF Library</h1></body></html>";
wkhtmltopdf_add_object(converter, os, htmlString.c_str());
Wkhtmltopdf provides a converter method to convert HTML content to PDF. Here's the code snippet:
wkhtmltopdf_convert(converter);
wkhtmltopdf_convert(converter);
With the wkhtmltopdf_get_output
function, we can get the PDF data as a memory buffer stream. It also returns the length of the PDF. 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);
The pdfData
pointer will now have the stream of characters from the wkhtmltopdf_converter
. To save to a PDF file, first we will specify the file path where we want to save the PDF. Then, using the output file stream, we open the file in binary mode and write the pdfData
to it by type casting it using the reinterpret_cast
function. Finally, we close the file.
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 successfully creating a PDF file, it's essential to clean up the resources allocated by Wkhtmltopdf, otherwise it will result in a memory leak.
wkhtmltopdf_destroy_converter(converter);
wkhtmltopdf_destroy_object_settings(os);
wkhtmltopdf_destroy_global_settings(gs);
wkhtmltopdf_deinit();
cout << "PDF created successfully." << endl;
wkhtmltopdf_destroy_converter(converter);
wkhtmltopdf_destroy_object_settings(os);
wkhtmltopdf_destroy_global_settings(gs);
wkhtmltopdf_deinit();
cout << "PDF created 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 is a .NET C# PDF library that allows developers to create PDF documents from HTML effortlessly. It provides an intuitive API that simplifies the process of creating PDF files from HTML.
IronPDF is robust and versatile when it comes to working with PDFs. It not only creates a PDF from 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 an example code to create PDF with IronPDF using HTML string to PDF conversion:
using IronPdf;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from a 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 a HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
// Export to a file or Stream
pdf.SaveAs("output.pdf");
We create an instance of ChromePdfRenderer
to render the HTML content as a PDF. We call the RenderHtmlAsPdf
method on the renderer
object and pass in the HTML string. This generates the PDF document, and then we save the PDF document using the SaveAs
method.
For more details on how to create a PDF from various resources with IronPDF, please visit this IronPDF code examples page.
In this article, we explored PDF generation in C++ using the wkhtmltopdf C++ Library, and also learned how to use IronPDF to generate PDF documents in C#.
With IronPDF, generating PDF files from HTML content becomes a straightforward task in .NET Framework languages. Its 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 for commercial use to test out its complete functionality. Download IronPDF and try it out for yourself.
9 .NET API products for your office documents