How to Create PDF Files in C++
PDF (Portable Document Format) files are widely used for sharing and preserving documents in a consistent format across different platforms. This article shows how to generate PDF files in C++ using wkhtmltopdf, through libwkhtmltox, the C API behind the wkhtmltopdf command, which you call from C++. It is an open-source command-line tool, which is used to convert HTML content into PDF files. With it, you can programmatically create PDF documents from HTML content in your C++ applications.
wkhtmltopdf Library
wkhtmltopdf is an open-source command-line tool and rendering engine that converts HTML web pages or HTML content into PDF documents. It uses the WebKit rendering engine to parse and render HTML, CSS, and JavaScript, and then generates a PDF file based on the rendered content.
The library used in this tutorial is libwkhtmltox, the C API behind the wkhtmltopdf command, which you call from C++. It exposes the wkhtmltopdf converter as plain C functions, allowing you to create PDF files from HTML content directly in your C++ applications. The sections below walk through the step-by-step process of HTML to PDF conversion in C++ using libwkhtmltox. The HTML to PDF in C++ tutorial covers the same API with a longer example.
Is wkhtmltopdf Safe to Use?
wkhtmltopdf still works, but nobody maintains it. The GitHub repository was archived on 2 January 2023 and is read-only. The last release, 0.12.6, is from June 2020. It renders with Qt 4.8 and QtWebKit, and Qt 4.8 standard support ended on 19 December 2015, so years of browser-engine fixes are missing.
The best-known issue is CVE-2022-35583, a server-side request forgery. HTML passed to wkhtmltopdf 0.12.6 can make it fetch internal URLs, such as cloud metadata endpoints, and write the response into the PDF. It carries a CVSS 3.x score of 9.8 (Critical). The maintainers dispute it: wkhtmltopdf fetches resources by design, so they consider the fault to lie with applications that render untrusted HTML. Both views lead to the same place. No fix is coming, so the risk is yours to manage. The Ubuntu and Debian security trackers record the dispute.
If you keep using it, render only HTML you wrote. Set the load.blockLocalFileAccess object setting to "true" (see the libwkhtmltox page settings), turn off web.enableJavascript when the page does not need scripts, and run the converter in a container with no route to internal services. The code in this tutorial renders a fixed HTML string, so it is not exposed to untrusted input as written. For .NET projects, see the wkhtmltopdf migration guide and the wkhtmltopdf vs IronPDF comparison.
Prerequisites
To create PDF files in C++, you need the following:
- A C++ compiler such as GCC or Clang installed on your system. You can use any IDE that supports C++ programming.
- wkhtmltopdf library downloaded and installed. You can download the latest version from the official wkhtmltopdf website and install it according to your operating system's instructions.
- Basic knowledge of C++ programming.
C++ Create PDF project in Code::Blocks
Create a project in Code::Blocks and then follow the steps below to link the library folder and file. If you are using another IDE, the options might be different, but you need to reference the downloaded library in your project to make it work.
1. Adding include folder in Search Directories
To ensure that Code::Blocks can find the necessary header files, you need to set up the search directories.
- Click the "Project" menu in the menu bar and select "Build options". Make sure you select "Debug".
- In the "Build options" dialog box, select the "Search directories" tab.
- Under the "Compiler" tab, click on the "Add" button.
- Browse to the directory where the wkhtmltox header files are located (e.g., C:\Program Files\wkhtmltopdf\include), and select it.
- Finally, click "OK" to close the dialog box.

2. Linking the Libraries in Linker Settings
To link against the wkhtmltox library, follow these steps:
- Again, click the "Project" menu in the menu bar and select "Build options". Make sure you select "Debug".
- In the "Build options" dialog box, select the "Linker settings" tab.
- Under the "Link libraries" tab, click the "Add" button.
- Browse to the directory where the wkhtmltox library files are located (e.g., C:\Program Files\wkhtmltopdf\lib), and select the appropriate library file.
- Click "Open" to add the library to your project.
- Finally, click "OK" to close the dialog box.

Steps to Create a PDF in C++ using wkhtmltopdf
1. Include Necessary Libraries
To get started, first include the necessary header files to use the functionalities of the wkhtmltopdf library in your C++ program. Include the following header files at the beginning of your main.cpp source code file:
#include <iostream>
#include <fstream>
#include <string>
#include <wkhtmltox/pdf.h> // Include wkhtmltopdf library headers
2. Disable the GUI Component
If you are not working with a GUI, then you need to disable it to avoid errors. The code goes as follows:
wkhtmltopdf_init(false); // Initialize the wkhtmltopdf library in non-GUI mode
3. Initialize Settings and Converter
Next, you need to initialize global and object settings 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);
4. Adding PDF Content
Now, create an HTML string to fill some content into your newly created PDF document. The code goes as follows:
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());
5. Convert HTML String to PDF File
wkhtmltopdf provides a converter method to convert HTML content to PDF. Here's the code snippet:
wkhtmltopdf_convert(converter); // Converts HTML content to PDF
6. Getting output as Memory Buffer
With the wkhtmltopdf_get_output function, you can get the PDF data as a memory buffer stream. It also returns the length of the PDF. The following example will perform this task:
const unsigned char* pdfData;
int pdfLength = wkhtmltopdf_get_output(converter, &pdfData); // Get PDF data and length
7. Save PDF File
The pdfData pointer will now have the stream of characters from the wkhtmltopdf_converter. To save to a PDF file, first, specify the file path where you want to save the PDF. Then, using the output file stream, open the file in binary mode and write the pdfData to it by type casting it using the reinterpret_cast function. Finally, 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
8. Freeing up Memory
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); // 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
9. Execute the Code and Generate PDF File
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 for C++ (Coming Soon)
IronPDF for C++ is in development. The planned library will generate PDFs from HTML text or a URL, combine, split and modify existing PDFs, and extract text and images from them. Join the IronPDF for C++ beta program to get early access and help shape the API.
Create PDF File in C#
IronPDF
IronPDF is a .NET C# PDF library that allows developers to create PDF documents from HTML in a few lines of code. It provides an intuitive API that simplifies the process of creating PDF files from HTML.
IronPDF is 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 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 a PDF from an HTML string using C#
Private 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 a PDF 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 code creates an instance of ChromePdfRenderer to render the HTML content as a PDF. It calls the RenderHtmlAsPdf method on the renderer object and pass in the HTML string. This generates the PDF document, and then the code saves 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.
IronPDF for .NET turns HTML into PDF files for reports, invoices, and other documents in C# projects. It is free for development, and commercial use needs a license. Try it with a free trial or download IronPDF.
Conclusion
This article showed how to create a PDF in C++ with libwkhtmltox, the C API behind the wkhtmltopdf command. It works for HTML you control, but the project is archived and CVE-2022-35583 will not be fixed, so treat it as a legacy dependency. For a longer walkthrough of the same API, see the HTML to PDF in C++ tutorial.
IronPDF for C++ is coming, and it is planned to generate PDFs from HTML text or a URL. Join the beta for early access.
Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.