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
impl Document
Sourcepub fn new(default_font_family: FontFamily<FontData>) -> Document
pub fn new(default_font_family: FontFamily<FontData>) -> Document
Creates a new document with the given default font family.
Sourcepub fn add_font_family(
&mut self,
font_family: FontFamily<FontData>,
) -> FontFamily<Font>
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.
Sourcepub fn font_cache(&self) -> &FontCache
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.
Sourcepub fn set_hyphenator(&mut self, hyphenator: Standard)
pub fn set_hyphenator(&mut self, hyphenator: Standard)
Activates hyphenation and sets the hyphentor to use.
Only available if the hyphenation feature is enabled.
Sourcepub fn set_title(&mut self, title: impl Into<String>)
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.
Sourcepub fn set_font_size(&mut self, font_size: u8)
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.
Sourcepub fn set_line_spacing(&mut self, line_spacing: f32)
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.
Sourcepub fn set_paper_size(&mut self, paper_size: impl Into<Size>)
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.
Sourcepub fn set_page_decorator<D: PageDecorator + 'static>(&mut self, decorator: D)
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.
Sourcepub fn set_conformance(&mut self, conformance: PdfConformance)
pub fn set_conformance(&mut self, conformance: PdfConformance)
Sets the PDF conformance settings for this document.
Sourcepub fn set_minimal_conformance(&mut self)
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.
Sourcepub fn set_creation_date(&mut self, date: OffsetDateTime)
pub fn set_creation_date(&mut self, date: OffsetDateTime)
Sets the creation date of the PDF file.
Sourcepub fn set_modification_date(&mut self, date: OffsetDateTime)
pub fn set_modification_date(&mut self, date: OffsetDateTime)
Sets the modification date of the PDF file.
Sourcepub fn push<E: IntoBoxedElement>(&mut self, element: E)
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.
Sourcepub fn render(self, w: impl Write) -> Result<Vec<RenderResult>, Error>
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());Sourcepub fn render_to_file(
self,
path: impl AsRef<Path>,
) -> Result<Vec<RenderResult>, Error>
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
impl<E: IntoBoxedElement> Extend<E> for Document
Source§fn extend<I: IntoIterator<Item = E>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = E>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Auto Trait Implementations§
impl Freeze for Document
impl !RefUnwindSafe for Document
impl !Send for Document
impl !Sync for Document
impl Unpin for Document
impl !UnwindSafe for Document
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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
impl<T> Pointable for T
§impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
impl<U, T> ToOwnedObj<U> for Twhere
U: FromObjRef<T>,
§fn to_owned_obj(&self, data: FontData<'_>) -> U
fn to_owned_obj(&self, data: FontData<'_>) -> U
T, using the provided data to resolve any offsets.