PDF Generation in C++: Overview of Libraries and Tools

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

PDF Generation in C++: Overview of Libraries and Tools



Generating PDF documents is one of the common tasks in software development. Reports, invoices, certificates, documentation — all of this requires a reliable and flexible tool for creating PDFs. In the C++ ecosystem, there are several proven solutions, each with its own strengths. In this article, we will review the main libraries and tools that will help you implement PDF generation in C++, and also provide code examples for each of them.



1. PoDoFo — A Powerful Library for Working with PDF



PoDoFo is a cross-platform C++ library designed for creating, editing, and analyzing PDF documents. It provides full control over the file structure and supports fonts, images, annotations, and forms.



Key Features:

  • Creating PDFs from scratch and editing existing documents
  • Support for TrueType, Type1, and standard PDF fonts
  • Embedding images (PNG, JPEG, TIFF)
  • Working with tables and multi-page documents
  • Support for compression and encryption


Code Example: Creating a Simple PDF with Text

#include <podofo/podofo.h>using namespace PoDoFo;

int main() { PdfMemDocument document; PdfPage* page = document.CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); PdfPainter painter; painter.SetPage(page);

// Set the font PdfFont* font = document.CreateFont("Arial"); painter.SetFont(font, 12.0);

// Draw text painter.DrawText("Hello, PDF World!", 50, 700);

painter.FinishPage(); document.Save("output_podofo.pdf"); return 0;}


2. libharu — A Lightweight and Fast Library



libharu (or HPDF) is a minimalist C library for PDF generation. Despite being written in C, it works perfectly with C++ and does not require complex dependencies. libharu is ideal for simple tasks where full PDF standard support is not needed.



Key Features:

  • Support for text, images, lines, and rectangles
  • Working with Cyrillic characters via encodings
  • Page compression
  • Zlib license (free)


Code Example: Creating a PDF with an Image

#include <hpdf.h>#include <stdio.h>

int main() { HPDF_Doc pdf = HPDF_New(NULL, NULL); HPDF_Page page = HPDF_AddPage(pdf); HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);

// Load an image HPDF_Image image = HPDF_LoadJpegImageFromFile(pdf, "example.jpg"); HPDF_Page_DrawImage(page, image, 50, 500, 200, 150);

// Text HPDF_Page_BeginText(page); HPDF_Page_SetFontAndSize(page, HPDF_GetFont(pdf, "Helvetica", NULL), 14); HPDF_Page_TextOut(page, 50, 700, "Example with an image"); HPDF_Page_EndText(page);

HPDF_SaveToFile(pdf, "output_libharu.pdf"); HPDF_Free(pdf); return 0;}


3. wkhtmltopdf — Converting HTML to PDF



wkhtmltopdf is not a library in the classic sense, but a command-line utility based on WebKit. However, it can be easily called from C++ code via system() or popen(). This is an ideal choice if you need to convert an HTML template into a PDF document while preserving styles and layout.



Key Features:

  • Support for CSS, JavaScript, tables, and images
  • Configuration of margins, orientation, headers, and footers
  • High rendering quality
  • Ease of integration


Code Example: Calling wkhtmltopdf from C++

#include <cstdlib>#include <string>

int main() { std::string input = "report.html"; std::string output = "report.pdf"; std::string

Recommendations