Skip to main content

PdfBuilder

Struct PdfBuilder 

Source
pub struct PdfBuilder<'tab> { /* private fields */ }
Expand description

Chainable PDF export bound to a Tab.

Default state: every knob unset, so Self::bytes dispatches Page.printToPDF with an empty parameter object and Chrome applies its own defaults. Terminate the chain with Self::bytes (raw PDF bytes) or Self::save (write to file).

Implementations§

Source§

impl<'tab> PdfBuilder<'tab>

Source

pub fn new(tab: &'tab Tab) -> Self

Construct a fresh builder bound to tab with no overrides.

Every knob starts unset; Self::bytes then dispatches Page.printToPDF with an empty parameter object so Chrome uses its own defaults. Most users go through Tab::pdf_builder.

§Examples
let bytes = PdfBuilder::new(&tab).bytes().await?;
Source

pub fn landscape(self, on: bool) -> Self

Orient the page in landscape (true) or portrait (false).

Maps to Page.printToPDF { landscape }. Unset leaves Chrome’s default (portrait).

§Examples
tab.pdf_builder().landscape(true).save("wide.pdf").await?;
Source

pub fn print_background(self, on: bool) -> Self

Render the page’s background graphics (colors / images).

Maps to Page.printToPDF { printBackground }. Off by default in Chrome, so set this to true for WYSIWYG output.

§Examples
tab.pdf_builder().print_background(true).save("solid.pdf").await?;
Source

pub fn scale(self, scale: f64) -> Self

Scale the rendered content (1.0 = 100%).

Maps to Page.printToPDF { scale }. Chrome clamps to roughly 0.1..=2.0.

§Examples
tab.pdf_builder().scale(0.75).save("smaller.pdf").await?;
Source

pub fn paper_width(self, inches: f64) -> Self

Paper width in inches.

Maps to Page.printToPDF { paperWidth }. Pair with Self::paper_height for a custom sheet; e.g. A4 portrait is 8.27 × 11.69.

§Examples
tab.pdf_builder().paper_width(8.27).paper_height(11.69).save("a4.pdf").await?;
Source

pub fn paper_height(self, inches: f64) -> Self

Paper height in inches.

Maps to Page.printToPDF { paperHeight }. See Self::paper_width.

§Examples
tab.pdf_builder().paper_width(8.27).paper_height(11.69).save("a4.pdf").await?;
Source

pub fn margin_top(self, inches: f64) -> Self

Top margin in inches.

Maps to Page.printToPDF { marginTop }.

§Examples
tab.pdf_builder().margin_top(0.5).save("margins.pdf").await?;
Source

pub fn margin_bottom(self, inches: f64) -> Self

Bottom margin in inches.

Maps to Page.printToPDF { marginBottom }.

§Examples
tab.pdf_builder().margin_bottom(0.5).save("margins.pdf").await?;
Source

pub fn margin_left(self, inches: f64) -> Self

Left margin in inches.

Maps to Page.printToPDF { marginLeft }.

§Examples
tab.pdf_builder().margin_left(0.5).save("margins.pdf").await?;
Source

pub fn margin_right(self, inches: f64) -> Self

Right margin in inches.

Maps to Page.printToPDF { marginRight }.

§Examples
tab.pdf_builder().margin_right(0.5).save("margins.pdf").await?;
Source

pub fn page_ranges(self, ranges: impl Into<String>) -> Self

Restrict output to the given page ranges (e.g. "1-5, 8, 11-13").

Maps to Page.printToPDF { pageRanges }. Empty / unset prints all pages.

§Examples
tab.pdf_builder().page_ranges("1-3").save("first3.pdf").await?;
Source

pub fn prefer_css_page_size(self, on: bool) -> Self

Prefer any CSS-declared @page size over Self::paper_width / Self::paper_height.

Maps to Page.printToPDF { preferCSSPageSize }. When true, an explicit paper_width / paper_height is ignored in favor of the page’s own @page { size: ... } rule.

§Examples
tab.pdf_builder().prefer_css_page_size(true).save("css.pdf").await?;
Source

pub async fn bytes(self) -> Result<Vec<u8>>

Execute the export and return the raw PDF bytes.

Sends Page.printToPDF with every set knob and base64-decodes the response’s data field into the returned Vec<u8>. Unset knobs are omitted so Chrome’s defaults apply.

§Errors

Returns ZendriverError::Navigation when Chrome returns no PDF data or malformed base64.

§Examples
let bytes = tab.pdf_builder().bytes().await?;
tokio::fs::write("out.pdf", bytes).await?;
Source

pub async fn save(self, path: impl AsRef<Path>) -> Result<()>

Execute the export and write the raw PDF bytes to path.

Convenience wrapper over Self::bytes + tokio::fs::write.

§Examples
tab.pdf_builder().landscape(true).save("page.pdf").await?;

Trait Implementations§

Source§

impl<'tab> Debug for PdfBuilder<'tab>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'tab> !RefUnwindSafe for PdfBuilder<'tab>

§

impl<'tab> !UnwindSafe for PdfBuilder<'tab>

§

impl<'tab> Freeze for PdfBuilder<'tab>

§

impl<'tab> Send for PdfBuilder<'tab>

§

impl<'tab> Sync for PdfBuilder<'tab>

§

impl<'tab> Unpin for PdfBuilder<'tab>

§

impl<'tab> UnsafeUnpin for PdfBuilder<'tab>

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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more