PDF Generation in C++: Library Overview and Code Examples

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++: Library Overview and Practical Examples



Generating PDF documents is a common task in the development of enterprise applications, reporting systems, and document management. Despite the fact that C++ does not have built-in support for the PDF format, there are several mature libraries that allow you to create, edit, and render PDF files. In this article, we will review three popular tools: libharu, PoDoFo, and wkhtmltopdf. For each, we will provide a code example and describe key features.



1. libharu — Lightweight and Fast PDF Generation



libharu (Haru PDF) is a cross-platform library written in pure C that is well-suited for C++ projects. It has no external graphics library dependencies, supports Unicode (UTF-8), font embedding (TrueType), and image handling (JPEG, PNG). Its primary use case is programmatically creating documents "from scratch."



Installation

Download the source code from GitHub and build it using CMake:

mkdir build && cd buildcmake .. -DLIBHPDF_SHARED=ONmakesudo make install


Example: Creating a Simple PDF with Text

#include <hpdf.h>#include <cstdio>

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);

// Font const char* font_name = HPDF_LoadTTFontFromFile(pdf, "arial.ttf", HPDF_TRUE); HPDF_Font font = HPDF_GetFont(pdf, font_name, "UTF-8"); HPDF_Page_SetFontAndSize(page, font, 14);

// Text HPDF_Page_BeginText(page); HPDF_Page_TextOut(page, 50, 750, "Hello, world! PDF from C++."); HPDF_Page_EndText(page);

HPDF_SaveToFile(pdf, "output.pdf"); HPDF_Free(pdf); printf("PDF created: output.pdf"); return 0;}


libharu Features

  • Creation of multi-page documents
  • Vector graphics (lines, rectangles, curves)
  • Embedding JPEG/PNG images
  • Support for bookmarks (outline) and annotations
  • Page content compression


2. PoDoFo — Advanced PDF Manipulation



PoDoFo is a C++ library providing full control over the PDF structure. It allows not only creating but also editing existing documents, extracting text, managing metadata, and encryption. PoDoFo uses CMake and requires minimal setup.



Installation (Ubuntu/Debian)

sudo apt install libpodofo-dev


Example: Adding Text to a Page

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

int main() { PdfMemDocument document; document.SetPdfVersion(PdfVersion::V1_4); document.GetMetadata().SetTitle("PoDoFo Document");

PdfPage* page = document.CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); PdfPainter painter; painter.SetCanvas(*page);

PdfFont* font = document.CreateFont("Arial", false, false, false); font->SetFontSize(16); painter.SetFont(font); painter.DrawText(50, 700, "Generated with PoDoFo!");

painter.FinishDrawing(); document.Save("podofo_example.pdf"); return 0;}


PoDoFo Strengths

  • Support for PDF/A (archival format)
  • Encryption and password protection
  • Extracting text and images from PDF
  • Working with forms (AcroForms)
  • Editing existing PDFs (adding/removing pages)


3. wkhtmltopdf — HTML to PDF via C++



wkhtmltopdf is a command-line utility that converts HTML pages to PDF using the WebKit engine. For C++, you can invoke it via system() or <

Recommendations