write_object_streams.json

The JSON file of the “Making the file smaller” example. The same document written twice, plainly and packed, with what each one weighs and what the packing saved.

JSON 95 lines

This file is a request: an application sends it over the network to the server, which creates the PDF and sends it back. It makes the same page as the Rust and Python versions of this example. A request names every file it uses, never its content; the call carries the request and the files together. A font the server already holds, such as DejaVuSans, is named with nothing attached. Send the request to the address the API page gives.

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.

What this example is for

What a PDF weighs shows when it is being sent to two thousand people, or still kept decades from now, or opened over a telephone connection at the far end of a field. A document spends a large share of itself on bookkeeping — where each page begins, which font goes where, which area is clickable — and every character of it is written out in plain view.

What this example shows

 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": ["22 810 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": ["18 459 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 19 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
    }
  ]
}