Skip to main content

Crate markdown2pdf

Crate markdown2pdf 

Source
Expand description

The markdown2pdf library enables conversion of Markdown content into professionally styled PDF documents. It provides a complete pipeline for parsing Markdown text, applying configurable styling rules, and generating polished PDF output.

The library handles the intricacies of Markdown parsing and PDF generation while giving users control over the visual presentation through styling configuration. Users can customize fonts, colors, spacing, and other visual properties via a TOML configuration file.

Basic usage involves passing Markdown content as a string along with an output path:

use markdown2pdf;
use markdown2pdf::config::ConfigSource;
use std::error::Error;

// Convert Markdown string to PDF with proper error handling
fn example() -> Result<(), Box<dyn Error>> {
    let markdown = "# Hello World\nThis is a test.".to_string();
    markdown2pdf::parse_into_file(markdown, "output.pdf", ConfigSource::Default, None)?;
    Ok(())
}

For more control over the output styling, users can create a configuration file (markdown2pdfrc.toml) to specify custom visual properties:

use markdown2pdf;
use markdown2pdf::config::ConfigSource;
use std::fs;
use std::error::Error;

// Read markdown file with proper error handling
fn example_with_styling() -> Result<(), Box<dyn Error>> {
    let markdown = fs::read_to_string("input.md")?;
    markdown2pdf::parse_into_file(markdown, "styled-output.pdf", ConfigSource::Default, None)?;
    Ok(())
}

The library also handles rich content like images and links seamlessly:

use markdown2pdf;
use markdown2pdf::config::ConfigSource;
use std::error::Error;

fn example_with_rich_content() -> Result<(), Box<dyn Error>> {
    let markdown = r#"

    ![Logo](./images/logo.png)

    See our [website](https://example.com) for more info.
    "#.to_string();

    markdown2pdf::parse_into_file(markdown, "doc-with-images.pdf", ConfigSource::Default, None)?;
    Ok(())
}

The styling configuration file supports comprehensive customization of the document appearance. Page layout properties control the overall document structure:

[page]
margins = { top = 72, right = 72, bottom = 72, left = 72 }
size = "a4"
orientation = "portrait"

Individual elements can be styled with precise control:

[heading.1]
size = 24
textcolor = { r = 0, g = 0, b = 0 }
bold = true
afterspacing = 1.0

[text]
size = 12
fontfamily = "roboto"
alignment = "left"

[code]
backgroundcolor = { r = 245, g = 245, b = 245 }
fontfamily = "roboto-mono"

The conversion process follows a carefully structured pipeline. First, the Markdown text undergoes lexical analysis to produce a stream of semantic tokens. These tokens then receive styling rules based on the configuration. Finally, the styled elements are rendered into the PDF document.

§Token Processing Flow

+-------------+     +----------------+     +----------------+
|  Markdown   |     |  Tokens        |     |  PDF Elements  |
|  Input      |     |  # -> Heading  |     |  - Styled      |
|  # Title    | --> |  * -> List     | --> |    Heading     |
|  * Item     |     |  > -> Quote    |     |  - List with   |
|  > Quote    |     |                |     |    bullets     |
+-------------+     +----------------+     +----------------+

+---------------+     +------------------+     +--------------+
| Styling       |     | Font Loading     |     | Output:      |
| - Font sizes  | --> | - Font families  | --> | Final        |
| - Colors      |     | - Weights        |     | Rendered     |
| - Margins     |     | - Styles         |     | PDF Document |
+---------------+     +------------------+     +--------------+

Modules§

config
Configuration loading: TOML on disk / embedded string → ResolvedStyle.
fonts
Font configuration and resolution.
frontmatter
Detect and parse YAML or TOML front-matter at the top of a markdown document.
markdown
Markdown lexical analysis and token representation.
render
PDF renderer for markdown2pdf.
styling
Styling module — config schema, theme presets, merge / resolve pipeline, and the concrete ResolvedStyle consumed by the renderer.
validation
Validation and warning system for markdown2pdf

Enums§

MdpError
Represents errors that can occur during the markdown-to-pdf conversion process. This includes both parsing failures and PDF generation issues.

Functions§

parse_into_bytes
Transforms Markdown content into a styled PDF document and returns the PDF data as bytes. This function provides the same conversion pipeline as parse_into_file but returns the PDF content directly as a byte vector instead of writing to a file.
parse_into_bytes_with_style
Variant of parse_into_bytes that takes a pre-resolved style instead of a ConfigSource. Mirrors parse_into_file_with_style for callers that already have a ResolvedStyle in hand (web services, in-memory pipelines).
parse_into_file
parse_into_file_with_style
Transforms Markdown content into a styled PDF document and saves it to the specified path. This function provides a high-level interface for converting Markdown to PDF with configurable styling through TOML configuration files.