Renderer

Struct Renderer 

Source
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

Source

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());
Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn page_count(&self) -> usize

Returns the number of pages in the current document.

Source

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.

Source

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.).

Source

pub fn first_page(&self) -> &Page

Returns an immutable reference to the first page of the document.

Source

pub fn first_page_mut(&mut self) -> &mut Page

Returns a mutable reference to the first page of the document.

Source

pub fn last_page(&self) -> &Page

Returns an immutable reference to the last page of the document.

Source

pub fn last_page_mut(&mut self) -> &mut Page

Returns a mutable reference to the last page of the document.

Source

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") }
Source

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"),
}
Source

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§

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