write_object_streams.json

The JSON file of the “Making the File Smaller” example. One document written twice: once uncompressed, once compressed. The page gives both file sizes and what the compression saved.

JSON 345 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 creates 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 figures the library counts for itself. On the first page they are three weights: what the plain file weighs, what the packed one weighs, and the difference between them. On the second they are the seven paragraphs of figures the library measures by writing the same document other ways — at other compression levels, with and without its information dictionary, as a dry run, with a cover put in front — or reads back off it. The library reads them off the files it writes, 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
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
{
  "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,
      "pages": [
        1
      ]
    },
    {
      "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,
      "pages": [
        1
      ]
    },
    {
      "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,
      "pages": [
        1
      ]
    },
    {
      "type": "text",
      "rect": {
        "llx": 60,
        "lly": 586.8,
        "urx": 535,
        "ury": 600
      },
      "content": [
        "The same document, weighed twice"
      ],
      "font": "DejaVuSans",
      "font_size": 11,
      "pages": [
        1
      ]
    },
    {
      "type": "text",
      "rect": {
        "llx": 60,
        "lly": 562,
        "urx": 535,
        "ury": 574
      },
      "content": [
        "Spelled out"
      ],
      "font": "DejaVuSans",
      "font_size": 10,
      "pages": [
        1
      ]
    },
    {
      "type": "text",
      "rect": {
        "llx": 220,
        "lly": 562,
        "urx": 535,
        "ury": 574
      },
      "content": [
        "22 877 bytes"
      ],
      "font": "DejaVuSans",
      "font_size": 10,
      "pages": [
        1
      ]
    },
    {
      "type": "text",
      "rect": {
        "llx": 60,
        "lly": 542,
        "urx": 535,
        "ury": 554
      },
      "content": [
        "Packed"
      ],
      "font": "DejaVuSans",
      "font_size": 10,
      "pages": [
        1
      ]
    },
    {
      "type": "text",
      "rect": {
        "llx": 220,
        "lly": 542,
        "urx": 535,
        "ury": 554
      },
      "content": [
        "18 489 bytes"
      ],
      "font": "DejaVuSans",
      "font_size": 10,
      "pages": [
        1
      ]
    },
    {
      "type": "text",
      "rect": {
        "llx": 60,
        "lly": 522,
        "urx": 535,
        "ury": 534
      },
      "content": [
        "Saved"
      ],
      "font": "DejaVuSans",
      "font_size": 10,
      "pages": [
        1
      ]
    },
    {
      "type": "text",
      "rect": {
        "llx": 220,
        "lly": 522,
        "urx": 535,
        "ury": 534
      },
      "content": [
        "4 388 bytes"
      ],
      "font": "DejaVuSans",
      "font_size": 10,
      "pages": [
        1
      ]
    },
    {
      "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,
      "pages": [
        1
      ]
    },
    {
      "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,
      "pages": [
        1
      ]
    },
    {
      "type": "flow",
      "rect": {
        "llx": 60.0,
        "lly": 740.0,
        "urx": 535.0,
        "ury": 758.4
      },
      "content": "The same document, weighed other ways",
      "font": "DejaVuSans",
      "font_size": 18.0,
      "first_baseline": {
        "points": 0
      },
      "pages": [
        2
      ]
    },
    {
      "type": "flow",
      "rect": {
        "llx": 60.0,
        "lly": 716.0,
        "urx": 535.0,
        "ury": 748.0
      },
      "content": "With compression off it weighs 31 242 bytes; at level 1, 26 906; at level 6, the default, 22 877; at level 9, 22 866.",
      "font": "DejaVuSans",
      "font_size": 10.0,
      "leading": 14.0,
      "pages": [
        2
      ]
    },
    {
      "type": "flow",
      "rect": {
        "llx": 60.0,
        "lly": 678.0,
        "urx": 535.0,
        "ury": 710.0
      },
      "content": "Given a title, it weighs 22 971 bytes with its information dictionary and 25 418 without it, the title then said in its metadata alone; the dictionary is written: no.",
      "font": "DejaVuSans",
      "font_size": 10.0,
      "leading": 14.0,
      "pages": [
        2
      ]
    },
    {
      "type": "flow",
      "rect": {
        "llx": 60.0,
        "lly": 640.0,
        "urx": 535.0,
        "ury": 672.0
      },
      "content": "A dry run, which writes nothing, counts 24 pages and 56 objects, written as PDF 1.7 though PDF 1.4 would do, and 0 warnings.",
      "font": "DejaVuSans",
      "font_size": 10.0,
      "leading": 14.0,
      "pages": [
        2
      ]
    },
    {
      "type": "flow",
      "rect": {
        "llx": 60.0,
        "lly": 602.0,
        "urx": 535.0,
        "ury": 634.0
      },
      "content": "Written with its warnings reported it weighs 22 877 bytes, and 22 877 written to a stream the same way; warnings: 0.",
      "font": "DejaVuSans",
      "font_size": 10.0,
      "leading": 14.0,
      "pages": [
        2
      ]
    },
    {
      "type": "flow",
      "rect": {
        "llx": 60.0,
        "lly": 578.0,
        "urx": 535.0,
        "ury": 596.0
      },
      "content": "Packed into object streams: no; laid out first page first: no; the version it needs: PDF 1.4.",
      "font": "DejaVuSans",
      "font_size": 10.0,
      "leading": 14.0,
      "pages": [
        2
      ]
    },
    {
      "type": "flow",
      "rect": {
        "llx": 60.0,
        "lly": 540.0,
        "urx": 535.0,
        "ury": 572.0
      },
      "content": "With a cover put in front and a bookmark to it put first, it holds 25 pages, and its first bookmark reads “Cover”.",
      "font": "DejaVuSans",
      "font_size": 10.0,
      "leading": 14.0,
      "pages": [
        2
      ]
    },
    {
      "type": "flow",
      "rect": {
        "llx": 60.0,
        "lly": 488.0,
        "urx": 535.0,
        "ury": 534.0
      },
      "content": "A line of four hundred characters measured inside a measuring pass costs the file nothing: 7 602 bytes, and 7 602 with the pass held open by hand, against 24 979 when measured outside one.",
      "font": "DejaVuSans",
      "font_size": 10.0,
      "leading": 14.0,
      "pages": [
        2
      ]
    }
  ]
}