PDF Generation in C++: Overview of Libraries and Tools
Generating PDF documents is one of the common tasks in C++ software development. From creating simple reports to complex multi-page documents with tables, images, and graphics. In this article, we will review the most popular libraries and tools for working with PDF in C++, their capabilities, advantages and disadvantages, and provide practical code examples.
Why is PDF Generation Needed in C++?
PDF (Portable Document Format) is a cross-platform format that preserves document layout regardless of the device and operating system. In C++, PDF generation is required for:
- Creating reports and invoices in business applications
- Generating documentation from templates
- Exporting data from databases
- Generating receipts and waybills in accounting systems
Main Libraries for Working with PDF in C++
1. libharu (HARU PDF)
One of the most popular and easy-to-use libraries for PDF generation. Written in C, but has an excellent C++ interface. Allows you to create PDF documents from scratch, add text, images, lines, rectangles, annotations.
Features:
- Small size and high performance
- TrueType font support
- Page compression
- Image support (JPEG, PNG)
- License: ZLIB/Libpng (free)
Example of generating a simple PDF:
#include <hpdf.h>#include <stdio.h>
int main() { HPDF_Doc pdf = HPDF_New(NULL, NULL); HPDF_Page page = HPDF_AddPage(pdf); // Set page size to A4 HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_LANDSCAPE); // Add text HPDF_Page_BeginText(page); HPDF_Page_SetFontAndSize(page, HPDF_GetFont(pdf, "Helvetica", NULL), 24); HPDF_Page_TextOut(page, 100, 500, "Hello, PDF world in C++!"); HPDF_Page_EndText(page); // Save the document HPDF_SaveToFile(pdf, "output.pdf"); HPDF_Free(pdf); return 0;}2. PoDoFo
A modern open-source C++ library for working with PDF. Supports not only creation but also editing of existing PDF files. Well-documented and actively maintained.
Features:
- Pure C++ code (modern standards)
- Unicode and Cyrillic support
- Working with forms, bookmarks, metadata
- PDF reading and writing
- License: LGPL
Example of creating a PDF with a table:
#include <podofo/podofo.h>
using namespace PoDoFo;
int main() { PdfMemDocument document; PdfPage* page = document.CreatePage(PdfPage::CreateStandardPageSize(PdfPageSize::A4)); PdfPainter painter; painter.SetCanvas(*page); // Draw a table painter.DrawRect(50, 700, 500, 30); painter.DrawText(60, 710, "Table header"); // Save painter.FinishPage(); document.Save("table.pdf"); return 0;}3. wkhtmltopdf
Not a library in the classical sense, but a command-line utility that uses the WebKit engine to convert HTML to PDF. In C++, you can call it via system() or use a wrapper library.
Features:
- Ideal for generating PDF from HTML templates
- Support for CSS, JavaScript, SVG
- High-quality layout (as in a browser)
- Headers, footers, table of contents, page numbering
- License: LGPLv3
Example of calling from C++:
#include <cstdlib>
int main() { // Convert HTML file to PDF int result = std::system("w