pub struct Renderer {
doc: PdfDocument,
conformance: Option<PdfConformance>,
creation_date: Option<OffsetDateTime>,
modification_date: Option<OffsetDateTime>,
pages: Vec<Page>,
}Expand description
Renders a PDF document with one or more pages.
This is a wrapper around a printpdf::PdfDocumentReference.
Fields§
§doc: PdfDocument§conformance: Option<PdfConformance>§creation_date: Option<OffsetDateTime>§modification_date: Option<OffsetDateTime>§pages: Vec<Page>Implementations§
Source§impl Renderer
impl Renderer
Sourcepub fn new(
size: impl Into<Size>,
title: impl AsRef<str>,
) -> Result<Renderer, Error>
pub fn new( size: impl Into<Size>, title: impl AsRef<str>, ) -> Result<Renderer, Error>
Creates a new PDF renderer with an initial page of the given size and a title.
The method initializes a printpdf document and adds a page and a default layer
named “Layer 1”. It returns an error if creation fails.
§Example
use genpdfi_extended::render::Renderer;
use genpdfi_extended::Size;
use genpdfi_extended::Mm;
let mut r = Renderer::new(Size::new(210.0, 297.0), "title").expect("renderer");
assert_eq!(r.page_count(), 1);
r.add_page(Size::new(100.0, 100.0));
assert!(r.page_count() >= 2);
let page = r.get_page(0).unwrap();
let layer = page.first_layer();
let area = layer.area();
assert!(area.size().width > Mm::from(0.0));§Example: print text using a builtin-equivalent font
use genpdfi_extended::render::Renderer;
use genpdfi_extended::{Size, Mm, Position};
use genpdfi_extended::fonts::{FontData, FontFamily, FontCache};
use printpdf::BuiltinFont;
// Use bundled font bytes but mark as Builtin so we can rely on builtin font rendering
let data = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/fonts/NotoSans-Regular.ttf")).to_vec();
let fd_builtin = FontData::new(data, Some(BuiltinFont::Helvetica)).expect("font data");
let family_builtin = FontFamily { regular: fd_builtin.clone(), bold: fd_builtin.clone(), italic: fd_builtin.clone(), bold_italic: fd_builtin.clone() };
let mut builtin_cache = FontCache::new(family_builtin);
let mut r = Renderer::new(Size::new(210.0, 297.0), "text").expect("renderer");
builtin_cache.load_pdf_fonts(&mut r).expect("load builtin font");
let area = r.first_page().first_layer().area();
let style = genpdfi_extended::style::Style::new().with_font_family(builtin_cache.default_font_family()).with_font_size(12);
area.print_str(&builtin_cache, Position::new(Mm::from(10.0), Mm::from(280.0)), style, "Hello from builtin").expect("print");
let mut buf = Vec::new();
r.write(&mut buf).expect("write");
assert!(!buf.is_empty());Sourcepub fn with_conformance(self, conformance: PdfConformance) -> Self
pub fn with_conformance(self, conformance: PdfConformance) -> Self
Sets the PDF conformance (e.g. PDF/A) for the generated document.
The option is applied when saving the document.
Sourcepub fn with_creation_date(self, date: OffsetDateTime) -> Self
pub fn with_creation_date(self, date: OffsetDateTime) -> Self
Sets the creation date that will be recorded in the PDF metadata.
The value is retained and applied when saving the document.
Sourcepub fn with_modification_date(self, date: OffsetDateTime) -> Self
pub fn with_modification_date(self, date: OffsetDateTime) -> Self
Sets the last modification date for the PDF metadata.
Use this to force the modification date recorded in the file.
Sourcepub fn add_page(&mut self, size: impl Into<Size>)
pub fn add_page(&mut self, size: impl Into<Size>)
Adds a new page of the given size to the document.
A default layer (Layer 1) is created for the new page.
Sourcepub fn page_count(&self) -> usize
pub fn page_count(&self) -> usize
Returns the number of pages in the current document.
Sourcepub fn get_page(&self, idx: usize) -> Option<&Page>
pub fn get_page(&self, idx: usize) -> Option<&Page>
Returns an immutable reference to the page at the given index, or None if out
of range.
Sourcepub fn get_page_mut(&mut self, idx: usize) -> Option<&mut Page>
pub fn get_page_mut(&mut self, idx: usize) -> Option<&mut Page>
Returns a mutable reference to the page at the given index, or None if out
of range. Allows modifying the page (adding layers, etc.).
Sourcepub fn first_page(&self) -> &Page
pub fn first_page(&self) -> &Page
Returns an immutable reference to the first page of the document.
Sourcepub fn first_page_mut(&mut self) -> &mut Page
pub fn first_page_mut(&mut self) -> &mut Page
Returns a mutable reference to the first page of the document.
Sourcepub fn last_page(&self) -> &Page
pub fn last_page(&self) -> &Page
Returns an immutable reference to the last page of the document.
Sourcepub fn last_page_mut(&mut self) -> &mut Page
pub fn last_page_mut(&mut self) -> &mut Page
Returns a mutable reference to the last page of the document.
Sourcepub fn add_builtin_font(
&self,
builtin: BuiltinFont,
) -> Result<IndirectFontRef, Error>
pub fn add_builtin_font( &self, builtin: BuiltinFont, ) -> Result<IndirectFontRef, Error>
Loads the builtin font and returns a reference to it.
§Examples
use genpdfi_extended::render::Renderer;
use genpdfi_extended::Size;
use printpdf::BuiltinFont;
let r = Renderer::new(Size::new(210.0, 297.0), "ex").expect("renderer");
let f = r.add_builtin_font(BuiltinFont::Helvetica).expect("builtin");
match f { genpdfi_extended::render::IndirectFontRef::Builtin(_) => {}, _ => panic!("expected builtin") }Sourcepub fn add_embedded_font(
&mut self,
data: &[u8],
) -> Result<IndirectFontRef, Error>
pub fn add_embedded_font( &mut self, data: &[u8], ) -> Result<IndirectFontRef, Error>
Loads an embedded font from the given data and returns a reference to it.
§Examples
use genpdfi_extended::render::Renderer;
use genpdfi_extended::Size;
// Add a font from bundled bytes
let mut r = Renderer::new(Size::new(210.0, 297.0), "ex").expect("renderer");
let data = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/fonts/NotoSans-Regular.ttf"));
let font_ref = r.add_embedded_font(data).expect("add font");
match font_ref {
genpdfi_extended::render::IndirectFontRef::External(_) => {}
_ => panic!("expected external font"),
}Sourcepub fn write(self, w: impl Write) -> Result<(), Error>
pub fn write(self, w: impl Write) -> Result<(), Error>
Writes this PDF document to a writer.
§Examples
use genpdfi_extended::render::Renderer;
use genpdfi_extended::Size;
let r = Renderer::new(Size::new(210.0, 297.0), "ex").expect("renderer");
let mut buf = Vec::new();
r.write(&mut buf).expect("write");
assert!(!buf.is_empty());Auto Trait Implementations§
impl Freeze for Renderer
impl !RefUnwindSafe for Renderer
impl !Send for Renderer
impl !Sync for Renderer
impl Unpin for Renderer
impl !UnwindSafe for Renderer
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.