pdf-manip 1.2.0

PDF manipulation: pages, encryption, watermarks, optimization, bookmarks
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
//! Image downsampling for PDF optimization.
//!
//! Scans embedded images and downsamples those above a target DPI
//! to reduce file size. Re-encodes as JPEG via DCTDecode.

// Copyright (c) 2026 Innovation Trigger B.V.
//
// PDFluent is available under two licences, at your option: the GNU AGPLv3, or
// the PDFluent Commercial Licence. See the LICENSE file in this repository --
// that file travels with the copy you received, which a URL does not.

use crate::error::{ManipError, Result};
use crate::flate_decode::decode_zlib;
use crate::image_insert::checked_image_allocation_len;
use image::imageops::FilterType;
use image::{DynamicImage, RgbImage};
use lopdf::content::Content;
use lopdf::{dictionary, Document, Object, ObjectId, Stream};
use std::collections::HashMap;

/// Configuration for image downsampling.
#[derive(Debug, Clone)]
pub struct DownsampleConfig {
    /// Target DPI threshold — images above this will be downsampled.
    pub target_dpi: u32,
    /// JPEG output quality (1–100).
    pub jpeg_quality: u8,
    /// Skip images smaller than this width in pixels.
    pub min_width: u32,
}

impl Default for DownsampleConfig {
    fn default() -> Self {
        Self {
            target_dpi: 150,
            jpeg_quality: 80,
            min_width: 64,
        }
    }
}

/// Report from a downsampling pass.
#[derive(Debug, Clone)]
pub struct DownsampleReport {
    /// Number of images inspected.
    pub images_inspected: usize,
    /// Number of images downsampled.
    pub images_downsampled: usize,
    /// Total bytes saved (approximate).
    pub bytes_saved: i64,
}

/// Downsample all embedded images above `target_dpi` in the document.
pub fn downsample_images(
    doc: &mut Document,
    config: &DownsampleConfig,
) -> Result<DownsampleReport> {
    let mut report = DownsampleReport {
        images_inspected: 0,
        images_downsampled: 0,
        bytes_saved: 0,
    };

    let display_sizes = collect_image_display_sizes(doc);

    let image_ids: Vec<ObjectId> = doc
        .objects
        .keys()
        .copied()
        .filter(|id| is_image_xobject(doc, *id))
        .collect();

    for id in image_ids {
        report.images_inspected += 1;

        let (pixel_w, pixel_h, components, filter, content_len) = {
            let Some(Object::Stream(stream)) = doc.objects.get(&id) else {
                continue;
            };
            let w = get_int(&stream.dict, b"Width").unwrap_or(0) as u32;
            let h = get_int(&stream.dict, b"Height").unwrap_or(0) as u32;
            let bpc = get_int(&stream.dict, b"BitsPerComponent").unwrap_or(8);
            if bpc != 8 || w == 0 || h == 0 {
                continue;
            }
            let cs = get_name(&stream.dict, b"ColorSpace");
            let comp = match cs.as_deref() {
                Some("DeviceGray") => 1u32,
                Some("DeviceCMYK") => 4,
                _ => 3,
            };
            let f = get_name(&stream.dict, b"Filter");
            (w, h, comp, f, stream.content.len())
        };

        if pixel_w < config.min_width {
            continue;
        }

        let current_dpi = if let Some((dw, dh)) = display_sizes.get(&id).copied() {
            if dw > 0.0 && dh > 0.0 {
                let dpi_x = (pixel_w as f64) / (dw / 72.0);
                let dpi_y = (pixel_h as f64) / (dh / 72.0);
                dpi_x.max(dpi_y)
            } else {
                continue;
            }
        } else {
            300.0
        };

        if current_dpi <= config.target_dpi as f64 {
            continue;
        }

        let scale = config.target_dpi as f64 / current_dpi;
        let new_w = ((pixel_w as f64 * scale).round() as u32).max(1);
        let new_h = ((pixel_h as f64 * scale).round() as u32).max(1);
        let expected_cmyk_len = if components == 4 {
            Some(checked_image_allocation_len(pixel_w, pixel_h, 4)?)
        } else {
            None
        };

        let raw_pixels = {
            let Some(Object::Stream(stream)) = doc.objects.get(&id) else {
                continue;
            };
            match decode_image_data(
                &stream.content,
                filter.as_deref(),
                pixel_w,
                pixel_h,
                components,
            ) {
                Ok(data) => data,
                Err(_) => continue,
            }
        };

        let img = match components {
            1 => image::GrayImage::from_raw(pixel_w, pixel_h, raw_pixels)
                .map(DynamicImage::ImageLuma8),
            3 => RgbImage::from_raw(pixel_w, pixel_h, raw_pixels).map(DynamicImage::ImageRgb8),
            4 => {
                let Some(expected_cmyk_len) = expected_cmyk_len else {
                    continue;
                };
                if raw_pixels.len() != expected_cmyk_len {
                    continue;
                }

                let mut rgb_data =
                    Vec::with_capacity(checked_image_allocation_len(pixel_w, pixel_h, 3)?);
                for chunk in raw_pixels.chunks(4) {
                    let c = chunk[0] as f32 / 255.0;
                    let m = chunk[1] as f32 / 255.0;
                    let y = chunk[2] as f32 / 255.0;
                    let k = chunk[3] as f32 / 255.0;
                    rgb_data.push(((1.0 - c) * (1.0 - k) * 255.0) as u8);
                    rgb_data.push(((1.0 - m) * (1.0 - k) * 255.0) as u8);
                    rgb_data.push(((1.0 - y) * (1.0 - k) * 255.0) as u8);
                }
                RgbImage::from_raw(pixel_w, pixel_h, rgb_data).map(DynamicImage::ImageRgb8)
            }
            _ => None,
        };

        let Some(img) = img else { continue };

        let resized = img.resize_exact(new_w, new_h, FilterType::Lanczos3);
        let rgb = resized.to_rgb8();

        let jpeg_data = match encode_jpeg(&rgb) {
            Ok(data) => data,
            Err(_) => continue,
        };

        let bytes_before = content_len as i64;
        let bytes_after = jpeg_data.len() as i64;

        if bytes_after >= bytes_before {
            continue;
        }

        let new_dict = dictionary! {
            "Type" => "XObject",
            "Subtype" => "Image",
            "Width" => Object::Integer(new_w as i64),
            "Height" => Object::Integer(new_h as i64),
            "BitsPerComponent" => Object::Integer(8),
            "ColorSpace" => "DeviceRGB",
            "Filter" => "DCTDecode",
            "Length" => Object::Integer(jpeg_data.len() as i64),
        };
        doc.objects
            .insert(id, Object::Stream(Stream::new(new_dict, jpeg_data)));

        report.images_downsampled += 1;
        report.bytes_saved += bytes_before - bytes_after;
    }

    Ok(report)
}

