pub struct PdfDocument<W: Write> { /* private fields */ }Expand description
High-level API for building PDF documents.
Generic over Write so it works with files (BufWriter<File>),
in-memory buffers (Vec<u8>), or any other writer.
Pages are written incrementally: end_page() flushes page data
to the writer and frees page content from memory. This keeps
memory usage low even for documents with hundreds of pages.
Implementations§
Source§impl PdfDocument<BufWriter<File>>
impl PdfDocument<BufWriter<File>>
Source§impl<W: Write> PdfDocument<W>
impl<W: Write> PdfDocument<W>
Sourcepub fn new(writer: W) -> Result<Self>
pub fn new(writer: W) -> Result<Self>
Create a new PDF document that writes to the given writer. Writes the PDF header immediately.
Sourcepub fn set_info(&mut self, key: &str, value: &str) -> &mut Self
pub fn set_info(&mut self, key: &str, value: &str) -> &mut Self
Set a document info entry (e.g. “Creator”, “Title”).
Sourcepub fn set_compression(&mut self, enabled: bool) -> &mut Self
pub fn set_compression(&mut self, enabled: bool) -> &mut Self
Enable or disable FlateDecode compression for stream objects. When enabled, page content, embedded fonts, and ToUnicode CMaps are compressed, typically reducing file size by 50-80%. Disabled by default.
Sourcepub fn load_font_file<P: AsRef<Path>>(
&mut self,
path: P,
) -> Result<FontRef, String>
pub fn load_font_file<P: AsRef<Path>>( &mut self, path: P, ) -> Result<FontRef, String>
Load a TrueType font from a file path. Returns a FontRef that can be used in TextStyle.
Sourcepub fn load_font_bytes(&mut self, data: Vec<u8>) -> Result<FontRef, String>
pub fn load_font_bytes(&mut self, data: Vec<u8>) -> Result<FontRef, String>
Load a TrueType font from raw bytes. Returns a FontRef that can be used in TextStyle.
Sourcepub fn page_count(&self) -> usize
pub fn page_count(&self) -> usize
Returns the number of completed pages (pages for which end_page has been called).
Sourcepub fn begin_page(&mut self, width: f64, height: f64) -> &mut Self
pub fn begin_page(&mut self, width: f64, height: f64) -> &mut Self
Begin a new page with the given dimensions in points. If a page is currently open, it is automatically closed.
Sourcepub fn open_page(&mut self, page_num: usize) -> Result<()>
pub fn open_page(&mut self, page_num: usize) -> Result<()>
Open a completed page for editing (1-indexed).
Used for adding overlay content such as page numbers (“Page X of Y”)
after all pages have been written. The overlay content is written as
an additional content stream appended to the page’s /Contents array.
If a page is currently open, it is automatically closed first.
Returns an error if page_num is out of range.
Sourcepub fn place_text(&mut self, text: &str, x: f64, y: f64) -> &mut Self
pub fn place_text(&mut self, text: &str, x: f64, y: f64) -> &mut Self
Place text at position (x, y) using default 12pt Helvetica. Coordinates use PDF’s default bottom-left origin.
Sourcepub fn place_text_styled(
&mut self,
text: &str,
x: f64,
y: f64,
style: &TextStyle,
) -> &mut Self
pub fn place_text_styled( &mut self, text: &str, x: f64, y: f64, style: &TextStyle, ) -> &mut Self
Place text at position (x, y) with the given style. Coordinates use PDF’s default bottom-left origin.
Sourcepub fn fit_textflow(
&mut self,
flow: &mut TextFlow,
rect: &Rect,
) -> Result<FitResult>
pub fn fit_textflow( &mut self, flow: &mut TextFlow, rect: &Rect, ) -> Result<FitResult>
Fit a TextFlow into a bounding rectangle on the current page. The flow’s cursor advances so subsequent calls continue where it left off (for multi-page flow).
Sourcepub fn fit_row(
&mut self,
table: &Table,
row: &Row,
cursor: &mut TableCursor,
) -> Result<FitResult>
pub fn fit_row( &mut self, table: &Table, row: &Row, cursor: &mut TableCursor, ) -> Result<FitResult>
Place a single table row on the current page.
cursor tracks the current Y position within the page. Pass the same
cursor to successive calls; call cursor.reset() when starting a new page.
Returns:
Stop— row placed; advance to the next row.BoxFull— page full; end the page, begin a new one, reset the cursor, retry.BoxEmpty— rect too small for this row even from the top; skip or abort.
Sourcepub fn load_image_file<P: AsRef<Path>>(
&mut self,
path: P,
) -> Result<ImageId, String>
pub fn load_image_file<P: AsRef<Path>>( &mut self, path: P, ) -> Result<ImageId, String>
Load an image from a file path.
Returns an ImageId that can be used with place_image.
Sourcepub fn load_image_bytes(&mut self, data: Vec<u8>) -> Result<ImageId, String>
pub fn load_image_bytes(&mut self, data: Vec<u8>) -> Result<ImageId, String>
Load an image from raw bytes (JPEG or PNG).
Returns an ImageId that can be used with place_image.
Sourcepub fn place_image(
&mut self,
image: &ImageId,
rect: &Rect,
fit: ImageFit,
) -> &mut Self
pub fn place_image( &mut self, image: &ImageId, rect: &Rect, fit: ImageFit, ) -> &mut Self
Place an image on the current page within the given bounding rect.
Sourcepub fn add_text_field(
&mut self,
name: &str,
rect: Rect,
) -> Result<(), FormFieldError>
pub fn add_text_field( &mut self, name: &str, rect: Rect, ) -> Result<(), FormFieldError>
Add a fillable text field to the current page.
name must be unique across the document. Returns an error if called
with no active page or if the name has already been used.
Sourcepub fn set_stroke_color(&mut self, color: Color) -> &mut Self
pub fn set_stroke_color(&mut self, color: Color) -> &mut Self
Set the stroke color (PDF RG operator).
Sourcepub fn set_fill_color(&mut self, color: Color) -> &mut Self
pub fn set_fill_color(&mut self, color: Color) -> &mut Self
Set the fill color (PDF rg operator).
Sourcepub fn set_line_width(&mut self, width: f64) -> &mut Self
pub fn set_line_width(&mut self, width: f64) -> &mut Self
Set the line width (PDF w operator).
Sourcepub fn move_to(&mut self, x: f64, y: f64) -> &mut Self
pub fn move_to(&mut self, x: f64, y: f64) -> &mut Self
Move to a point without drawing (PDF m operator).
Sourcepub fn line_to(&mut self, x: f64, y: f64) -> &mut Self
pub fn line_to(&mut self, x: f64, y: f64) -> &mut Self
Draw a line from the current point (PDF l operator).
Sourcepub fn rect(&mut self, x: f64, y: f64, width: f64, height: f64) -> &mut Self
pub fn rect(&mut self, x: f64, y: f64, width: f64, height: f64) -> &mut Self
Append a rectangle to the path (PDF re operator).
Sourcepub fn close_path(&mut self) -> &mut Self
pub fn close_path(&mut self) -> &mut Self
Close the current subpath (PDF h operator).
Sourcepub fn fill_stroke(&mut self) -> &mut Self
pub fn fill_stroke(&mut self) -> &mut Self
Fill and stroke the current path (PDF B operator).
Sourcepub fn save_state(&mut self) -> &mut Self
pub fn save_state(&mut self) -> &mut Self
Save the graphics state (PDF q operator).
Sourcepub fn restore_state(&mut self) -> &mut Self
pub fn restore_state(&mut self) -> &mut Self
Restore the graphics state (PDF Q operator).
Sourcepub fn end_page(&mut self) -> Result<()>
pub fn end_page(&mut self) -> Result<()>
End the current page. Writes the content stream to the writer
and frees page content from memory. The page dictionary is
deferred until end_document() so overlay streams can be added.
Sourcepub fn end_document(self) -> Result<W>
pub fn end_document(self) -> Result<W>
Finish the document. Writes page dictionaries, the catalog, pages tree, info dictionary, xref table, and trailer. Consumes self – no further operations are possible.