PHPackages                             pforret/php\_markdown\_writer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. pforret/php\_markdown\_writer

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

pforret/php\_markdown\_writer
=============================

Write Markdown documents

1.1.2(4mo ago)145MITPHPPHP ^8.2CI passing

Since Jan 14Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/pforret/php_markdown_writer)[ Packagist](https://packagist.org/packages/pforret/php_markdown_writer)[ Docs](https://github.com/pforret/php_markdown_writer)[ RSS](/packages/pforret-php-markdown-writer/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (2)Dependencies (5)Versions (21)Used By (0)

Write Markdown documents
========================

[](#write-markdown-documents)

Github: [![GitHub tag](https://camo.githubusercontent.com/1feebeda07aa381b607f621f8b4b61659b08cd44c16df84ce389384f0810e8b6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f70666f727265742f7068705f6d61726b646f776e5f777269746572)](https://camo.githubusercontent.com/1feebeda07aa381b607f621f8b4b61659b08cd44c16df84ce389384f0810e8b6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f70666f727265742f7068705f6d61726b646f776e5f777269746572)[![Tests](https://github.com/pforret/php_markdown_writer/workflows/Run%20Tests/badge.svg)](https://github.com/pforret/php_markdown_writer/workflows/Run%20Tests/badge.svg)[![Psalm](https://github.com/pforret/php_markdown_writer/workflows/Detect%20Psalm%20warnings/badge.svg)](https://github.com/pforret/php_markdown_writer/workflows/Detect%20Psalm%20warnings/badge.svg)

Packagist: [![Packagist Version](https://camo.githubusercontent.com/8bef95b56810ae3ac9614740dc45cee22e59992318f1e582f4361229d52d518b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70666f727265742f7068705f6d61726b646f776e5f7772697465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pforret/php_markdown_writer)[![Packagist Downloads](https://camo.githubusercontent.com/089c54cb1da4347a8eddd77a6df40db2102fc2b0ebea2247ccb13d53b600c720/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70666f727265742f7068705f6d61726b646f776e5f7772697465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pforret/php_markdown_writer)

Write Markdown documents

```
created on 2022-01-14 by peter@forret.com

```

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require pforret/php_markdown_writer
```

Usage
-----

[](#usage)

### Basic Usage (in-memory)

[](#basic-usage-in-memory)

```
use Pforret\PhpMarkdownWriter\PhpMarkdownWriter;

$writer = new PhpMarkdownWriter();
$writer
    ->h1('Document Title')
    ->paragraph('Introduction text')
    ->h2('Section 1')
    ->bullet('Point 1')
    ->bullet('Point 2', 1)  // indented
    ->h2('Section 2')
    ->code('echo "Hello";', 'php');

echo $writer->asMarkdown();
```

### Write directly to file

[](#write-directly-to-file)

```
$writer = new PhpMarkdownWriter('output.md');
$writer->h1('Report')->paragraph('Content here.');
// File is written automatically when $writer goes out of scope
```

### Export to different formats

[](#export-to-different-formats)

```
$writer = new PhpMarkdownWriter();
$writer->h1('Title')->paragraph('Content');

// Save as Markdown
$writer->saveAsMarkdown('output.md');

// Save as HTML
$writer->saveAsHtml('output.html');

// Save as PDF (requires mpdf/mpdf)
$writer->saveAsPdf('output.pdf');

// Get HTML string
$html = $writer->asHtml();
```

Available Methods
-----------------

[](#available-methods)

### Headings &amp; Structure

[](#headings--structure)

- `h1($text)` - Level 1 heading
- `h2($text)` - Level 2 heading
- `h3($text)` - Level 3 heading
- `h4($text)` - Level 4 heading
- `h5($text)` - Level 5 heading
- `h6($text)` - Level 6 heading
- `hr()` - Horizontal rule/divider
- `pagebreak()` - Page break for PDF output (adds ``)

### Text Formatting

[](#text-formatting)

- `paragraph($text, $continued = false)` - Paragraph (use `$continued = true` for single line break)
- `bold($text, $continued = false)` - Bold text
- `italic($text, $continued = false)` - Italic text
- `strikethrough($text, $continued = false)` - Strikethrough text (text)
- `blockquote($text, $continued = false)` - Blockquote (&gt; text)

### Links &amp; Images

[](#links--images)

- `link($text, $url)` - Inline link [text](url)
- `image($alt, $url, $title = '')` - Image [![alt](url "title")](url)

### Lists

[](#lists)

- `bullet($text, $indent = 0)` - Bullet point (use `$indent` for nested items)
- `numbered($text, $number = 1, $indent = 0)` - Numbered list item
- `check($text, $indent = 0, $done = false)` - Checkbox item

### Code

[](#code)

- `code($text, $language = '')` - Fenced code block with optional language
- `fixed($text)` - Fixed-width/preformatted text (indented)
- `inlineCode($text)` - Returns inline code string (`text`)

### Tables

[](#tables)

- `table($array, $with_headers = true)` - Full table from array
- `table_header($array)` - Table header row
- `table_row($array)` - Table data row

### Output

[](#output)

- `asMarkdown()` - Get accumulated Markdown as string
- `asHtml()` - Convert to HTML string
- `saveAsMarkdown($filename)` - Save to Markdown file
- `saveAsHtml($filename)` - Save to HTML file
- `saveAsPdf($filename)` - Save to PDF file

### Utility

[](#utility)

- `reset()` - Clear accumulated content
- `setOutput($filename)` - Set file output after construction
- `markup($text)` - Auto-convert URLs and emails to links
- `getConverterConfig()` - Get current CommonMark converter config
- `setConverterConfig($config)` - Modify CommonMark converter settings
- `getPdfConfig()` - Get current mPDF config
- `setPdfConfig($config)` - Modify mPDF settings

### Converter Configuration

[](#converter-configuration)

The HTML converter can be configured with these options:

```
$writer = new PhpMarkdownWriter();
$writer->setConverterConfig([
    'html_input' => 'escape',      // 'strip', 'allow', or 'escape'
    'allow_unsafe_links' => false, // Block javascript:, vbscript:, etc.
]);
```

### PDF Configuration

[](#pdf-configuration)

The PDF output can be configured with these options:

```
$writer = new PhpMarkdownWriter();

// Quick font settings
$writer->setPdfFontFamily('DejaVuSerif');  // DejaVuSans, DejaVuSerif, FreeSans, etc.
$writer->setPdfFontSize(11);

// Or use full config
$writer->setPdfConfig([
    'format' => 'Letter',          // 'A4', 'Letter', 'A5', etc.
    'default_font_size' => 11,
    'margin_left' => 20,
    'margin_right' => 20,
]);
$writer->saveAsPdf('document.pdf');
```

### PDF Headers, Footers, and Metadata

[](#pdf-headers-footers-and-metadata)

Add headers, footers, metadata, and watermarks to PDF documents:

```
$writer = new PhpMarkdownWriter();

// Set document metadata
$writer->addPdfTitle('Annual Report 2025');
$writer->addPdfAuthor('John Doe');
$writer->addPdfSubject('Financial Overview');
$writer->addPdfKeywords('finance, report, annual, 2025');

// Set header and footer
$writer->addPdfHeader('My Document');
$writer->addPdfFooter('Page {PAGENO} of {nbpg}');

// Add watermark (optional)
$writer->addPdfWatermark('DRAFT');

$writer->h1('Title')->paragraph('Content...');
$writer->saveAsPdf('document.pdf');
```

Available mPDF variables: `{PAGENO}`, `{nbpg}`, `{DATE j-m-Y}`, `{DOCNUM}`

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email author\_email instead of using the issue tracker.

Credits
-------

[](#credits)

- [Peter Forret](https://github.com/pforret)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

44

—

FairBetter than 92% of packages

Maintenance76

Regular maintenance activity

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~76 days

Recently: every ~3 days

Total

20

Last Release

130d ago

Major Versions

0.3.8 → 1.0.02025-12-19

PHP version history (3 changes)0.1.0PHP ^7.4

0.2.0PHP ^7.4|^8.0

1.0.1PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/474312?v=4)[Peter Forret](/maintainers/pforret)[@pforret](https://github.com/pforret)

---

Top Contributors

[![pforret](https://avatars.githubusercontent.com/u/474312?v=4)](https://github.com/pforret "pforret (57 commits)")

---

Tags

markdownpforretphp\_markdown\_writer

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/pforret-php-markdown-writer/health.svg)

```
[![Health](https://phpackages.com/badges/pforret-php-markdown-writer/health.svg)](https://phpackages.com/packages/pforret-php-markdown-writer)
```

###  Alternatives

[daux/daux.io

Documentation generator that uses a simple folder structure and Markdown files to create custom documentation on the fly

825191.0k1](/packages/daux-dauxio)[bookdown/bookdown

Provides DocBook-like rendering of Markdown files.

8257.6k16](/packages/bookdown-bookdown)[torchlight/torchlight-commonmark

A Commonmark extension for Torchlight, the syntax highlighting API.

29256.6k6](/packages/torchlight-torchlight-commonmark)[maglnet/magl-markdown

Provides a ZF2 View Helper to render markdown syntax. It uses third-party libraries for the rendering and you can switch between different renderers.

22178.2k4](/packages/maglnet-magl-markdown)[zoon/commonmark-ext-youtube-iframe

Extension for league/commonmark to replace youtube link with iframe

12275.9k1](/packages/zoon-commonmark-ext-youtube-iframe)[spatie/commonmark-wire-navigate

Add a wire:navigate attribute to links rendered in Markdown

1010.7k](/packages/spatie-commonmark-wire-navigate)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
