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 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(markdown, "output.pdf")?;
Ok(())
}
For more control over the output styling, users can create a configuration file (markdown2pdfrc.toml) to specify custom visual properties:
use markdown2pdf;
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(markdown, "styled-output.pdf")?;
Ok(())
}
The library also handles rich content like images and links seamlessly:
use markdown2pdf;
use std::error::Error;
fn example_with_rich_content() -> Result<(), Box<dyn Error>> {
let markdown = r#"

See our [website](https://example.com) for more info.
"#.to_string();
markdown2pdf::parse(markdown, "doc-with-images.pdf")?;
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§
- assets
- Asset management module for embedded fonts and resources.
- config
- Configuration module for styling and formatting PDF output.
- markdown
- Markdown lexical analysis and token representation.
- PDF generation module for markdown-to-pdf conversion.
- styling
- Styling module for markdown-to-pdf conversion.
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
- Transforms Markdown content into a styled PDF document. The function orchestrates the entire conversion pipeline, from parsing the input text through generating the final PDF file.