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.
- Document
Date - A date in W3C’s ISO 8601 profile extracted from document metadata.
- Document
Metadata - Source metadata associated with a rendered document.
- Forced
Color Palette - CSS system colors supplied by the rendering environment.
- Html
- An HTML or XML source document and its rendering configuration.
- Http
Request Timeout - A non-zero time limit for a single HTTP(S) resource request.
- Invalid
Http Request Timeout - Error returned when constructing an
HttpRequestTimeoutfrom zero. - Media
Environment - Static capabilities used to evaluate CSS Media Queries for one rendering.
- PdfOptions
- Policy for serializing a rendered document as PDF.
- Render
Options - Inputs that control document parsing, cascade, and layout.
- Resource
Policy - Policy used for filesystem and HTTP(S) document resources.
- Url
- A parsed URL used as an HTML or stylesheet source.
Enums§
- Bookmark
State - The initial expansion state of a bookmark in a PDF viewer.
- Color
Scheme Preference - User preference consulted when resolving CSS Color Adjustment’s used color
scheme.
Noneis 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.
- Fetch
Error Policy - Controls how a resource fetch failure affects rendering.
- Font
Embedding Mode - Controls whether PDF embedding keeps complete font programs or subsets them.
- Forced
Colors Mode - Whether CSS forced-colors mode is active for this render.
- Input
Syntax - Input markup syntax used for document parsing.
- Media
Type - 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§
- CssViewport
Size - A viewport size expressed in CSS pixels.
- Result
- Spindrift’s standard result type.