Embed JPEG, PNG, or WebP images at a specific position and size on any PDF page.
use pdfluent::PdfDocument;
use pdfluent::parity::{ImageInsert, InsertImageFormat};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut doc = PdfDocument::open("report.pdf")?;
let bytes = std::fs::read("logo.png")?;
doc.insert_image(ImageInsert::new(bytes, InsertImageFormat::Png, 0, 50.0, 700.0, 120.0, 40.0))?;
doc.save("with-logo.pdf")?;
Ok(())
}Image embedding is in the base crate. The image feature adds format conversion helpers.
# Cargo.toml
[dependencies]
pdfluent = "1.0.0-beta.8"Image::from_file() reads JPEG, PNG, or WebP. Image::from_bytes() accepts the raw image bytes if you are reading from memory or a network source.
// Load the image bytes (PNG or JPEG)
let bytes = std::fs::read("logo.png")?;
println!("{} bytes loaded", bytes.len());page_mut(n) returns a mutable handle to page n. PDF page indices are zero-based.
use pdfluent::parity::{ImageInsert, InsertImageFormat};
// Place on the first page (index 0). Coordinates are points from bottom-left.
let insert = ImageInsert::new(bytes.clone(), InsertImageFormat::Png, 0, 50.0, 700.0, 150.0, 60.0);PDF uses a coordinate system where (0, 0) is the bottom-left corner. Measurements are in points (1 pt = 1/72 inch). A4 is 595 x 842 pt, US Letter is 612 x 792 pt.
doc.insert_image(insert)?;Use ImagePosition::fit_width() to scale the image to a given width while preserving the aspect ratio.
use pdfluent::parity::{ImageInsert, InsertImageFormat};
// Insert with reduced opacity (e.g. a watermark-style logo)
doc.insert_image(
ImageInsert::new(bytes, InsertImageFormat::Png, 0, 50.0, 700.0, 150.0, 60.0)
.with_opacity(0.85),
)?;
doc.save("document-with-logo.pdf")?;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.