Part 1 of 1

Building a PDF Creation Library in MQL5 (Part1): Writing a PDF by Hand

Building a PDF Creation Library in MQL5 (Part1): Writing a PDF by Hand

Why this article was written

Every trading tool eventually has to hand a document to a human being — a statement for an investor, a signed trade ticket, a monthly report, a record for a prop-firm review. MetaTrader's HTML statement survives until someone prints it or opens it on a phone, and then the columns shift and page breaks land in the middle of tables.

PDF does not move. But MQL5 has no PDF function, and using an external library means a DLL — which costs you Market validation and the trust of anyone who has to install it.

So this article does the thing everyone assumes is impossible: it writes a PDF by hand, in a text editor, and explains every character. No library, no DLL, no dependencies.

What is inside

A PDF is a text file. Open a generated PDF in Notepad and the skeleton is plain, readable text. Every PDF ever made has the same five regions in the same order — header, body, cross-reference table, trailer, %%EOF — and four of them are near-identical boilerplate. Only the body carries your document.

The eight kinds of value. The entire format is built from booleans, numbers, strings, names, arrays, dictionaries, streams and null. The article spends real time on the distinction that trips everyone up: /Helvetica (a name — an identifier the format understands) is not (Helvetica) (a string — text to print).

Indirect objects. Why the body is a flat, numbered list rather than a nested tree, and how objects reference each other by number the way rows in a database table do. A page does not contain its content; it names it.

The four objects every page needs. Catalog, Pages, Page and Contents — the minimum skeleton that makes a viewer open the file.

Two files, typed line by line. A 592-byte hello.pdf, then an 885-byte trade ticket. Both open in Acrobat, Chrome, Preview and on a phone.

The content stream. The operators that actually put text on the page, and how coordinates work.

The xref table and trailer. The index at the bottom that says where each object starts — and why getting these byte offsets wrong is the single most common reason a generated PDF fails to open.

Then, from MQL5. A script that writes the same files from code, plus a section on what the viewer does when something is wrong and how to read its complaints. The article ends with a one-page cheat card and the project structure the rest of the series builds on.

What you can do after reading it

Write a valid PDF from MQL5 without any library, and — more importantly — debug one when it does not open.

More in this series

Generating real PDF documents from MetaTrader in pure MQL5 — statements, trade tickets and performance reports — with no DLL and no external library. About this series

  1. Part 1Writing a PDF by Hand — you are hereSeptember 24, 2026

Continue reading on MQL5.com

The full article with complete source code is published on MQL5.com.

Read the full article