pdf-rs / pdf

Rust library to read, manipulate and write PDF files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Page rect (media_box, crop_box) values being transposed

bellwether-softworks opened this issue · comments

I'm attempting to use the current (7a3f761) version of the library to split a multi-page PDF into several one-page documents. In the course of doing so, I'm noting downstream that the crop box is incorrect, which causes problems in some applications.

To reproduce: using the attached .pdf file in conjunction with the following code, the console output will reflect a change in the media_box() call between the input document and the output.

use pdf::{
    build::{CatalogBuilder, Importer, PageBuilder, PdfBuilder},
    file::FileOptions,
};

fn main() {
    let pdf_path = "bob.pdf";
    let output_path = "out.pdf";
    let old_file = FileOptions::cached().open(pdf_path).unwrap();

    if let Some(Ok(page)) = old_file.pages().next() {
        let mut builder = PdfBuilder::new(FileOptions::cached());
        let mut importer = Importer::new(old_file.resolver(), &mut builder.storage);

        eprintln!("Before: {:?}", page.media_box());

        let new_page = PageBuilder::clone_page(&*page, &mut importer).unwrap();
        let catalog = CatalogBuilder::from_pages(vec![new_page]);
        let data = builder.build(catalog).unwrap();

        std::fs::write(output_path, data).unwrap();
    }

    let new_file = FileOptions::cached().open(output_path).unwrap();

    if let Some(Ok(page)) = new_file.pages().next() {
        eprintln!("After: {:?}", page.media_box());
    };
}

bob.pdf

When testing locally, I see the following output:

Before: Ok(Rect { left: 0.0, bottom: 0.0, right: 612.0, top: 792.0 })
After: Ok(Rect { left: 0.0, bottom: 792.0, right: 612.0, top: 0.0 })

Heh. nice catch.