fn is_image_xobject(doc: &Document, id: ObjectId) -> bool {
    if let Some(Object::Stream(stream)) = doc.objects.get(&id) {
        get_name(&stream.dict, b"Subtype").as_deref() == Some("Image")
    } else {
        false
    }
}

fn get_int(dict: &lopdf::Dictionary, key: &[u8]) -> Option<i64> {
    match dict.get(key).ok()? {
        Object::Integer(n) => Some(*n),
        _ => None,
    }
}

fn get_name(dict: &lopdf::Dictionary, key: &[u8]) -> Option<String> {
    match dict.get(key).ok()? {
        Object::Name(n) => String::from_utf8(n.clone()).ok(),
        _ => None,
    }
}

/// Collect display sizes of images from page content streams (CTM analysis).
fn collect_image_display_sizes(doc: &Document) -> HashMap<ObjectId, (f64, f64)> {
    let mut sizes = HashMap::new();

    for (_page_num, page_id) in doc.get_pages() {
        let Some(Object::Dictionary(page_dict)) = doc.objects.get(&page_id) else {
            continue;
        };

        let xobject_map = get_page_xobjects(doc, page_dict);
        let Some(content_data) = get_page_content_data(doc, page_dict) else {
            continue;
        };
        let Ok(content) = Content::decode(&content_data) else {
            continue;
        };

        let mut ctm_stack: Vec<[f64; 6]> = vec![[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]];

        for op in &content.operations {
            match op.operator.as_str() {
                "q" => {
                    let current = *ctm_stack.last().unwrap_or(&[1.0, 0.0, 0.0, 1.0, 0.0, 0.0]);
                    ctm_stack.push(current);
                }
                "Q" if ctm_stack.len() > 1 => {
                    ctm_stack.pop();
                }
                "cm" if op.operands.len() >= 6 => {
                    let vals: Vec<f64> = op.operands.iter().filter_map(obj_to_f64).collect();
                    if vals.len() == 6 {
                        if let Some(ctm) = ctm_stack.last_mut() {
                            *ctm = multiply_ctm(ctm, &vals);
                        }
                    }
                }
                "Do" if op.operands.len() == 1 => {
                    if let Object::Name(ref name) = op.operands[0] {
                        let name_str = String::from_utf8_lossy(name);
                        if let Some(&obj_id) = xobject_map.get(name_str.as_ref()) {
                            let ctm = ctm_stack
                                .last()
                                .copied()
                                .unwrap_or([1.0, 0.0, 0.0, 1.0, 0.0, 0.0]);
                            let dw = (ctm[0] * ctm[0] + ctm[2] * ctm[2]).sqrt();
                            let dh = (ctm[1] * ctm[1] + ctm[3] * ctm[3]).sqrt();
                            if dw > 0.0 && dh > 0.0 {
                                sizes.insert(obj_id, (dw, dh));
                            }
                        }
                    }
                }
                _ => {}
            }
        }
    }

    sizes
}

