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>
impl<'tab> PdfBuilder<'tab>
Sourcepub fn new(tab: &'tab Tab) -> Self
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?;Sourcepub fn landscape(self, on: bool) -> Self
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?;Sourcepub fn print_background(self, on: bool) -> Self
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?;Sourcepub fn scale(self, scale: f64) -> Self
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?;Sourcepub fn paper_width(self, inches: f64) -> Self
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?;Sourcepub fn paper_height(self, inches: f64) -> Self
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?;Sourcepub fn margin_top(self, inches: f64) -> Self
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?;Sourcepub fn margin_bottom(self, inches: f64) -> Self
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?;Sourcepub fn margin_left(self, inches: f64) -> Self
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?;Sourcepub fn margin_right(self, inches: f64) -> Self
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?;Sourcepub fn page_ranges(self, ranges: impl Into<String>) -> Self
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?;Sourcepub fn prefer_css_page_size(self, on: bool) -> Self
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?;Sourcepub async fn bytes(self) -> Result<Vec<u8>>
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?;Sourcepub async fn save(self, path: impl AsRef<Path>) -> Result<()>
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?;