parse_into_file

Function parse_into_file 

Source
pub fn parse_into_file(
    markdown: String,
    path: &str,
    config: ConfigSource<'_>,
    font_config: Option<&FontConfig>,
) -> Result<(), MdpError>
Expand description

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.

The process begins by parsing the Markdown content into a structured token representation. It then applies styling rules, either from a configuration file if present or using defaults. Finally, it generates the PDF document with the appropriate styling and structure.

§Arguments

  • markdown - The Markdown content to convert
  • path - The output file path for the generated PDF
  • config - Configuration source (Default, File path, or Embedded TOML)

§Returns

  • Ok(()) on successful PDF generation and save
  • Err(MdpError) if errors occur during parsing, styling, or file operations

§Example

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

fn example() -> Result<(), Box<dyn Error>> {
    let markdown = "# Hello World\nThis is a test.".to_string();

    // Use default configuration
    markdown2pdf::parse_into_file(markdown.clone(), "output1.pdf", ConfigSource::Default, None)?;

    // Use file-based configuration
    markdown2pdf::parse_into_file(markdown.clone(), "output2.pdf", ConfigSource::File("config.toml"), None)?;

    // Use embedded configuration with custom font
    const EMBEDDED: &str = r#"
        [heading.1]
        size = 18
        bold = true
    "#;
    let font_config = FontConfig {
        custom_paths: vec!["./fonts".into()],
        default_font: Some("Roboto".to_string()),
        code_font: None,
        fallback_fonts: vec![],
        enable_subsetting: true,
    };
    markdown2pdf::parse_into_file(markdown, "output3.pdf", ConfigSource::Embedded(EMBEDDED), Some(&font_config))?;

    Ok(())
}