Skip to content

WASM Playground

Paul Gaston GOURON edited this page Apr 18, 2026 · 2 revisions

WASM and Playground

ironpress compiles to WebAssembly for browser-side PDF generation.

WASM Feature

Enable with:

[dependencies]
ironpress = { version = "1.4", features = ["wasm"] }

Exported Functions

#[wasm_bindgen(js_name = "htmlToPdf")]
pub fn html_to_pdf(html: &str) -> Result<Uint8Array, JsError>

#[wasm_bindgen(js_name = "markdownToPdf")]
pub fn markdown_to_pdf(md: &str) -> Result<Uint8Array, JsError>

#[wasm_bindgen(js_name = "htmlToPdfCustom")]
pub fn html_to_pdf_custom(
    html: &str,
    page_width: f32, page_height: f32,
    margin_top: f32, margin_right: f32,
    margin_bottom: f32, margin_left: f32,
) -> Result<Uint8Array, JsError>

Building

wasm-pack build --target web --features wasm

Produces:

  • pkg/ironpress_bg.wasm: WASM binary (~900 KB)
  • pkg/ironpress.js: JS glue code
  • pkg/ironpress.d.ts: TypeScript definitions

Usage in JavaScript

import init, { htmlToPdf, markdownToPdf } from './ironpress.js';

await init();

const pdfBytes = htmlToPdf('<h1>Hello</h1><p>World</p>');
const blob = new Blob([pdfBytes], { type: 'application/pdf' });
const url = URL.createObjectURL(blob);
window.open(url);

Usage with Markdown + Math

const pdfBytes = markdownToPdf(`
# Euler's Identity

$$e^{i\\pi} + 1 = 0$$
`);

Playground

Location: playground/index.html

Interactive web app for testing ironpress in the browser.

Architecture

[Editor Panel]     [Preview Panel]
  textarea    →    WASM convert    →    PDF iframe
     ↑                                      ↑
  examples                              blob URL
  dropdown

Features

  • Split-panel UI: code editor on left, PDF preview on right
  • Mode selector: HTML or Markdown
  • Example loader with prebuilt templates:
    • Simple page
    • Invoice
    • Report
    • Markdown doc
    • Math paper (LaTeX formulas)
  • Real-time PDF generation via WASM
  • PDF download button
  • Loading indicator during WASM initialization

Example Templates

Template Content
Simple page Basic HTML with heading and paragraph
Invoice Styled invoice with table, flex layout, header/footer
Report Multi-section report with charts, tables, flex metrics
Markdown doc Full Markdown demo with headings, lists, code, links
Math paper LaTeX formulas: quadratic, Gauss sum, Euler, integrals, matrices

Hosting

The playground is a single HTML file with no build step. Serve with any static file server:

cd playground
python3 -m http.server 8080

The WASM binary must be in the same directory. Build with wasm-pack first.

Performance Notes

  • WASM compilation adds no runtime overhead beyond the initial load (~100ms)
  • PDF conversion runs synchronously on the main thread
  • For large documents, consider using a Web Worker to avoid blocking the UI
  • The remote feature is not available in WASM (no network access from WASM without JS bridges)

Clone this wiki locally