parse_into_bytes

Function parse_into_bytes 

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

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.

The process begins by parsing the Markdown content into a structured token representation. It then applies styling rules based on the provided configuration source. Finally, it generates the PDF document with the appropriate styling and structure.

§Arguments

  • markdown - The Markdown content to convert
  • config - Configuration source (Default, File path, or Embedded TOML)

§Returns

  • Ok(Vec<u8>) containing the PDF data on successful conversion
  • Err(MdpError) if errors occur during parsing or PDF generation

§Example

use std::fs;
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 embedded configuration
    const EMBEDDED: &str = r#"
        [heading.1]
        size = 18
        bold = true
    "#;
    let pdf_bytes = markdown2pdf::parse_into_bytes(markdown, ConfigSource::Embedded(EMBEDDED), None)?;

    // Save to file or send over network
    fs::write("output.pdf", pdf_bytes)?;
    Ok(())
}