Skip to content

Markdown

Dragon edited this page Jun 4, 2026 · 2 revisions

Markdown

Render a CommonMark core subset directly to flowing PDF content. Two surfaces are available: Page::markdown() lays out text from the current cursor position and auto-paginates when the document has auto-break enabled, while cell(markdown: true) renders Markdown into a cell's inner box and auto-sizes the cell's height.

Basic example

use DragonOfMercy\PhpPdf\Document;
use DragonOfMercy\PhpPdf\Markdown\MarkdownStyle;

$doc = new Document();
$doc->setAutoPageBreak(true);
$page = $doc->addPage();
$page->setFont($doc->getFont('Helvetica'), 10);

// Flowing Markdown from the cursor, auto-paginating when auto-break is on.
$page->markdown(<<<MD
# Invoice

Thank you for your order. See **terms** below.

- Item one
- Item two

> Net 30 days.
MD, x: 20, y: 20);

// Markdown inside a cell (height is auto-sized; w is required).
$page->cell(x: 20, y: 120, w: 80, text: "## Notes\n\nHandle with *care*.", border: Border::all(), markdown: true);

$doc->save('document.pdf');

Page::markdown() signature

Page::markdown(
    string        $markdown,
    ?float        $x     = null,
    ?float        $y     = null,
    ?float        $width = null,
    ?MarkdownStyle $style = null,
    NextPosition  $ln    = NextPosition::BELOW,
): void
  • $x / $y - cursor position in the document unit; defaults to the current cursor position.
  • $width - render width; defaults to the page width minus the right margin minus $x.
  • $style - styling configuration (see below); defaults to MarkdownStyle::default().
  • $ln - cursor advance after rendering. Defaults to BELOW (the cursor moves to the block's left edge, just under the content) so consecutive markdown() / cell() calls flow down the page. Pass NONE to leave the cursor where it was, or NEWLINE.

When setAutoPageBreak(true) is active, Page::markdown() will emit new pages as required. When auto-break is off, the entire block is rendered atomically on the current page, which may overflow.

cell(markdown: true) usage

Pass markdown: true as a named argument to Page::cell(). The cell auto-sizes its height to fit the rendered Markdown. A non-null w is required.

$page->cell(x: 20, y: 60, w: 100, text: "## Section\n\nSome *body* text.", markdown: true);

MarkdownStyle

Start from MarkdownStyle::default() and chain the immutable withers below. All spacing values are in the document unit; font sizes are in points.

use DragonOfMercy\PhpPdf\Color;
use DragonOfMercy\PhpPdf\Markdown\MarkdownStyle;

$style = MarkdownStyle::default()
    ->withBodySize(11)
    ->withLinkColor(Color::hex('#003366'))
    ->withCodeBackground(Color::rgb(245, 245, 245));

$page->markdown($markdown, x: 20, y: 20, style: $style);
Wither Parameter Effect
withHeadingSize(int $level, float $size) level 1-6, size in pt Override the font size for a single heading level.
withBodySize(?float $size) size in pt, or null to inherit from the active font Override the body text size.
withParagraphSpacing(float $spacing) document unit Space added after each paragraph.
withCodeFont(Font $font) Font instance Font used for inline code and code blocks (default: Courier).
withCodeBackground(?Color $color) Color or null Background fill for code blocks.
withLinkColor(Color $color) Color Color applied to link text (default: blue rgb(0, 0, 238)).
withLinkUnderline(bool $underline) bool Whether link text is underlined (default: true).
withBlockQuoteBar(Color $color, ?float $width, ?float $indent) color; width and indent in document unit, nullable to keep current Left bar color, width, and indent for block quotes.
withListIndent(float $indent) document unit Per-level indent for unordered and ordered lists.

Supported CommonMark subset

  • ATX headings (# to ######) and paragraphs.
  • Inline bold (** / __), italic (* / _), bold+italic (***), and inline code (`code`).
  • Links [text](url) and images ![alt](src) - the image source is a local file path or a data: URI.
  • Unordered and ordered lists, including nesting.
  • Fenced (```) and indented (4-space) code blocks.
  • Block quotes (>) and thematic breaks (---).

Not supported

The following are silently skipped or rendered as literal text - they never cause an error:

  • Tables
  • Reference links
  • Footnotes
  • Task lists
  • Raw HTML (escaped to literal text)
  • Setext headings
  • Syntax highlighting in code blocks
  • Autolinks

See also

Clone this wiki locally