Gotenberg PDF Client
gotenberg_pdf is a Rust library that provides an easy-to-use interface for interacting with the Gotenberg API. Use this library to convert URLs, HTML, or Markdown to PDF with highly customizable options.
Features
- URL to PDF: Generate PDFs directly from a webpage URL.
- HTML to PDF: Convert raw HTML into a PDF.
- Markdown to PDF: Render Markdown files into a PDF using an HTML template.
- Customizable Options: Configure PDF rendering with paper size, margins, headers/footers, and more.
Installation
Add gotenberg_pdf to your Cargo.toml:
[]
= "0.1.0" # Replace with the latest version
Ensure you have a running instance of Gotenberg, typically via Docker:
Usage Examples
Convert URL to PDF
use ;
use tokio;
async
Convert HTML to PDF
use ;
use tokio;
async
Convert Markdown to PDF
use ;
use HashMap;
use tokio;
async
Configuration Options
RequestOptions provides fine-grained control over the PDF generation process. Here are some of the available fields:
single_page: Render all content on a single page. Default isfalse.paper_width/paper_height: Set the paper size in inches. Defaults are8.5inches wide and11inches tall (standard letter size).margin_top,margin_bottom,margin_left,margin_right: Configure page margins. Defaults are0.39inches on all sides.header_html,footer_html: Add headers and footers to your PDF. These can include placeholders such as:{{date}}: Inserts the formatted print date.{{title}}: Inserts the document title.{{url}}: Inserts the document location.{{pageNumber}}: Inserts the current page number.{{totalPages}}: Inserts the total number of pages.
print_background: Enable background graphics in the PDF. Default isfalse.landscape: Set the page orientation to landscape. Default isfalse.prefer_css_page_size: Use the page size defined by CSS instead of the provided paper dimensions. Default isfalse.generate_document_outline: Embed the document outline (table of contents) into the PDF. Default isfalse.omit_background: Hide the default white background and generate a PDF with transparency. Default isfalse.scale: Set the rendering scale. Defaults to1.0.native_page_ranges: Specify a subset of pages to include in the output PDF (e.g.,1-5, 8, 11-13). Default is all pages.wait_delay: Wait for a specified duration (e.g.,5s) before converting an HTML document to PDF.wait_until: Provide a JavaScript condition to wait for before rendering (e.g.,window.isReady === true).emulated_media_type: Specify the media type to emulate ("screen"or"print"). Default is"print".cookies: Add a list of cookies to be used in the request.skip_network_idle_events: Set tofalseto wait for the network to be fully idle before rendering the PDF. Default istrue.