Skip to content

pdf

stable

Create and write PDF documents programmatically using the PdfDoc class, with support for text, bold text, lines, rectangles, metadata, and multi-page output.

use plugin pdf::{PdfDoc.new, add_page, add_text, …}
13 functions Image
/ filter jk navigate Esc clear
Functions (13)
  1. PdfDoc.new Create a new A4 PDF document
  2. add_page Add a new page with custom dimensions
  3. add_text Draw text on a page at mm coordinates
  4. add_text_bold Draw bold text on a page
  5. add_line Draw a line segment on a page
  6. add_rect Draw a rectangle outline on a page
  7. page_count Return number of pages in the document
  8. set_title Set document title metadata
  9. set_subject Set document subject metadata
  10. set_keywords Set document keywords metadata
  11. set_author Set document author metadata
  12. save Save the document to a file path
  13. to_bytes Serialise the document to a byte array

Create a new A4 PDF document

Creates a new A4 PDF document (210 × 297 mm) with an initial blank page and sets the document title. The document is consumed on save or to_bytes and cannot be reused after.

use plugin pdf::{PdfDoc}

let doc = PdfDoc.new("My Report")
doc.add_text(0, "Hello, PDF!", 20.0, 270.0, 14.0)
doc.save("report.pdf")

Add a new page with custom dimensions

Adds a new blank page with the given dimensions in millimetres. Returns the zero-based page index to use with drawing methods.

use plugin pdf::{PdfDoc}

let doc = PdfDoc.new("Multi-page")
let p2 = doc.add_page(297.0, 210.0)
doc.add_text(p2, "Page 2 (A4 landscape)", 20.0, 190.0, 12.0)
doc.save("multipage.pdf")

Draw text on a page at mm coordinates

Draws text on page page_idx at position (x_mm, y_mm) millimetres from the bottom-left corner, using Helvetica at font_size_pt points.

use plugin pdf::{PdfDoc}

let doc = PdfDoc.new("Invoice")
doc.add_text(0, "Invoice #1001", 20.0, 260.0, 18.0)
doc.add_text(0, "Date: 2026-06-09", 20.0, 245.0, 11.0)
doc.save("invoice.pdf")

Draw bold text on a page

Same as add_text but uses Helvetica-Bold. Useful for headings and labels.

use plugin pdf::{PdfDoc}

let doc = PdfDoc.new("Report")
doc.add_text_bold(0, "Summary", 20.0, 260.0, 16.0)
doc.add_text(0, "See details below.", 20.0, 250.0, 11.0)
doc.save("report.pdf")

Draw a line segment on a page

Draws a straight line from (x1, y1) to (x2, y2), all in millimetres from the bottom-left of the page.

use plugin pdf::{PdfDoc}

let doc = PdfDoc.new("Ruled")
doc.add_line(0, 15.0, 255.0, 195.0, 255.0)
doc.save("ruled.pdf")

Draw a rectangle outline on a page

Draws a rectangle outline with bottom-left corner at (x, y) and the given w × h dimensions, all in millimetres.

use plugin pdf::{PdfDoc}

let doc = PdfDoc.new("Boxes")
doc.add_rect(0, 20.0, 220.0, 80.0, 40.0)
doc.add_text(0, "Inside the box", 25.0, 250.0, 11.0)
doc.save("boxes.pdf")

Return number of pages in the document

Returns the total number of pages currently in the document, including the initial page created by PdfDoc.new.

use plugin pdf::{PdfDoc}

let doc = PdfDoc.new("Pages")
doc.add_page(210.0, 297.0)
doc.add_page(210.0, 297.0)
print("Pages: {doc.page_count()}")

Set document title metadata

Sets the PDF document title metadata field. Can be updated at any point before saving.

use plugin pdf::{PdfDoc}

let doc = PdfDoc.new("Draft")
doc.set_title("Final Report Q2 2026")
doc.save("final.pdf")

Set document subject metadata

Sets the PDF subject metadata field.

use plugin pdf::{PdfDoc}

let doc = PdfDoc.new("Report")
doc.set_subject("Quarterly Sales Analysis")
doc.save("report.pdf")

Set document keywords metadata

Sets PDF keywords metadata. Pass a comma-separated string; the plugin splits on commas automatically.

use plugin pdf::{PdfDoc}

let doc = PdfDoc.new("Report")
doc.set_keywords("sales, quarterly, 2026")
doc.save("report.pdf")

Set document author metadata

Sets the PDF author metadata field.

use plugin pdf::{PdfDoc}

let doc = PdfDoc.new("Proposal")
doc.set_author("Alice Smith")
doc.save("proposal.pdf")

Save the document to a file path

Writes the completed PDF to disk at path. The document handle is consumed — do not call any further methods after save.

use plugin pdf::{PdfDoc}

let doc = PdfDoc.new("Hello World")
doc.add_text(0, "Hello, World!", 20.0, 270.0, 24.0)
doc.save("hello.pdf")

Serialise the document to a byte array

Serialises the PDF to a raw byte array instead of writing to disk. Useful for serving PDFs over HTTP or passing them to another plugin. The handle is consumed after this call.

use plugin pdf::{PdfDoc}

let doc = PdfDoc.new("Receipt")
doc.add_text(0, "Total: $42.00", 20.0, 270.0, 12.0)
let bytes = doc.to_bytes()
print("PDF size: {bytes.len} bytes")
enespt-br