Read the MediaBox, CropBox, and BleedBox from any PDF page. Convert between points and millimetres or inches for printing and layout workflows.
use pdfluent::PdfDocument;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let doc = PdfDocument::open("drawing.pdf")?;
for (i, page) in doc.pages().enumerate() {
let size = page.media_box();
println!(
"Page {}: {:.1} x {:.1} pt ({:.1} x {:.1} mm)",
i + 1,
size.width(),
size.height(),
size.width() * 25.4 / 72.0,
size.height() * 25.4 / 72.0,
);
}
Ok(())
}Add the pdfluent crate to Cargo.toml.
[dependencies]
pdfluent = "0.9"Open the document and iterate or index pages directly.
use pdfluent::PdfDocument;
let doc = PdfDocument::open("blueprint.pdf")?;
let page = doc.page(0)?; // first pageThe MediaBox defines the full physical extent of the page in PDF user units (points). 1 point = 1/72 inch.
let media = page.media_box();
println!("Width: {:.2} pt", media.width());
println!("Height: {:.2} pt", media.height());
println!("Origin: ({:.2}, {:.2})", media.x1, media.y1);CropBox is the visible page area. BleedBox is for print bleed. Both fall back to the MediaBox if not set.
let crop = page.crop_box();
println!("CropBox: {:.1} x {:.1} pt", crop.width(), crop.height());
let bleed = page.bleed_box();
println!("BleedBox: {:.1} x {:.1} pt", bleed.width(), bleed.height());PDF points convert to mm with factor 25.4/72 and to inches with factor 1/72.
fn pt_to_mm(pt: f32) -> f32 { pt * 25.4 / 72.0 }
fn pt_to_inch(pt: f32) -> f32 { pt / 72.0 }
let media = page.media_box();
println!(
"Page size: {:.1} x {:.1} mm ({:.3} x {:.3} in)",
pt_to_mm(media.width()),
pt_to_mm(media.height()),
pt_to_inch(media.width()),
pt_to_inch(media.height()),
);No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.
Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.
Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.