Document

Struct Document 

Source
pub struct Document {
    pub(crate) root: LinearLayout,
    pub(crate) title: String,
    pub(crate) context: Context,
    pub(crate) style: Style,
    pub(crate) paper_size: Size,
    pub(crate) decorator: Option<Box<dyn PageDecorator>>,
    pub(crate) conformance: Option<PdfConformance>,
    pub(crate) creation_date: Option<OffsetDateTime>,
    pub(crate) modification_date: Option<OffsetDateTime>,
}
Expand description

A PDF document.

This struct is the entry point for the high-level genpdfi API. It stores a set of elements and default style and layout settings. Add elements to the document by calling the push method and then render them to a PDF file using the render and render_to_file methods.

The root element of the document is a LinearLayout that vertically arranges all elements. For details on the rendering process, see the Rendering Process section of the crate documentation.

You can add a PageDecorator to this document by calling set_page_decorator. This page decorator will be called for every new page and can add a margin, a header or other elements to the page before it is filled with the actual document content. See the SimplePageDecorator for a basic implementation.

If the hyphenation feature is enabled, users can activate hyphenation with the [set_hyphenator][] method.

§Example

use genpdfi_extended::{fonts, elements, Document};
// Create a font family from the bundled fonts and create a simple document
let font_family = fonts::from_files(concat!(env!("CARGO_MANIFEST_DIR"), "/fonts"), "NotoSans", None)
    .expect("Failed to load font family");
let mut doc = Document::new(font_family);
doc.push(elements::Paragraph::new("Document content"));
// Render to an in-memory buffer (no filesystem side effects in doctest)
let mut buf = Vec::new();
let _render_results = doc.render(&mut buf).expect("Failed to render document");
assert!(!buf.is_empty());

Fields§

§root: LinearLayout§title: String§context: Context§style: Style§paper_size: Size§decorator: Option<Box<dyn PageDecorator>>§conformance: Option<PdfConformance>§creation_date: Option<OffsetDateTime>§modification_date: Option<OffsetDateTime>

Implementations§

Source§

impl Document

Source

pub fn new(default_font_family: FontFamily<FontData>) -> Document

Creates a new document with the given default font family.

Source

pub fn add_font_family( &mut self, font_family: FontFamily<FontData>, ) -> FontFamily<Font>

Adds the given font family to the font cache for this document and returns a reference to it.

Note that the returned font reference may only be used for this document. It cannot be shared with other Document or FontCache instances.

Source

pub fn font_cache(&self) -> &FontCache

Returns the font cache used by this document.

You can use the font cache to get the default font and to query glyph metrics for a font. Use the load_font_family method instead if you want to add fonts to this document.

Source

pub fn set_hyphenator(&mut self, hyphenator: Standard)

Activates hyphenation and sets the hyphentor to use.

Only available if the hyphenation feature is enabled.

Source

pub fn set_title(&mut self, title: impl Into<String>)

Sets the title of the PDF document.

If this method is not called, the PDF title will be empty.

Source

pub fn set_font_size(&mut self, font_size: u8)

Sets the default font size in points for this document.

If this method is not called, the default value of 12 points is used.

Source

pub fn set_line_spacing(&mut self, line_spacing: f32)

Sets the default line spacing factor for this document.

If this method is not called, the default value of 1 is used.

Source

pub fn set_paper_size(&mut self, paper_size: impl Into<Size>)

Sets the paper size for all pages of this document.

If this method is not called, the default size A4 is used.

Source

pub fn set_page_decorator<D: PageDecorator + 'static>(&mut self, decorator: D)

Sets the page decorator for this document.

The page decorator is called for every page before it is filled with the document content. It can add margins, headers or other elements.

See the SimplePageDecorator for an example implementation.

Source

pub fn set_conformance(&mut self, conformance: PdfConformance)

Sets the PDF conformance settings for this document.

Source

pub fn set_minimal_conformance(&mut self)

Sets the minimal PDF conformance settings for this document.

If this method is called, the generation of ICC profiles and XMP metadata is deactivated, leading to a smaller file size.

Source

pub fn set_creation_date(&mut self, date: OffsetDateTime)

Sets the creation date of the PDF file.

Source

pub fn set_modification_date(&mut self, date: OffsetDateTime)

Sets the modification date of the PDF file.

Source

pub fn push<E: IntoBoxedElement>(&mut self, element: E)

Adds the given element to the document.

The given element is appended to the list of elements that is rendered by the root LinearLayout once render or render_to_file is called.

Source

pub fn render(self, w: impl Write) -> Result<Vec<RenderResult>, Error>

Renders this document into a PDF file and writes it to the given writer.

The given writer is always wrapped in a buffered writer. For details on the rendering process, see the Rendering Process section of the crate documentation.

§Example
use genpdfi_extended::{Document, elements, fonts};
let data = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/fonts/NotoSans-Regular.ttf")).to_vec();
let fd = fonts::FontData::new(data, None).expect("font data");
let family = fonts::FontFamily { regular: fd.clone(), bold: fd.clone(), italic: fd.clone(), bold_italic: fd.clone() };
let mut doc = Document::new(family);
doc.push(elements::Paragraph::new("Hello, world!"));
let mut out = Vec::new();
let _render_results = doc.render(&mut out).expect("render");
assert!(!out.is_empty());
Source

pub fn render_to_file( self, path: impl AsRef<Path>, ) -> Result<Vec<RenderResult>, Error>

Renders this document into a PDF file at the given path.

If the given file does not exist, it is created. If it exists, it is overwritten.

For details on the rendering process, see the Rendering Process section of the crate documentation.

Trait Implementations§

Source§

impl<E: IntoBoxedElement> Extend<E> for Document

Source§

fn extend<I: IntoIterator<Item = E>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Finish for T

§

fn finish(self)

Does nothing but move self, equivalent to drop.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

§

fn to_owned_table(&self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V