fn multiply_ctm(current: &[f64; 6], new: &[f64]) -> [f64; 6] {
    [
        new[0] * current[0] + new[1] * current[2],
        new[0] * current[1] + new[1] * current[3],
        new[2] * current[0] + new[3] * current[2],
        new[2] * current[1] + new[3] * current[3],
        new[4] * current[0] + new[5] * current[2] + current[4],
        new[4] * current[1] + new[5] * current[3] + current[5],
    ]
}

fn obj_to_f64(obj: &Object) -> Option<f64> {
    match obj {
        Object::Integer(n) => Some(*n as f64),
        Object::Real(n) => Some(*n as f64),
        _ => None,
    }
}

fn get_page_xobjects(doc: &Document, page_dict: &lopdf::Dictionary) -> HashMap<String, ObjectId> {
    let mut map = HashMap::new();
    let res = match page_dict.get(b"Resources").ok() {
        Some(Object::Dictionary(d)) => d,
        Some(Object::Reference(id)) => match doc.objects.get(id) {
            Some(Object::Dictionary(d)) => d,
            _ => return map,
        },
        _ => return map,
    };
    let xobjects = match res.get(b"XObject").ok() {
        Some(Object::Dictionary(d)) => d,
        Some(Object::Reference(id)) => match doc.objects.get(id) {
            Some(Object::Dictionary(d)) => d,
            _ => return map,
        },
        _ => return map,
    };
    for (key, val) in xobjects.iter() {
        if let Object::Reference(id) = val {
            map.insert(String::from_utf8_lossy(key).to_string(), *id);
        }
    }
    map
}

fn get_page_content_data(doc: &Document, page_dict: &lopdf::Dictionary) -> Option<Vec<u8>> {
    match page_dict.get(b"Contents").ok()? {
        Object::Reference(id) => {
            if let Some(Object::Stream(stream)) = doc.objects.get(id) {
                Some(stream.content.clone())
            } else {
                None
            }
        }
        Object::Array(arr) => {
            let mut data = Vec::new();
            for item in arr {
                if let Object::Reference(id) = item {
                    if let Some(Object::Stream(stream)) = doc.objects.get(id) {
                        data.extend_from_slice(&stream.content);
                        data.push(b'\n');
                    }
                }
            }
            Some(data)
        }
        _ => None,
    }
}

fn decode_image_data(
    data: &[u8],
    filter: Option<&str>,
    _width: u32,
    _height: u32,
    _components: u32,
) -> Result<Vec<u8>> {
    match filter {
        Some("FlateDecode") => decode_zlib(data, |e| {
            ManipError::Image(format!("FlateDecode failed: {e}"))
        }),
        Some("DCTDecode") => {
            let img = image::load_from_memory_with_format(data, image::ImageFormat::Jpeg)
                .map_err(|e| ManipError::Image(format!("JPEG decode failed: {e}")))?;
            Ok(img.to_rgb8().into_raw())
        }
        None => Ok(data.to_vec()),
        Some(other) => Err(ManipError::Image(format!(
            "unsupported image filter for downsampling: {other}"
        ))),
    }
}

