Skip to main content

render_template

Function render_template 

Source
pub fn render_template(
    main_typ: String,
    file_system: Arc<dyn RenderFileSystem>,
    data: &Value,
) -> Result<RenderResult>
Expand description

Render a Typst template to PDF

This is the main public API for template compilation. It takes a template string, a file system for resolving imports, and JSON data to inject into the template.

§Arguments

  • main_typ - The main Typst template content as a string
  • file_system - File system abstraction for resolving imports and assets
  • data - JSON data to inject into the template

§Returns

Returns a RenderResult containing either the PDF bytes (on success) or detailed error information (on failure).

§Errors

This function can return various errors:

  • DataError - JSON serialization issues
  • CompilationError - Typst compilation failures
  • FileSystemError - File access issues during import resolution

§Example

use papermake::{render_template, typst::InMemoryFileSystem};
use std::sync::Arc;

let template = "Hello #data.name!";
let fs = Arc::new(InMemoryFileSystem::new());
let data = serde_json::json!({ "name": "World" });

let result = render_template(template.to_string(), fs, &data).unwrap();
if result.success {
    println!("PDF generated: {} bytes", result.pdf.unwrap().len());
} else {
    for error in result.errors {
        println!("Error: {}", error);
    }
}