Making the file smaller

The same document written twice, plainly and packed, with what each one weighs and what the packing saved.

JSON write_object_streams.json 95 lines

This one is not a program. It is the request the document server is sent: the same page, asked for over the network by an application that writes no PDF code at all.

This request writes out the three weights the library counts for itself: what the plain file weighs, what the packed one weighs, and the difference between them. The library reads them off the two files it has just written, so they follow whatever goes into a document; here they are typed in, and would have to be typed in again.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{
  "compression": {"object_streams": true},
  "items": [
    {
      "type": "text",
      "rect": {"llx": 60, "lly": 758.4, "urx": 535, "ury": 780},
      "content": ["What packing a document saves"],
      "font": "DejaVuSans",
      "font_size": 18
    },
    {
      "type": "text",
      "rect": {"llx": 60, "lly": 664, "urx": 535, "ury": 748},
      "content": ["A PDF is mostly dictionaries — the catalogue, the page tree, every page, every font, every annotation — and a dictionary is not a stream, so each one is written out in full and none of them is compressed.\nObject streams gather them into streams that are compressed together, and the table stating where every object lives becomes a stream too. The document a reader sees is the same one either way."],
      "font": "DejaVuSans",
      "font_size": 10,
      "leading": 14
    },
    {
      "type": "text",
      "rect": {"llx": 60, "lly": 622, "urx": 535, "ury": 636},
      "content": ["The document weighed here holds 24 pages of text, each with a heading of its own."],
      "font": "DejaVuSans",
      "font_size": 10,
      "leading": 14
    },
    {
      "type": "text",
      "rect": {"llx": 60, "lly": 586.8, "urx": 535, "ury": 600},
      "content": ["The same document, weighed twice"],
      "font": "DejaVuSans",
      "font_size": 11
    },

    {
      "type": "text",
      "rect": {"llx": 60, "lly": 562, "urx": 535, "ury": 574},
      "content": ["Spelled out"],
      "font": "DejaVuSans",
      "font_size": 10
    },
    {
      "type": "text",
      "rect": {"llx": 220, "lly": 562, "urx": 535, "ury": 574},
      "content": ["50 177 bytes"],
      "font": "DejaVuSans",
      "font_size": 10
    },
    {
      "type": "text",
      "rect": {"llx": 60, "lly": 542, "urx": 535, "ury": 554},
      "content": ["Packed"],
      "font": "DejaVuSans",
      "font_size": 10
    },
    {
      "type": "text",
      "rect": {"llx": 220, "lly": 542, "urx": 535, "ury": 554},
      "content": ["45 826 bytes"],
      "font": "DejaVuSans",
      "font_size": 10
    },
    {
      "type": "text",
      "rect": {"llx": 60, "lly": 522, "urx": 535, "ury": 534},
      "content": ["Saved"],
      "font": "DejaVuSans",
      "font_size": 10
    },
    {
      "type": "text",
      "rect": {"llx": 220, "lly": 522, "urx": 535, "ury": 534},
      "content": ["4 351 bytes"],
      "font": "DejaVuSans",
      "font_size": 10
    },

    {
      "type": "text",
      "rect": {"llx": 60, "lly": 444, "urx": 535, "ury": 486},
      "content": ["Packing this document saved 8 per cent of it. What it saves goes up with the number of pages, because a page costs a handful of dictionaries and dictionaries are what packing compresses."],
      "font": "DejaVuSans",
      "font_size": 10,
      "leading": 14
    },
    {
      "type": "text",
      "rect": {"llx": 60, "lly": 396, "urx": 535, "ury": 424},
      "content": ["This very file is packed, so a reader that opened it has read one of the two documents being weighed."],
      "font": "DejaVuSans",
      "font_size": 10,
      "leading": 14
    }
  ]
}