Skip to main content

Crate spindrift

Crate spindrift 

Source
Expand description

A from-scratch Rust HTML/CSS to PDF renderer inspired by WeasyPrint.

§Examples

§Convert an HTML file to a PDF

Load a local HTML document and write its PDF to a local file:

use spindrift::{Html, PdfOptions, RenderOptions};
use std::fs::File;

let mut output = File::create("document.pdf")?;
Html::from_file("document.html")
    .await?
    .write_pdf(
        &mut output,
        &RenderOptions::default(),
        &PdfOptions::default(),
    )
    .await?;

§Render a report with explicit resource handling

The rendering pipeline keeps document and stylesheet loading, CSS layout, and PDF serialization separate. This lets an application share a resource policy, supply a user-origin stylesheet, inspect the rendered document, and choose its PDF output settings:

use spindrift::{
    Css, FetchErrorPolicy, Html, HttpRequestTimeout, PdfOptions, RenderOptions,
    ResourcePolicy,
};
use std::{fs::File, time::Duration};

let resource_policy = ResourcePolicy {
    follow_http_redirects: false,
    http_timeout: HttpRequestTimeout::try_from(Duration::from_secs(5))
        .expect("five seconds is non-zero"),
    error_policy: FetchErrorPolicy::Allow,
};

let stylesheet = Css::from_file("print.css")
    .await?
    .with_user_origin()
    .with_resource_policy(resource_policy);
let html = Html::from_file("report.html")
    .await?
    .with_resource_policy(resource_policy)
    .with_stylesheet(stylesheet);

let document = html.render(&RenderOptions::default()).await?;
if let Some(title) = document.metadata().title() {
    println!("Rendered {title}");
}
for bookmark in document.bookmarks() {
    println!("Bookmark: {}", bookmark.label());
}

let mut output = File::create("report.pdf")?;
document.write_pdf(&mut output, &PdfOptions::default())?;

Structs§

Bookmark
A document bookmark derived from the rendered source.
Css
A stylesheet source and its resource-loading context.
Document
The fully rendered, inspectable document before PDF serialization.
DocumentDate
A date in W3C’s ISO 8601 profile extracted from document metadata.
DocumentMetadata
Source metadata associated with a rendered document.
ForcedColorPalette
CSS system colors supplied by the rendering environment.
Html
An HTML or XML source document and its rendering configuration.
HttpRequestTimeout
A non-zero time limit for a single HTTP(S) resource request.
InvalidHttpRequestTimeout
Error returned when constructing an HttpRequestTimeout from zero.
MediaEnvironment
Static capabilities used to evaluate CSS Media Queries for one rendering.
PdfOptions
Policy for serializing a rendered document as PDF.
RenderOptions
Inputs that control document parsing, cascade, and layout.
ResourcePolicy
Policy used for filesystem and HTTP(S) document resources.
Url
A parsed URL used as an HTML or stylesheet source.

Enums§

BookmarkState
The initial expansion state of a bookmark in a PDF viewer.
ColorSchemePreference
User preference consulted when resolving CSS Color Adjustment’s used color scheme. None is intentionally the default: in that case the first author-supported scheme wins. https://www.w3.org/TR/css-color-adjust-1/#color-scheme-preference
Error
An error returned while loading, rendering, or serializing a document.
FetchErrorPolicy
Controls how a resource fetch failure affects rendering.
FontEmbeddingMode
Controls whether PDF embedding keeps complete font programs or subsets them.
ForcedColorsMode
Whether CSS forced-colors mode is active for this render.
InputSyntax
Input markup syntax used for document parsing.
MediaType
The output medium used when evaluating CSS Media Queries.
PdfCompression
Controls whether Spindrift applies Flate compression to generated PDF streams.
PdfProfile
Selects the PDF output profile and conformance-identification metadata to emit.

Type Aliases§

CssViewportSize
A viewport size expressed in CSS pixels.
Result
Spindrift’s standard result type.