Skip to main content

Crate sghtmltopdf

Crate sghtmltopdf 

Source
Expand description

A lightweight HTML to PDF renderer that does not depend on Chromium, WebKit or Gecko.

Feed HTML to an Engine in chunks and it writes the PDF bytes into a Sink.

use sghtmltopdf::{Engine, EngineOptions, MemorySink};

let mut engine = Engine::new(EngineOptions::default(), MemorySink::default());
engine.feed(b"<!DOCTYPE html><p>Hello</p>").unwrap();
let pdf: Vec<u8> = engine.finish().unwrap();

With the cli feature (on by default), Converter runs a conversion configured by the same options as the sghtmltopdf command, which is the simplest entry point for language bindings.

Rendering recurses as deep as the document, so run it on a thread with enough stack, for example through with_render_stack.

§Stability

The items re-exported at the crate root are the public API and follow semver. The modules are reachable only so that the CLI, the tests and the Ruby binding can use the internals; they are hidden from the documentation and may change in any release.

Structs§

BufferedSink
A Sink that calls on_part every time threshold bytes have accumulated.
ContentOptions
Options changing the behaviour of what is rendered.
Converter
A conversion configured by the same options as the sghtmltopdf command.
DocumentMetadata
The document metadata written to the PDF Info dictionary.
EdgeSizes
A length for each of the four sides, such as a margin, in CSS px.
Engine
The rendering engine: feed it HTML and it writes the PDF into a Sink.
EngineOptions
The initialisation options for Engine.
FileSink
A Sink that writes the PDF to a file.
FontSpec
An explicit font specification, the equivalent of --font.
HeaderFooterHtml
The --header-html/--footer-html templates.
HeaderFooterPlaceholders
The text that stands for a page number in HeaderFooterHtml templates.
LocalAccess
The permission settings for local file references.
MemorySink
A Sink that collects the PDF in memory. Sink::finish returns the bytes.
PageSettings
The page size and margins. The default is A4 with a 1in (96px) margin on every side.
PageSize
A page size in CSS px (1px = 1/96in), as @page { size } gives it.
PdfOutputOptions
PDF output options.
StdoutSink
A Sink that writes the PDF to standard output.
TocHeading
One heading listed in the table of contents.
TocSettings
The table-of-contents (--toc) settings.

Enums§

ConvertError
Errors from the command and from Converter, classified by cause.
EngineError
The errors Engine returns. E is the error type of the Sink.
GenericFamily
The CSS generic family names whose concrete font can be given explicitly. cursive/fantasy are not covered.
Mode
Selects batch or streaming processing.

Constants§

DEFAULT_SCALE
The default factor converting CSS px to PDF pt (at 96dpi, 72 / 96).
MULTIPART_MIN_PART_SIZE
Minimum part size for a multipart upload (all but the last part).
STACK_SIZE
Stack size allocated for the thread that runs rendering.

Traits§

Sink
Where the Engine writes the PDF bytes.

Functions§

with_render_stack
Run f on a thread with a STACK_SIZE stack and return its result.

Type Aliases§

TocHtmlBuilder
The function building the table-of-contents HTML from the list of headings.