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_barcodesfound. - Barcode
Options - Options of
read_barcodes. - Error
- A failed PdfPecker call: its
ErrorKindand the message the library gave. - Image
Options - Options of
pdf_to_images. - Margin
- Page margins in points (72 per inch). A CSS
@page { margin: ... }rule in the document wins over them. - Markdown
Options - Options of
pdf_to_markdown. - Merge
Options - Options of
merge_pdfs. - Page
Count Options - Options of
pdf_page_count. - Pages
- Which pages a call reads, numbered from 1.
- PdfA
Options - Options of
pdf_to_pdfa. - PdfA
Result - 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.
- Redact
Options - What
redact_pdfremoves, and how the removed places look. - Text
Options - Options of
pdf_to_textandpdf_to_text_pages.
Enums§
- Accessibility
- How much structure the PDF carries for screen readers and text extraction.
- Barcode
Format - A barcode symbology that
read_barcodescan find. - Conformance
- A PDF/A level for archiving.
- Error
Kind - What kind of failure an
Erroris, so callers can tell failures apart without matching messages. - Image
Format - The format of rendered pages.
- Page
Size - The paper size of the pages. A CSS
@page { size: ... }rule in the document wins over it. - Text
Order - The order in which
pdf_to_textreads 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_dirsays 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_dirsays 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.