Struct rckive_genpdf::Document
source · pub struct Document { /* private fields */ }
Expand description
A PDF document.
This struct is the entry point for the high-level rckive_genpdf
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
// Load a font from the file system
let font_family = rckive_genpdf::fonts::from_files("./fonts", "LiberationSans", None)
.expect("Failed to load font family");
// Create a document and set the default font family
let mut doc = rckive_genpdf::Document::new(font_family);
doc.push(rckive_genpdf::elements::Paragraph::new("Document content"));
doc.render_to_file("output.pdf").expect("Failed to render document");
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: f64)
pub fn set_line_spacing(&mut self, line_spacing: f64)
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<(), Error>
pub fn render(self, w: impl Write) -> Result<(), 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.
sourcepub fn render_to_file(self, path: impl AsRef<Path>) -> Result<(), Error>
pub fn render_to_file(self, path: impl AsRef<Path>) -> Result<(), 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
)