Building a PDF writer
A PDF file is basically a set of object serialised in a specific format similar to PostScript. Our initial goal is to be able to place text in a PDF by writing the required objects in the correct format but today we’ll just start with the overall layout of the PDF file source.
The first thing we write to the file is the PDF header which consists of the PDF version and 4 binary characters to ensure readers know the file contains binary.
%PDF-1.4
%ÿÿÿÿ
After that we add the objects which will lay out the document. Each object begins with an id followed by a dictionary and an optional stream. We won’t be working with streams this round.
1 0 obj
<<
/Type /Catalog
/Pages 2 0 R
>>
endobj
Following the objects we put down the xref
. The xref
is an index of the objects in the file to help readers locate them quickly (and add a bit more info in PDF files that have been modified).
xref
...