PDF TOOLS

How to Convert HTML to PDF in C++

Curtis Chau
Curtis Chau
Updated: October 18, 2026

The ability to convert HTML files or content to PDF pages is a valuable feature in many applications. In C++, it can be tedious to build an application from scratch to generate HTML to PDF format files. This article shows how to convert HTML to PDF in C++ using the wkhtmltopdf library.

wkhtmltopdf Library

wkhtmltopdf is an open-source command-line tool that transforms HTML plain text pages into high-quality PDF documents. By using its functionalities in C++ programs, you can convert HTML string content to PDF format. The steps below walk through the process of HTML page to PDF conversion in C++ using the wkhtmltopdf library.

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.

If you keep using it, render only HTML you wrote. Set the load.blockLocalFileAccess object setting to "true", turn off web.enableJavascript when the page does not need scripts, and run the converter in a container with no route to internal services. For .NET projects, see the wkhtmltopdf migration guide and the wkhtmltopdf vs IronPDF comparison. The related C++ PDF creation tutorial uses the same API and carries the same caveats.

Prerequisites

To create an HTML to PDF file converter in C++, ensure the following prerequisites are fulfilled:

  1. A C++ compiler such as GCC or Clang installed on your system.
  2. wkhtmltopdf library installed. You can download the latest version from the official wkhtmltopdf website and install it according to your operating system's instructions.
  3. Basic knowledge of C++ programming.

Create a C++ HtmltoPdf project in Code::Blocks

To create a C++ PDF conversion project in Code::Blocks, follow these steps:

  1. Open the Code::Blocks IDE.
  2. Go to the "File" menu and select "New" and then "Project" to open the New Project wizard.
  3. In the New Project wizard, select "Console Application".
  4. Select C++ Language.
  5. Set Project Title and Location where you want to save it. Click "Next" to proceed.
  6. Select the appropriate C++ compiler and build target, such as Debug or Release. Click "Finish" to create the project.

Setting up Search Directories

To ensure that Code::Blocks can find the necessary header files, set up the search directories:

  1. Click the "Project" menu in the menu bar and select "Build options". Make sure you select "Debug".
  2. In the "Build options" dialog box, select the "Search directories" tab.
  3. Under the "Compiler" tab, click on the "Add" button.
  4. Browse to the directory where the wkhtmltox header files are located (e.g., C:\Program Files\wkhtmltopdf\include), and select it.
  5. Finally, click "OK" to close the dialog box.

Code::Blocks Build options dialog, Search directories tab, with the wkhtmltox include folder added: Figure 1

Linking the Libraries

To link against the wkhtmltox library, follow these steps:

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

Code::Blocks Build options dialog, Linker settings tab, with the wkhtmltox library file added under Link libraries: Figure 2

Steps to Easily Convert HTML to PDF in C++

Step 1: Including the Library to Convert HTML Files

To get started, 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 the main.cpp source code file as shown in the following example:

#include <iostream>
#include <fstream>
#include <string>
#include <wkhtmltox/pdf.h>
C++

Step 2: Initializing the Converter

To convert HTML to PDF, you need to initialize the wkhtmltopdf converter. The code goes as follows:

// 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++

Step 3: Setting HTML Content

Now, provide the HTML content that needs to be converted to a PDF. You can either load an HTML file or provide the string directly.

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++

Step 4: Converting HTML to PDF

With the converter and HTML content ready, you can proceed to convert the HTML to a PDF file. Use the following code snippet:

// Perform the actual conversion
if (!wkhtmltopdf_convert(converter)) {
    std::cerr << "Conversion failed!" << std::endl;
}
C++

Step 5: Getting Output as Memory Buffer

With the wkhtmltopdf_get_output function, you can get the existing PDF data as a memory buffer stream. It also returns the length of the PDF. The following example will perform this task:

// Retrieve the PDF data in memory buffer
const unsigned char* pdfData;
int pdfLength = wkhtmltopdf_get_output(converter, &pdfData);
C++

Step 6: Saving the PDF File

Once the conversion is complete, 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:

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++

Step 7: Cleaning Up

After converting HTML to PDF, clean up the resources allocated by 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;
C++

Step 8: 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:

PDF generated by the wkhtmltopdf C++ program, showing a Hello, World! heading: Figure 3

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.

HTML File to PDF File in C#

IronPDF

IronPDF HTML-to-PDF Conversion Library is a .NET and .NET Core C# library that allows developers to generate PDF documents from HTML content. It provides a straightforward and intuitive API that simplifies the process of converting HTML web pages to PDF.

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. You can also 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");

The PDF output:

PDF generated by IronPDF from an HTML string, showing a Hello World heading: Figure 4

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.

IronPDF for .NET generates PDF files from HTML in C# projects, for reports, invoices, or any other document that needs precise HTML to PDF conversion. It is free for development, and commercial use needs a license. Try it with a Free Trial of IronPDF's Full Functionality or Download IronPDF.

Conclusion

This tutorial converted an HTML string to a PDF file in C++ with the wkhtmltopdf C API, from Code::Blocks setup to saving the output. 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 the same API with a shorter example, see the C++ PDF creation 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.

Read More

Related Articles