pdf#
PDF writer (vector pages, font subsets, links, outlines) and reader (encryption, text, outlines, forms, attachments, drawing pages) -- crate root lib/pdf/src/lib.rs
pdf -- a vector PDF writer: pages of their own size drawn on a canvas (paths, fills with the
non-zero or even-odd rule, strokes with width, dashes, caps, joins and miter limit,
transforms, clipping, alpha, RGB colours, axial and radial gradients with any number of stops
and alpha, hatches of parallel lines), images (JPEG passed through, other pixels with
FlateDecode and their alpha as a soft mask), text in embedded TrueType / OpenType subsets
(CID fonts with Identity-H, ToUnicode for search and copy, glyphs and widths from the
library's shaping), links to URIs and to places in the document, nested outlines
(bookmarks), the document information and XMP metadata, form fields (form.rs), encryption with
AES-256 or AES-128 (encrypt.rs). PDF 1.7 with a classic xref table; the same document with the
same options always gives the same bytes (an encrypted one with the same Encryption::with_seed). Not here: PDF/A. And a
reader, pdf::read: structure, encryption, text, outlines, forms, attachments, and drawing pages
(below); pdf::edit changes what it read and saves it as an incremental update (forms filled in
and flattened) or writes it again in full (encrypted, decrypted, with another password).
let mut doc = Document::new();
let page = doc.add_page(A4.0, A4.1); // points (1/72 inch); mm(210.0) converts
let font = doc.add_font(bytes, 0)?; // TrueType / OpenType (CFF), a collection's face
{
let mut c = doc.canvas(page, Origin::TopLeft); // or BottomLeft (PDF's own page space)
c.set_fill_color(Rgb::hex(0x2060C0));
c.rect(40.0, 40.0, 200.0, 100.0);
c.fill(FillRule::NonZero);
c.text(font, 12.0, 40.0, 180.0, "Hallo, Welt"); // shaped; returns the advance
c.link_uri(40.0, 168.0, 80.0, 16.0, "https://example.org/");
} // the drawing goes into the page here
doc.add_outline(None, "Start", Dest::Fit(page));
let bytes = doc.write()?;
Document add_page / page_size / page_count, canvas(page, origin) -> Canvas,
add_font -> FontId, font_metrics, shape, text_width, add_image(Image) -> ImageId,
add_outline -> OutlineId, set_outline_open, set_info(Info), set_language,
set_compression, set_encryption(Some(Encryption)), write() -> bytes
Encryption aes256 / aes128(user, owner), permissions(Permissions), encrypt_metadata,
with_seed; Permissions PRINT, MODIFY, COPY, ANNOTATE, FILL_FORMS, ...
Canvas save / restore, transform / translate / scale / rotate, fill / stroke colour and
alpha, line width, cap, join, miter limit, dash, path building (move_to,
line_to, curve_to, quad_to, rect, rounded_rect, ellipse, circle, arc, close,
append_path), fill / stroke / fill_stroke / clip / end_path, shade /
fill_shading (gradients), fill_hatch, image / image_transform, text /
text_fallback / glyphs, set_text_mode, link_uri / link_dest, dest (a
destination at a point of this canvas), text_field / check_box / radio /
combo_box / list_box / push_button / signature_field (TextField, FieldStyle,
FieldFont, Align)
Path the same path building, kept and drawn later (Canvas::fill_path, stroke_path)
Shading a gradient: ShadingKind Axial / Radial (PDF shading types 2 and 3), stops
(t, colour, alpha), extend; Shading::axial / radial
Hatch parallel lines: angle, spacing, width, colour, alpha, a point on a line
Image jpeg(file), gray / rgb / rgba(w, h, pixels), from_surface(gfx::Surface),
with_alpha, interpolate
Text: Canvas::text shapes one line with lib/text_shape (HarfRust: kerning, ligatures, marks,
Arabic joining, bidi); Canvas::glyphs takes glyphs a layout shaped already (pdf::Glyph). Each glyph is
placed where the shaping put it (TJ adjustments against the font's widths); the CIDs are the
font's glyph ids. The first glyph of each cluster maps to the cluster's text in ToUnicode.
Variable fonts are embedded at their default instance, colour glyphs as their outlines.
Fonts whose OS/2 fsType forbids embedding are refused (Error::FontLicense); fsType "no
subsetting" embeds every glyph.
Gradients (shading.rs): DeviceRGB shadings with a Type 2 function per stretch between stops,
stitched by a Type 3 function; one alpha for all stops is the fill alpha (ExtGState ca), varying
alpha a luminosity soft mask (a transparency group painting the alphas as a DeviceGray shading
in the same user space). Degenerate gradients paint the last stop's colour as SVG does.
Hatches are clipped, stroked lines in one path, not tiling patterns (pdfium shows seams between
rotated pattern cells, and readers round the cell step to device pixels).
Reading: pdf::read (read/mod.rs) opens PDF files of every version, also encrypted and damaged
ones, and gives their objects, pages, text with the position of every glyph, information,
outlines, links and other annotations, form fields, attachments and page labels. It shares
Info and Date with the writer and nothing else.
Sections
- pdf crate root
- pdf/src/canvas.rs
- pdf/src/edit/mod.rs
- pdf/src/edit/form.rs
- pdf/src/edit/full.rs
- pdf/src/encrypt.rs
- pdf/src/font.rs
- pdf/src/form.rs
- pdf/src/geom.rs
- pdf/src/image.rs
- pdf/src/read/mod.rs
- pdf/src/read/content.rs
- pdf/src/read/nav.rs
- pdf/src/read/object.rs
- pdf/src/read/render/mod.rs
- pdf/src/read/render/shade.rs
- pdf/src/read/text.rs
- pdf/src/shading.rs