Generating PDF Reports in C++: libHaru Library

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++ with libHaru



In modern programming, there is often a need to programmatically create documents in PDF format. These can be reports, invoices, waybills, analytical summaries, or simply saving data in a universal format. In the C++ language, there are several libraries for this purpose, and one of the most popular is libHaru (also known as Haru PDF).



libHaru is a cross-platform open-source library written in C with a convenient C++ interface. It allows you to create PDF documents without external dependencies (no need to install Ghostscript, LaTeX, or other systems). The library supports working with text (including Unicode via encodings), embedding images (PNG, JPEG), drawing lines, rectangles, circles, as well as creating annotations and hyperlinks.



Installation and Setup



You can download the libHaru source code from the official GitHub repository. CMake is used for building. Example installation on Linux:



git clone https://github.com/libharu/libharu.gitcd libharumkdir build && cd buildcmake ..makesudo make install


After installation, include the header file and link the library in your project:



#include <hpdf.h>

// Linking: -lhpdf


Creating a Simple PDF Document



Let's look at a basic example — creating a document with one page and text. C++ code:



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

int main() { HPDF_Doc pdf = HPDF_New(NULL, NULL); if (!pdf) { fprintf(stderr, "Error creating PDF"); return 1; }

HPDF_Page page = HPDF_AddPage(pdf); HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_LANDSCAPE);

HPDF_Font font = HPDF_GetFont(pdf, "Helvetica", NULL); HPDF_Page_SetFontAndSize(page, font, 20);

HPDF_Page_BeginText(page); HPDF_Page_TextOut(page, 100, 400, "Hello, world! This is a PDF report in C++."); HPDF_Page_EndText(page);

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


This code creates an A4 landscape sheet, outputs text in blue (black by default), and saves the result. Note: libHaru does not directly support Cyrillic in fonts like Helvetica — for Russian text, a TrueType font is required, as shown below.



Working with Russian Text and Fonts



To correctly display Cyrillic characters, you need to load a font that supports Unicode (e.g., Arial or Times New Roman). Example:



// Loading a TrueType font (the file must be accessible)const char* font_path = "/usr/share/fonts/truetype/arial.ttf";HPDF_Font font = HPDF_GetFont(pdf, font_path, NULL);

// Or use a built-in font with UTF-8 encodingHPDF_UseUTFEncodings(pdf);HPDF_Font font = HPDF_GetFont(pdf, "Helvetica", "UTF-8");


The first method is more reliable but requires the font file to be present on the target system. The second method works only with some built-in fonts and does not guarantee full Cyrillic support.



Drawing Tables and Graphic Elements



For creating reports, you often need to draw tables. libHaru allows you to draw lines and rectangles:



HPDF_Page_SetLineWidth(page, 1);HPDF_Page_SetRGBStroke(page, 0, 0, 0);

// Draw a rectangle (x, y, width, height)HPDF_Page_Rectangle(page, 50, 500, 200, 30);HPDF_Page_Stroke(page);

// Draw a lineHPDF_Page_MoveTo(page, 50, 500);HPDF_Page_LineTo(page, 250, 500);HPDF_Page_Stroke(page);


You can combine lines and text to create a full table. For complex reports with dynamic data, it is convenient to write a helper function that takes an array of strings

Recommendations