Expand description
§pdfrum-edit
The crate that writes (ISO 32000-1 §7.5.8). A save either rewrites the whole file or appends a new body, cross-reference section and trailer to the bytes already there. Page import and reordering, N-up imposition, font subsetting, image embedding and re-encryption live here.
use std::sync::Arc;
use pdfrum_edit::{EditDoc, SaveOptions, save};
use pdfrum_parser::{LoadOptions, load};
let bytes = std::fs::read(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/files/hello.pdf"))?;
let doc = load(Arc::from(bytes), &LoadOptions::default())?;
let mut out = Vec::new();
save(&EditDoc::new(&doc), &SaveOptions::default(), &mut out)?;
assert!(out.starts_with(b"%PDF"));An untouched page is copied as stored; a touched one is regenerated, and
regeneration is lossy. This is the single fact to know before editing. The
content of a page you did not modify is written back byte for byte, so a save
is not a re-encode of the document. A page whose graph you did change has
its content stream rebuilt from that graph, and the rebuild does not preserve
everything the original said: colour comes back as rg/RG, and shadings,
text clipping modes and Type 3 glyph runs are dropped. Edit the pages you
mean to change and nothing else.
SaveMode chooses between the two file shapes. An incremental save appends
and leaves the original bytes intact, which is what keeps an existing digital
signature verifiable and what a reader expects of a form fill; a full save
rewrites and can drop what nothing references any more. An unencrypted save
is byte-reproducible under IdSource::Fixed. An encrypted one draws its
file key and AES vectors from the operating system: reproducible ciphertext
is reproducible secrets, which is not what a seed is for.
Part of pdfrum. #![forbid(unsafe_code)].
MIT OR Apache-2.0
Structs§
- Attachment
Options - What an attachment carries besides its name and bytes.
- Attachment
Options Builder - Builds an
AttachmentOptionsa setting at a time. - Canvas
- One page’s drawing surface.
- Dash
- A dash pattern — ISO 32000-1 §8.4.3.6’s dash array and phase, written as
d. - EditDoc
- A document plus the edits made to it.
- Embedded
Font - A font dictionary this session added, ready to name from a content stream.
- Embedded
Image - An image
XObjectthis session added, ready to place on a page. - Encryption
- How a document is to be encrypted on save (ISO 32000-2 §7.6.4.4).
- Encryptor
- Enciphers the strings and stream payloads of one indirect object.
- FileId
- The
/IDarray a save will write, and whether minting it invalidated the document’s encryption key. - GidMap
- How the glyphs of a font were renumbered by subsetting.
- Image
Builder - An image placement, ready to
PageEdit::push. - Import
Options - How an import behaves.
- IvSource
- Where a save’s AES initialisation vectors come from.
- Missing
Glyph - A character an embedded font has no glyph for.
- Miter
Limit - The ratio of miter length to line width past which a
LineJoin::Mitercorner is drawn beveled instead — ISO 32000-1 §8.4.3.5’sM. - NUpOptions
- How an N-up imposition behaves.
- Page
Range - A parsed page range: zero-based page indices, in the order named, with duplicates kept.
- Page
Rewrite - Everything a save has to do to a page whose objects were edited.
- Path
Builder - A filled or stroked path, ready to
PageEdit::push. - Regenerated
- One regenerated
/Contentselement. - Resource
Table - The resource dictionaries of one page, while its streams are regenerated.
- Save
Options - Everything a save may be asked to do differently.
- Stamp
Options - How a stamp is drawn.
- Stamp
Options Builder - Builds a
StampOptionsa setting at a time. - Stroke
- A stroke’s colour, width and pen shape, in canvas units.
- Subsetted
- A subset font program and the renumbering it performed.
- SvgFonts
svg-text - The font faces an ingested SVG’s
<text>may be set in. - SvgForm
svg-import - Drawing compiled once into a Form
XObject, placeable on any number of pages. - SvgIngest
Report svg-import - Everything one
draw_svgcould not carry into the page. - Text
Builder - A run of text, ready to
PageEdit::push. - Unknown
Flatten Mode - The error
FlattenMode’sFromStrreturns. - Unknown
Stamp Position - The error
StampPosition’sFromStrreturns. - Unsupported
Item svg-import - One construct the walk could not carry, and where it was.
Enums§
- Contents
Shape - Where a regenerated element lands in the page’s
/Contents. - Error
- A failure that stops the editor producing output.
- Fill
- Which points a fill considers inside.
- Flatten
Mode - Which annotations flattening draws.
- Flattened
- What flattening a page did.
- Font
Encoding - How character codes in a content stream select glyphs of an embedded font.
- IdSource
- Where the bytes of a fresh
/IDelement come from. - LineCap
- How the open ends of a stroked subpath are drawn — ISO 32000-1 §8.4.3.3’s
line cap style, written as
J. - Line
Join - How two segments meet at a corner — ISO 32000-1 §8.4.3.4’s line join
style, written as
j. - PageBox
- One of the five page boxes (ISO 32000-1 §14.11.2).
- Paint
- How a shape is painted.
- Pixel
Format - How the bytes handed to
crate::EditDoc::embed_imageare laid out. - Save
Mode - How a document is written back out.
- Stamp
Position - Where a stamp sits on the page, as the page is displayed.
- Standard
Font - One of the fourteen standard fonts.
- SvgFit
svg-import - How the SVG’s own coordinate box is placed in the destination rectangle.
- Unsupported
svg-import - A construct in the source SVG that PDF drawing cannot carry.
Functions§
- add_
attachment - Adds an embedded file named
name, sorted into the/EmbeddedFilesname tree by name — creating the tree when the document has none — and returns its index among the attachments. - add_
blank_ page - Add an empty page of
widthbyheightpoints at indexat(past the end appends), and return its reference. - apply_
rewrite - Apply a page’s regenerated content to
edit. - build_
graph - The object graph of
dict’s page for editing, read throughr: the base document for a page as it was opened, or an editing session’s overlay for the page as that session’s edits leave it — so a stream an earlier edit appended is a clean stream of the graph, and the names it uses are kept. - delete_
attachment - Removes attachment
indexfrom the name tree;Ok(false)when there is no such attachment. - delete_
pages - Delete the pages
rangenames, by the base document’s numbering. - flatten
- Bakes the page’s annotation appearances into its content and removes
the annotations, the reference implementation’s way: one form
XObjectFFT<n>(the first such name free in the page’s/XObjectresources), its box the crop box, drawing each appearance stream through the matrix that lands its box on the annotation’s rectangle; the page’s own content wrapped inq … Qwith the form’sDoafter it;/MediaBoxand/CropBoxnormalized and written; the retired widgets pruned from the interactive form, which goes when it empties. A widget another page also lists keeps the form. - import_
pages - Import
pagesfromsrcintodest. - n_
page_ to_ one - Impose
pagesfromsrconto sheets indest. - pdf_
date timeas a PDF date string (ISO 32000-1 §7.9.4):D:YYYYMMDDHHmmSSZ00'00', always in UTC, which is the one zone every reader agrees on.- pdf_
date_ to_ iso8601 - A PDF date string (
D:YYYYMMDDHHmmSSOHH'mm') as XMP’s ISO 8601. - regenerate
- Rewrite the dirty content streams of
page, orNonewhen none is dirty. - remove_
attachment - Removes every attachment named
namefrom the name tree;Ok(false)when there is none. The objects go with the next full save’s garbage collection. - save
- Write
doctoout. - set_
attachment_ description - Sets the file specification’s
/Desc;Ok(false)when there is no such attachment. - set_
attachment_ file - Replaces attachment
index’s embedded file with a new stream carrying/Type /EmbeddedFile,/DL <len>and/Params << /Size <len> /CheckSum <md5> >>, linked as/EF << /F <ref> >>on the file specification; a MIME type or date the old stream had is not carried over.Ok(false)when there is no such attachment. - set_
attachment_ param - Sets a
/Paramstext entry on attachmentindex’s embedded file —CreationDate,ModDate, any key — creating/Paramswhen missing; aCheckSumgiven as<HEX…>is stored as that hex string.Ok(false)when the attachment has no embedded file. - set_
info_ entry - Set or remove one text entry of the document’s
/Infodictionary. - set_
page_ box - Set one of a page’s boxes.
- set_
page_ rotation - Set a page’s
/Rotate.degreesis normalized to a multiple of 90; a value that is not one is rounded to the nearest. - shared_
objects - The object numbers more than one object in
docpoints at. - string_
width - The advance of
codesin the fontfontnames, in thousandths of an em. - subset
- Subset a font program to
gids. - to_pdfa
- Run the conversion, returning what it did and the bytes when it converted.
- write_
float - Append one number:
10.5,.29,-7,0. - write_
matrix - Append a matrix as
a b c d e f— single spaces, no brackets, no trailing space. This is the operand list ofcmandTm. - write_
point - Append a point as
x y. - write_
rect - Append a rectangle as
left bottom width height— not thex0 y0 x1 y1an array-valued/MediaBoxuses. This is the operand list ofre, so the last two numbers are extents and may be negative.
Type Aliases§
- Share
Counts - How many objects still reference each object number.