Generating PDF Reports in C++ with libharu and PoDoFo

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

Generating PDF Reports in C++: Library Overview and Practical Examples



In modern software development, the need to generate printed documents, analytical reports, or invoices often arises. The C++ language provides powerful tools for working with PDF, but choosing the right library can be challenging. In this article, we will look at three main approaches: low-level work directly with PDF (libharu), working with an object document model (PoDoFo), and converting HTML to PDF (wkhtmltopdf). For each approach, code examples and application areas are provided.



1. libharu Library — A Minimalistic Approach



libharu is a lightweight library written in pure C, fully compatible with C++. It requires no external dependencies and allows you to create PDF files by controlling every page, font, and line. It is ideal for simple reports that only need text, tables, and basic graphics.



Installation

# Linux (Debian/Ubuntu)sudo apt-get install libhpdf-dev

# Windows (vcpkg)vcpkg install libharu


Example: Creating a Report with a Table

#include <hpdf.h>#include <iostream>

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

// Title HPDF_Page_BeginText(page); HPDF_Page_SetFontAndSize(page, HPDF_GetFont(pdf, "Helvetica", NULL), 18); HPDF_Page_TextOut(page, 50, 750, "Q1 Sales Report"); HPDF_Page_EndText(page);

// Table: cell coordinates const char* headers[] = {"Product", "Quantity", "Price, RUB"}; const char* data[][3] = {{"Laptop", "12", "85000"}, {"Mouse", "45", "1200"}, {"Keyboard", "30", "3500"}}; float y = 700; HPDF_Page_SetFontAndSize(page, HPDF_GetFont(pdf, "Courier", NULL), 12); for (int i = 0; i < 3; ++i) { HPDF_Page_BeginText(page); HPDF_Page_TextOut(page, 50, y, headers[i]); HPDF_Page_TextOut(page, 200, y, data[0][i]); HPDF_Page_TextOut(page, 350, y, data[1][i]); HPDF_Page_TextOut(page, 500, y, data[2][i]); HPDF_Page_EndText(page); y -= 20; }

HPDF_SaveToFile(pdf, "report_libharu.pdf"); HPDF_Free(pdf); std::cout << "PDF created: report_libharu.pdf" << std::endl; return 0;}


Pros: minimal size, no dependencies, quick start.
Cons: no Cyrillic support out of the box (requires manual font encoding), difficult to create complex layouts.



2. PoDoFo — Object Document Model



PoDoFo is a modern C++ library with support for Unicode, font embedding, working with images and forms. It provides a high-level API similar to HTML layout. It is excellent for reports with Cyrillic text, tables, and embedded fonts.



Installation

# Linuxsudo apt-get install libpodofo-dev

# Windows (vcpkg)vcpkg install podofo


Example: Report with Cyrillic and a Table

#include <podofo/podofo.h>#include <iostream>

int main() { using namespace PoDoFo;

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

// Load a font with Cyrillic support const char* fontPath = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"; PdfFont* font = document.CreateFont(fontPath); font->SetFontSize(16); painter.SetFont(font);

// Title painter.DrawText("Project «Alpha» Report", 50, 750);

// Table (manual line drawing) painter.SetStrokeWidth(1); painter.DrawLine(50, 720, 550, 720);

Recommendations