Skip to main content

Crate pdfpecker

Crate pdfpecker 

Source
Expand description

HTML and Markdown to PDF, and PDF to images, text and Markdown, with no browser.

This crate runs PdfPecker, a .NET library with its own HTML parser, CSS engine, layout engine, PDF writer and PDF renderer, compiled ahead of time into a native library: the same binary the pdfpecker packages on PyPI and npm ship. It runs no JavaScript and fetches nothing from the network.

let pdf = pdfpecker::html_to_pdf("<h1>Invoice 42</h1><p>Total: <b>€ 1,250.00</b></p>", &Default::default())?;
std::fs::write("invoice.pdf", &pdf)?;

let pngs = pdfpecker::pdf_to_images(&pdf, &pdfpecker::ImageOptions::new().dpi(96.0))?;
println!("{} page(s), {}", pngs.len(), pdfpecker::pdf_to_text(&pdf, &Default::default())?);

§The native library

The build script downloads PdfPecker’s native library for the target from PyPI (about 9 MB, once; it is cached), checks it against the SHA-256 values the crate carries, and puts it in Cargo’s build folder, where cargo run and cargo test find it. To deploy a program, copy pdfpecker_native.so (.dylib on macOS, .dll on Windows) next to its executable, or point PDFPECKER_NATIVE_LIB at it; library_path tells you which file was loaded. Offline builds set PDFPECKER_LIB_DIR to a folder that holds the library.

§Threads

Every function is safe to call from several threads at once, and conversions run in parallel. They block the calling thread, so async code runs them with tokio::task::spawn_blocking or its runtime’s equivalent.

Structs§

Barcode
A barcode that read_barcodes found.
BarcodeOptions
Options of read_barcodes.
Error
A failed PdfPecker call: its ErrorKind and the message the library gave.
ImageOptions
Options of pdf_to_images.
Margin
Page margins in points (72 per inch). A CSS @page { margin: ... } rule in the document wins over them.
MarkdownOptions
Options of pdf_to_markdown.
MergeOptions
Options of merge_pdfs.
PageCountOptions
Options of pdf_page_count.
Pages
Which pages a call reads, numbered from 1.
PdfAOptions
Options of pdf_to_pdfa.
PdfAResult
The outcome of pdf_to_pdfa: the converted PDF, what the conversion changed and what still breaks the rules.
PdfOptions
Options of HTML and Markdown conversion.
RedactOptions
What redact_pdf removes, and how the removed places look.
TextOptions
Options of pdf_to_text and pdf_to_text_pages.

Enums§

Accessibility
How much structure the PDF carries for screen readers and text extraction.
BarcodeFormat
A barcode symbology that read_barcodes can find.
Conformance
A PDF/A level for archiving.
ErrorKind
What kind of failure an Error is, so callers can tell failures apart without matching messages.
ImageFormat
The format of rendered pages.
PageSize
The paper size of the pages. A CSS @page { size: ... } rule in the document wins over it.
TextOrder
The order in which pdf_to_text reads a page’s text.

Functions§

html_file_to_pdf
Converts an HTML file to a PDF. Relative images, stylesheets and fonts resolve next to the file unless PdfOptions::base_dir says otherwise.
html_to_pdf
Converts HTML to a PDF and returns its bytes.
library_path
The native library file that was loaded, which is the file to ship with a program.
library_version
The version of the PdfPecker library inside the native library, which is also this crate’s version.
markdown_file_to_pdf
Converts a Markdown file to a PDF. Relative images resolve next to the file unless PdfOptions::base_dir says otherwise.
markdown_to_pdf
Converts Markdown (CommonMark with GitHub’s tables, task lists, strikethrough and autolinks) to a PDF.
merge_pdfs
Merges PDFs into one, keeping links, bookmarks and form fields.
pdf_page_count
The number of pages in a PDF.
pdf_to_images
Renders pages of a PDF, any PDF and not only one PdfPecker made, to PNG, JPEG or WebP images, one per page.
pdf_to_markdown
Converts a PDF, any PDF and not only one PdfPecker made, to GitHub Flavored Markdown: headings, paragraphs, lists and tables in reading order, for search indexes and language models.
pdf_to_pdfa
Converts an existing PDF, any PDF and not only one PdfPecker made, to PDF/A for long-term archiving.
pdf_to_text
Extracts the text of a PDF in reading order, pages separated by a form feed (\f).
pdf_to_text_pages
Extracts the text of each page, in reading order, as one string per page.
read_barcodes
Reads QR codes, Data Matrix codes, Code 128 and EAN-13 barcodes from an image (PNG, JPEG, GIF, WebP, BMP or TIFF) or from the pages of a PDF, which is recognised by its content.
redact_pdf
Removes text, images and drawings from a PDF for good, and draws a box where they were.

Type Aliases§

Result
The result type of every PdfPecker call.