fn encode_jpeg(img: &RgbImage) -> Result<Vec<u8>> {
    let mut buf = std::io::Cursor::new(Vec::new());
    img.write_to(&mut buf, image::ImageFormat::Jpeg)
        .map_err(|e| ManipError::Image(format!("JPEG encoding failed: {e}")))?;
    Ok(buf.into_inner())
}

#[cfg(test)]
mod tests {
    use super::*;
    use flate2::write::ZlibEncoder;
    use flate2::Compression;
    use std::io::Write;

    fn make_doc_with_image() -> Document {
        let mut doc = Document::with_version("1.7");
        let pages_id = doc.new_object_id();

        let raw_pixels: Vec<u8> = (0..4 * 4 * 3).map(|i| (i % 256) as u8).collect();
        let compressed = {
            let mut enc = ZlibEncoder::new(Vec::new(), Compression::default());
            enc.write_all(&raw_pixels).unwrap();
            enc.finish().unwrap()
        };

        let img_dict = dictionary! {
            "Type" => "XObject",
            "Subtype" => "Image",
            "Width" => Object::Integer(4),
            "Height" => Object::Integer(4),
            "BitsPerComponent" => Object::Integer(8),
            "ColorSpace" => "DeviceRGB",
            "Filter" => "FlateDecode",
            "Length" => Object::Integer(compressed.len() as i64),
        };
        let img_id = doc.add_object(Object::Stream(Stream::new(img_dict, compressed)));

        let content_ops = vec![
            lopdf::content::Operation::new("q", vec![]),
            lopdf::content::Operation::new(
                "cm",
                vec![
                    Object::Real(72.0),
                    Object::Real(0.0),
                    Object::Real(0.0),
                    Object::Real(72.0),
                    Object::Real(0.0),
                    Object::Real(0.0),
                ],
            ),
            lopdf::content::Operation::new("Do", vec![Object::Name(b"Im1".to_vec())]),
            lopdf::content::Operation::new("Q", vec![]),
        ];
        let content_data = lopdf::content::Content {
            operations: content_ops,
        }
        .encode()
        .unwrap();
        let content_id = doc.add_object(Object::Stream(Stream::new(dictionary! {}, content_data)));

        let mut xobject_dict = lopdf::Dictionary::new();
        xobject_dict.set("Im1", Object::Reference(img_id));
        let mut res_dict = lopdf::Dictionary::new();
        res_dict.set("XObject", Object::Dictionary(xobject_dict));

        let page = dictionary! {
            "Type" => "Page",
            "Parent" => Object::Reference(pages_id),
            "MediaBox" => Object::Array(vec![
                Object::Integer(0), Object::Integer(0),
                Object::Integer(612), Object::Integer(792),
            ]),
            "Contents" => Object::Reference(content_id),
            "Resources" => Object::Dictionary(res_dict),
        };
        let page_id = doc.add_object(Object::Dictionary(page));

        let pages = dictionary! {
            "Type" => "Pages",
            "Count" => Object::Integer(1),
            "Kids" => Object::Array(vec![Object::Reference(page_id)]),
        };
        doc.objects.insert(pages_id, Object::Dictionary(pages));

        let catalog = dictionary! {
            "Type" => "Catalog",
            "Pages" => Object::Reference(pages_id),
        };
        let catalog_id = doc.add_object(Object::Dictionary(catalog));
        doc.trailer.set("Root", Object::Reference(catalog_id));

        doc
    }

