How to Embed Typst Typesetting Directly in PHP with Ext‑Typst

Ext‑Typst is a PHP extension that integrates the modern Typst typesetting engine, allowing native compilation of Typst markup to PDF, PNG, JPEG, and SVG within PHP applications, with multiple installation options, IDE support, and examples for rapid PDF generation.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Embed Typst Typesetting Directly in PHP with Ext‑Typst

Overview

Ext‑Typst is a PHP extension that embeds the Typst typesetting engine directly into PHP, allowing native compilation of Typst markup to PDF, PNG, JPEG, and SVG without invoking an external CLI or spawning subprocesses.

Installation

Via PIE (recommended)

pie install carthage-software/ext-typst

Pre‑compiled binary

Download the ZIP for your platform from GitHub Releases; extract the typst.so file and add the following line to php.ini:

extension=/path/to/typst.so

IDE / static analysis (development)

composer require --dev carthage-software/ext-typst

This provides autocompletion in PhpStorm and works with static analysis tools such as Mago, PHPStan, and Psalm.

Quick start

$world = new Typst\World();
$compiler = new Typst\Compiler($world);

const TEMPLATE = <<<"TYPST"
#set page(height: auto)
= Hello from Typst

开源技术小栈

This is a *bold* statement with _italic_ flair.
TYPST;

$document = $compiler->compileString(TEMPLATE);
$document->toPdf()->save('output.pdf');
$document->toImage()->save('output.png');
$document->toSvg()->save('output.svg');

Full documentation: https://ext-typst.carthage.software

Supported platforms

Linux – x86_64

Linux – aarch64

macOS – arm64

Supported PHP versions: 8.3, 8.4, 8.5

Use cases

Ideal for generating high‑quality PDFs, images, or SVGs dynamically within PHP web applications—such as invoices, reports, resumes, or academic papers—offering a native, high‑performance alternative to using exec() with the external Typst CLI.

backend developmentPHPExtensionpdf-generationTypst
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.