You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Check out the new article: Building a PDF Creation Library in MQL5 (Part 3): The Byte-Exact Writer.
You can build a PDF by hand, but once a file grows past a few objects every edit becomes a minefield: insert one character and /Length, the xref offsets and startxref are suddenly wrong. The format validates none of those numbers, so a single change can produce a file that one viewer silently repairs, another displays as blank, and a third refuses to open. Part 1 typed such a file by hand, and Part 2 showed how far the damage from two extra characters reaches, finishing with a validator that reports it. What we still need is a way to generate a PDF from MQL5 in one pass, so that a change in the text, the page count or the size of a stream never forces manual byte arithmetic and never leaves a forgotten literal behind in the source.
This part removes the problem rather than detecting it. The idea is small enough to state in one line, and the rest of the article is the consequences of it:
Never calculate a byte offset. Ask the buffer where it is.
A program that appends bytes to an array already knows how many bytes it has written. That number is the current byte position. If an object records it at the instant the object begins, the offset is never a guess: it cannot go stale when something above it changes, and it cannot be forgotten when the text is edited.
Two classes result from it. CPdfBuffer is a growable array of bytes and the only class in the library that stores them. CPdfWriter allocates object numbers, records where each object landed, and writes the cross-reference table based on what it observed. Together they are about 300 lines, and they are the foundation on which every later part is built.
The take-away is a tool you can use today: a script that turns any ASCII text — a log, a settings dump, a list of symbols and spreads — into a paginated PDF.
Figure 1. An account summary printed to PDF by this part's second demo.
Author: Sandro Begashvili