    fn make_doc_with_cmyk_image(width: u32, height: u32, image_data: Vec<u8>) -> Document {
        let mut doc = Document::with_version("1.7");
        let pages_id = doc.new_object_id();

        let img_dict = dictionary! {
            "Type" => "XObject",
            "Subtype" => "Image",
            "Width" => Object::Integer(width as i64),
            "Height" => Object::Integer(height as i64),
            "BitsPerComponent" => Object::Integer(8),
            "ColorSpace" => "DeviceCMYK",
            "Length" => Object::Integer(image_data.len() as i64),
        };
        let img_id = doc.add_object(Object::Stream(Stream::new(img_dict, image_data)));

        let content_ops = vec![
            lopdf::content::Operation::new("q", vec![]),
            lopdf::content::Operation::new(
                "cm",
                vec![
                    Object::Real(72.0),
                    Object::Real(0.0),
                    Object::Real(0.0),
                    Object::Real(72.0),
                    Object::Real(0.0),
                    Object::Real(0.0),
                ],
            ),
            lopdf::content::Operation::new("Do", vec![Object::Name(b"Im1".to_vec())]),
            lopdf::content::Operation::new("Q", vec![]),
        ];
        let content_data = lopdf::content::Content {
            operations: content_ops,
        }
        .encode()
        .unwrap();
        let content_id = doc.add_object(Object::Stream(Stream::new(dictionary! {}, content_data)));

        let mut xobject_dict = lopdf::Dictionary::new();
        xobject_dict.set("Im1", Object::Reference(img_id));
        let mut res_dict = lopdf::Dictionary::new();
        res_dict.set("XObject", Object::Dictionary(xobject_dict));

        let page = dictionary! {
            "Type" => "Page",
            "Parent" => Object::Reference(pages_id),
            "MediaBox" => Object::Array(vec![
                Object::Integer(0), Object::Integer(0),
                Object::Integer(612), Object::Integer(792),
            ]),
            "Contents" => Object::Reference(content_id),
            "Resources" => Object::Dictionary(res_dict),
        };
        let page_id = doc.add_object(Object::Dictionary(page));

        let pages = dictionary! {
            "Type" => "Pages",
            "Count" => Object::Integer(1),
            "Kids" => Object::Array(vec![Object::Reference(page_id)]),
        };
        doc.objects.insert(pages_id, Object::Dictionary(pages));

        let catalog = dictionary! {
            "Type" => "Catalog",
            "Pages" => Object::Reference(pages_id),
        };
        let catalog_id = doc.add_object(Object::Dictionary(catalog));
        doc.trailer.set("Root", Object::Reference(catalog_id));

        doc
    }

    #[test]
    fn test_downsample_default_config() {
        let mut doc = make_doc_with_image();
        let config = DownsampleConfig {
            target_dpi: 2,
            jpeg_quality: 80,
            min_width: 1,
        };
        let report = downsample_images(&mut doc, &config).unwrap();
        assert_eq!(report.images_inspected, 1);
    }

    #[test]
    fn test_no_downsample_below_target() {
        let mut doc = make_doc_with_image();
        let config = DownsampleConfig {
            target_dpi: 300,
            jpeg_quality: 80,
            min_width: 1,
        };
        let report = downsample_images(&mut doc, &config).unwrap();
        assert_eq!(report.images_inspected, 1);
        assert_eq!(report.images_downsampled, 0);
    }

    #[test]
    fn test_skip_small_images() {
        let mut doc = make_doc_with_image();
        let config = DownsampleConfig {
            target_dpi: 2,
            jpeg_quality: 80,
            min_width: 100,
        };
        let report = downsample_images(&mut doc, &config).unwrap();
        assert_eq!(report.images_downsampled, 0);
    }

    #[test]
    fn test_empty_document() {
        let mut doc = Document::with_version("1.7");
        let pages_id = doc.new_object_id();
        let pages = dictionary! {
            "Type" => "Pages",
            "Count" => Object::Integer(0),
            "Kids" => Object::Array(vec![]),
        };
        doc.objects.insert(pages_id, Object::Dictionary(pages));
        let catalog = dictionary! {
            "Type" => "Catalog",
            "Pages" => Object::Reference(pages_id),
        };
        let catalog_id = doc.add_object(Object::Dictionary(catalog));
        doc.trailer.set("Root", Object::Reference(catalog_id));

        let report = downsample_images(&mut doc, &DownsampleConfig::default()).unwrap();
        assert_eq!(report.images_inspected, 0);
    }

    #[test]
    fn test_reject_oversized_cmyk_image() {
        let mut doc = make_doc_with_cmyk_image(
            crate::image_insert::MAX_IMAGE_DIMENSION,
            crate::image_insert::MAX_IMAGE_DIMENSION,
            vec![0, 0, 0, 0],
        );
        let config = DownsampleConfig {
            target_dpi: 2,
            jpeg_quality: 80,
            min_width: 1,
        };

        let err = downsample_images(&mut doc, &config).unwrap_err();
        assert!(matches!(
            err,
            ManipError::ImageTooLarge {
                width,
                height,
                bytes_per_pixel: 4,
                ..
            } if width == crate::image_insert::MAX_IMAGE_DIMENSION
                && height == crate::image_insert::MAX_IMAGE_DIMENSION
        ));
    }
}