PDF generation with text, fonts, images, graphics, bookmarks, and compression. Create professional PDFs from scratch.
ppm install libharu
Requires libhpdf-dev on Ubuntu/Debian: sudo apt install libhpdf-dev
Overview
libharu is a free, cross-platform C library for generating PDF files. It supports text layout, built-in and TrueType fonts,
JPEG and PNG images, vector graphics (lines, rectangles, circles, Bezier curves), outlines/bookmarks, and deflate compression.
Coordinate system: origin (0, 0) is the bottom-left corner of the page. 1 point = 1/72 inch.
An A4 page is 595 × 842 points.
Quick Start
uses libharu;
var doc := NewDoc();
EnableCompression(doc);
SetTitle(doc, 'My Report');
SetAuthor(doc, 'Alice');
var page := AddPage(doc);
SetPageSize(page, pdfA4, pdfPortrait);
var font := GetFont(doc, 'Helvetica-Bold');
SetFont(page, font, 24);
SetFillColorRGB(page, 0.2, 0.2, 0.7);
TextOut(page, 50, 780, 'Hello, PDF!');
var body := GetFont(doc, 'Times-Roman');
SetFont(page, body, 12);
SetFillColorRGB(page, 0, 0, 0);
TextRect(page, 50, 750, 545, 500,
'This PDF was generated with libharu and PascalAI.',
pdfAlignLeft);
SaveToFile(doc, '/tmp/report.pdf');
FreeDoc(doc);
Text Layout
var page := AddPage(doc);
SetPageSize(page, pdfA4, pdfPortrait);
var font := GetFont(doc, 'Helvetica');
SetFont(page, font, 14);
SetTextLeading(page, 20); { 20pt line height }
BeginText(page);
MoveTextPos(page, 50, 750);
ShowText(page, 'Line 1');
NextLine(page);
ShowText(page, 'Line 2');
NextLine(page);
ShowText(page, 'Line 3');
EndText(page);
{ Measure text width before placing it }
var w := TextWidth(page, 'Centered heading');
var pageW := GetPageWidth(page);
TextOut(page, (pageW - w) / 2, 700, 'Centered heading');
Vector Graphics
{ Filled blue rectangle }
SetFillColorRGB(page, 0.15, 0.45, 0.85);
FillRect(page, 50, 600, 200, 40);
{ Red border rectangle }
SetStrokeColorRGB(page, 0.8, 0, 0);
SetLineWidth(page, 2);
Rectangle(page, 50, 550, 200, 40);
Stroke(page);
{ Dashed diagonal line }
SetDash(page, [8, 4], 0);
DrawLine(page, 50, 530, 545, 400);
{ Circle }
SetFillColorRGB(page, 0, 0.7, 0.3);
Circle(page, 300, 450, 30);
Fill(page);
Images
var img := LoadJpeg(doc, '/tmp/photo.jpg');
var iw := GetImageWidth(img);
var ih := GetImageHeight(img);
{ Draw image preserving aspect ratio, max 400pt wide }
var scale := 400.0 / iw;
DrawImage(page, img, 50, 300, 400, ih * scale);
{ PNG with transparency }
var logo := LoadPng(doc, '/tmp/logo.png');
DrawImage(page, logo, 450, 780, 80, 40);
Bookmarks (Outlines)
var page1 := AddPage(doc);
var page2 := AddPage(doc);
var page3 := AddPage(doc);
var ch1 := AddOutline(doc, 'Chapter 1');
SetOutlineDest(doc, ch1, page1);
var ch2 := AddOutline(doc, 'Chapter 2');
SetOutlineDest(doc, ch2, page2);
var sec := AddChildOutline(doc, ch2, 'Section 2.1');
SetOutlineDest(doc, sec, page3);
API Reference
Document
| Function | Description |
| NewDoc | Create a new PDF document. Returns TPDFDoc. |
| FreeDoc(Doc) | Free document and all resources. |
| SaveToFile(Doc, Path) | Write PDF to file. Returns True on success. |
| SaveToBytes(Doc) | Return PDF as a byte string. |
| EnableCompression(Doc) | Enable deflate compression for all streams. |
| SetTitle/Author/Subject/Keywords(Doc, S) | Set metadata fields. |
Pages
| Function | Description |
| AddPage(Doc) | Append a new page. Returns TPDFPage. |
| InsertPage(Doc, Target) | Insert page before Target. |
| SetPageSize(Page, Size, Dir) | Standard size (pdfA4, pdfLetter, …) and orientation. |
| SetPageCustomSize(Page, W, H) | Custom size in points. |
| GetPageWidth/Height(Page) | Page dimensions in points. |
Fonts & Text
| Function | Description |
| GetFont(Doc, Name) | Load built-in font (Helvetica, Times-Roman, Courier, …). |
| LoadTTFFont(Doc, Path, Embed) | Load TrueType font from .ttf file. |
| SetFont(Page, Font, Size) | Set current font and size. |
| TextWidth(Page, Text) | Measure text width in points for the current font. |
| TextOut(Page, X, Y, Text) | Output text at absolute position. |
| TextRect(Page, L, T, R, B, Text, Align) | Wrapped text in a rectangle. |
| BeginText / EndText(Page) | Enter/exit text mode for manual positioning. |
| MoveTextPos(Page, X, Y) | Move text cursor to absolute position. |
| ShowText(Page, Text) | Output text at current cursor. |
| NextLine(Page) | Move cursor to next line using current leading. |
| SetTextLeading(Page, N) | Set line height in points. |
Colors & Graphics
| Function | Description |
| SetFillColorRGB(Page, R, G, B) | Fill color for text and shapes. 0.0–1.0 per channel. |
| SetStrokeColorRGB(Page, R, G, B) | Stroke color for lines and borders. |
| SetGrayFill(Page, Gray) | Grayscale fill (0=black, 1=white). |
| SetLineWidth(Page, W) | Line width in points. |
| SetLineCap / SetLineJoin(Page, Style) | Cap and join style. |
| SetDash(Page, Pattern, Phase) | Dash pattern for lines. |
| MoveTo / LineTo(Page, X, Y) | Path construction. |
| CurveTo(Page, X1,Y1,X2,Y2,X3,Y3) | Cubic Bezier curve. |
| Stroke / Fill / FillStroke(Page) | Paint the current path. |
| DrawLine(Page, X1,Y1,X2,Y2) | Shortcut: draw and stroke a line. |
| Rectangle / FillRect(Page, X,Y,W,H) | Rectangle (outline or filled). |
| Circle / Ellipse(Page, X,Y,R…) | Circle or ellipse. |
Images & Bookmarks
| Function | Description |
| LoadJpeg / LoadPng(Doc, Path) | Load image from file. Returns TPDFImage. |
| LoadJpegBytes(Doc, Data) | Load JPEG from in-memory bytes. |
| DrawImage(Page, Img, X,Y,W,H) | Draw image at position with given size in points. |
| GetImageWidth / Height(Img) | Image pixel dimensions. |
| AddOutline(Doc, Title) | Add root bookmark. Returns handle. |
| AddChildOutline(Doc, Parent, Title) | Add nested bookmark. |
| SetOutlineDest(Doc, Outline, Page) | Link bookmark to a page. |