Your first PDF in Rust
You’ll write hello.pdf, one A4 page that says “Hello, world!”, from a small Rust program. It takes about five minutes, most of it for the first build.
Make a project
Section titled “Make a project”You need Rust 1.97 or newer. Create a project and add the laminapdf crate:
cargo new hello-pdf && cd hello-pdfcargo add laminapdf@0.9.0This adds the version these docs describe. Cargo reads it as this release or a later one in the same 0.x line, which the stability policy promises needs no change to your code. Install covers the crate’s features.
Write the page
Section titled “Write the page”Replace src/main.rs with this program. It builds the page and writes it to hello.pdf:
//! The smallest document: one A4 page with a line of text in a standard 14 font.//!//! ```sh//! cargo run -p laminapdf --example hello -- [out-dir] # writes <out-dir>/hello.pdf (default target/examples-out)//! ```
use std::error::Error;use std::path::PathBuf;
use laminapdf::{BuildOptions, Builder, PageSize, PointPt, Pt, SaveOptions, Standard14};
fn main() -> Result<(), Box<dyn Error>> { let out = PathBuf::from(std::env::args().nth(1).unwrap_or_else(|| "target/examples-out".into())); std::fs::create_dir_all(&out)?; std::fs::write(out.join("hello.pdf"), build(BuildOptions::new())?)?; Ok(())}
/// The document's bytes. `options` can fix the file ID, the producer and the creation date, so/// that the same input gives the same bytes on every run.pub(crate) fn build(options: BuildOptions) -> Result<Vec<u8>, Box<dyn Error>> { let mut b = Builder::new(options); let helvetica = b.standard_font(Standard14::Helvetica)?; let mut page = b.page(PageSize::A4); // Page space: points from the top-left corner, y down; `y` is the text's baseline. page.surface().text( "Hello, world!", &helvetica, Pt::new(24.0), PointPt::from_points(72.0, 96.0), )?; page.finish()?; let mut doc = b.finish()?; Ok(doc.save(&SaveOptions::new())?.bytes)}
Run it
Section titled “Run it”cargo runThe comment at the top of the file shows how Lamina PDF’s own repository runs it; in your project, cargo run is all it takes. The program writes target/examples-out/hello.pdf; cargo run -- out writes out/hello.pdf instead. Open it in any PDF viewer: one page, with “Hello, world!” near its top-left corner.
What happened
Section titled “What happened”Builder made an A4 page, and the page’s surface drew the text in Helvetica, one of the standard 14 fonts every PDF viewer has, so nothing was embedded. Points count from the page’s top-left corner, and the text’s y is its baseline. finish turned the builder into a document, and save turned the document into bytes.
build takes BuildOptions, which can fix the file ID, the producer and the creation date, so the same program writes the same bytes on every run. The examples on this site use that to check their output byte for byte.
Next, see how Lamina PDF thinks about a PDF and where things go on a page. For your own fonts and text that wraps, read text and fonts.