Skip to content
PressJetDocs Docs v0.1.0-beta.9 GitHub
Edit this page
Source docs v0.1.0-beta.9 Install v0.1.0-beta.7
On this page

Render and edit PDFs in Rust

The in-process SDK is distributed separately from the HTTP server. Start with the published Beta 8 Rust crate. The Beta 9 RC examples below require the current source checkout.

[dependencies]
pressjet = "=0.1.0-beta.8"
use pressjet::{Accessibility, Html, Renderer};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let renderer = Renderer::default();
    let html = "<!doctype html><html lang='en'><title>Invoice</title><h1>Invoice</h1>";
    let document = renderer.render_html(Html {
        html,
        stylesheets: &["h1 { font-family: Helvetica; margin-bottom: 1em }"],
        accessibility: Accessibility::PdfUa2,
    })?;
    std::fs::write("invoice.pdf", document.pdf)?;
    Ok(())
}

Pass Limits to Renderer::new and choose with_maximum_input_bytes for the workload. Register a compressed image with add_image and a font with add_font. The renderer resolves named image sources against its resources. HTML can also carry embedded images and inline CSS. Resource access belongs to the caller, so the SDK never fetches a URL or reads a path.

For interactive forms, render the native tree or HTML controls and call form_values or fill_form. flatten_form accepts supported unsigned forms and preserves field reading order in tagged documents. merge borrows PDF slices and refuses signed inputs. Redaction uses a plan and preview bound to the original bytes and to the key that issued the plan: by default a random key per process, or the RedactionPlanKey set with with_redaction_options when plans must outlive the process. The same RedactionOptions set the time budget (with_maximum_millis, default 30 000 ms). Match RedactionReviewReason with a wildcard arm: new reasons may be added. Redaction runs in your process, where its static bounds are best effort and do not model every rendering cost: redact untrusted PDFs with pressjet redact, which isolates each one in a limited child process, or in a subprocess you limit yourself. overlay draws layout nodes such as images and text on an unsigned PDF. sign uses a caller-supplied identity and ISO 8601 time. Use typed errors to distinguish invalid documents, operation failures, and budget limits. See the runnable in-memory example and the redaction guide.

Collect sequential approvals (Beta 9 RC)

Declare a separate empty signature field for each approval on the original PDF. Start with Renderer::start_approvals, call approve with each field name and signing identity, then consume the chain with into_bytes. The caller owns the chain for the duration of the workflow. Each step creates an incremental revision and preserves all earlier signed bytes. This API covers approvals of supported unlocked fields on a single unsigned source revision. Certification and resumed external revisions require their own workflow. The in-memory example can write a PDF approved by two separate PKCS#12 identities.

Signing with your own database (Beta 9 RC)

The pressjet::signing module runs a multi-signer agreement in-process, over a sealed JSON envelope that you keep in your own database. PressJet stores nothing.

What you supply:

  • The clock. Every operation that records a time takes a signing::Instant, for example Instant::from_unix_millis(ms). An instant earlier than one the envelope already records is refused with clock_behind.
  • The randomness. prepare_signing_pdf and prepare_signing_template take the 24 random bytes of the revision id ([u8; 24]) from your own secure random source.
  • The key ring. Build a signing::KeyRing with KeyRing::from_json from your own secret store, never from the database that holds the envelopes.
  • The envelope. Store the JSON that Envelope::to_json returns and read it back with Envelope::parse. Store each transition's events too if you want signing::verify to check the whole chain. Save a new envelope only if the stored version is still the one you loaded, and on open insert it only if no envelope with that envelope_id exists; see the storage contract.
use pressjet::signing::{self, Instant, KeyRing};
use pressjet::Renderer;

let renderer = Renderer::default();
let keys = KeyRing::from_json(&key_ring_json)?;               // from your secret store
let revision = renderer.prepare_signing_pdf(&pdf, &fields, random_24, &keys)?;
let now = Instant::from_unix_millis(clock_ms).unwrap();
let opened = signing::open(&revision, &recipient_emails, None, now, &keys)?;
// Insert opened.envelope.to_json() and opened.events only if no envelope with that id
// exists, then send each Invite effect. After a later step on an envelope you loaded, save
// with the version you loaded: save(&transition.envelope.to_json(), loaded.version), which
// must change nothing if the stored version has moved on.

The function around this sample returns Result<(), pressjet::Error>; the ? on signing::open works because Error implements From<signing::EnvelopeError>. crates/pressjet/tests/pipeline.rs has the full flow, from prepare_signing_pdf to void_copy.

The pure steps (open, view, decide, revoke, void, effects, verify) are plain functions in pressjet::signing. The four that render a template or read a PDF need the renderer's limits and are methods on Renderer: prepare_signing_template, prepare_signing_pdf, seal_envelope and void_copy. They apply the renderer's input budget first (the template's bytes and data for prepare_signing_template, the PDF and images for the others). The engine functions that pressjet::signing re-exports for the same steps, signing::{prepare_pdf, prepare_template, seal, void_copy}, bypass that budget. Every refusal comes back as Error::Signing(signing::EnvelopeError); EnvelopeError::code() gives the stable code (for example envelope_closed). A decision is recorded once: a repeat returns the same receipt and no transition.

void_copy is for agreements that cannot complete (declined, revoked, voided or expired); to withdraw a completed one, call signing::void first.

What PressJet guarantees: it rejects a stored envelope or event that was changed without the signing key, and the sealed PDF and evidence fix the final history under your certificate. In this mode PressJet does not witness the signer: you report who accepted, from where and when the request arrived. PressJet cannot detect an older, valid copy of an envelope restored before sealing, nor any change by someone who holds both the database and the key. Keep the key away from the database.

Package the SDK for release

The release script builds and verifies the pressjet-engine, pressjet-html, and pressjet crates from the current Git commit. Supply the Cargo release version and an output directory. The script writes the source commit and SHA-256 checksums with the archives.

scripts/package-rust-sdk.sh 0.1.0-beta.9 ./dist/rust-sdk

Append --publish for a registry release after authenticating Cargo with the account that owns the release. The release build uses a staging workspace, and the three library crates carry the requested version together.

The SDK and each dependency have version and licensing metadata in their Cargo manifests. The engine and HTML packages must be available in the selected registry before publishing the SDK there. For candidate builds, use a path dependency pointing at crates/pressjet in a checkout of the candidate revision.

GitHub Actions verifies the three packages in CI and attaches them to tagged GitHub releases. See the release workflow in the repository for crates.io publication and the clean-install check.