Generating PDF Reports in C++ with libharu and PoDoFo
In modern C++ development, there is often a need to programmatically create PDF documents: from simple text reports to complex multi-page documents with tables, charts, and images. In this article, we will take a detailed look at two popular libraries for generating PDFs in C++ — libharu and PoDoFo, and provide practical examples of their use.
1. libharu Library
libharu is a lightweight, cross-platform library for creating PDF files in C. It has no external dependencies (except zlib for compression), supports Unicode, font embedding, images (JPEG/PNG), and basic vector primitives. It is ideal for generating simple reports, invoices, and certificates.
Installation (Linux / Windows)
- Linux (Ubuntu/Debian):
sudo apt-get install libhpdf-dev - Windows (vcpkg):
vcpkg install libharu - Windows (build from source): download the archive from GitHub, build using CMake.
Example: creating a simple PDF with text and a line
#include <hpdf.h>#include <stdio.h>
int main() { HPDF_Doc pdf = HPDF_New(NULL, NULL); if (!pdf) { fprintf(stderr, "Error creating document"); return 1; }
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), 24); HPDF_Page_TextOut(page, 50, 750, "Q1 Sales Report"); HPDF_Page_EndText(page);
// Line under the title HPDF_Page_SetLineWidth(page, 1.5); HPDF_Page_MoveTo(page, 50, 735); HPDF_Page_LineTo(page, 545, 735); HPDF_Page_Stroke(page);
// Save HPDF_SaveToFile(pdf, "report.pdf"); HPDF_Free(pdf); printf("PDF created: report.pdf"); return 0;}Example: adding a table (simulation)
libharu does not have a built-in table module, but tables can be drawn manually:
void draw_table(HPDF_Page page, float x, float y, float col_width, float row_height, int rows, int cols) { for (int r = 0; r <= rows; ++r) { HPDF_Page_MoveTo(page, x, y - r * row_height); HPDF_Page_LineTo(page, x + cols * col_width, y - r * row_height); HPDF_Page_Stroke(page); } for (int c = 0; c <= cols; ++c) { HPDF_Page_MoveTo(page, x + c * col_width, y); HPDF_Page_LineTo(page, x + c * col_width, y - rows * row_height); HPDF_Page_Stroke(page); }}2. PoDoFo Library
PoDoFo is a more modern and powerful C++ library with C++17 support. It provides an object-oriented API, support for forms, annotations, bookmarks, TrueType/OpenType font embedding, as well as reading and editing existing PDFs. PoDoFo is better suited for complex reports with dynamic content.
Installation
- Linux:
sudo apt-get install libpodofo-dev(the version may be old; it is better to build from source) - Windows: via vcpkg:
vcpkg install podofo - Build from source: requires CMake, zlib, libpng, freetype, fontconfig.
Example: creating a PDF with a table and an image
#include <podofo/podofo.h>#include <iostream>
int main() { using namespace PoDoFo;
PdfMemDocument doc; PdfPage* page = doc.CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); PdfPainter painter; painter.SetCanvas(*page);
// Font auto