PHPackages                             smnandre/pandoc - 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. [CLI &amp; Console](/categories/cli)
4. /
5. smnandre/pandoc

ActiveLibrary[CLI &amp; Console](/categories/cli)

smnandre/pandoc
===============

Pandoc PHP - Document Converter for Mardown, HTML, Latex, PDF, RST

v2.0.0(10mo ago)61.9k↑122.6%1MITPHPPHP ^8.3CI passing

Since Feb 2Pushed 10mo ago2 watchersCompare

[ Source](https://github.com/smnandre/pandoc)[ Packagist](https://packagist.org/packages/smnandre/pandoc)[ Docs](https://github.com/smnandre/pandoc/)[ GitHub Sponsors](https://github.com/smnandre)[ RSS](/packages/smnandre-pandoc/feed)WikiDiscussions main Synced yesterday

READMEChangelog (2)Dependencies (6)Versions (3)Used By (0)

![PANDOC PHP - Document Converter](./pandoc.dark.svg)
=====================================================

[](#)

 [![PHP Version](https://camo.githubusercontent.com/8f212facb5a00fc9c5e2601fb47f474a13088807809aaf8ea437805277624fae/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e332b2d3265376433323f6c6f676f436f6c6f723d364142373645266c6162656c436f6c6f723d303130)](https://camo.githubusercontent.com/8f212facb5a00fc9c5e2601fb47f474a13088807809aaf8ea437805277624fae/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e332b2d3265376433323f6c6f676f436f6c6f723d364142373645266c6162656c436f6c6f723d303130) [![CI](https://camo.githubusercontent.com/6361adb8c6f0a654d2cacaeaef52a1a7ccd02b071580b96e19e97d3f8dffaacf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736d6e616e6472652f70616e646f632f43492e79616d6c3f6272616e63683d6d61696e266c6162656c3d5465737473266c6f676f436f6c6f723d7768697465266c6f676f53697a653d6175746f266c6162656c436f6c6f723d30313026636f6c6f723d333838653363)](https://camo.githubusercontent.com/6361adb8c6f0a654d2cacaeaef52a1a7ccd02b071580b96e19e97d3f8dffaacf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f736d6e616e6472652f70616e646f632f43492e79616d6c3f6272616e63683d6d61696e266c6162656c3d5465737473266c6f676f436f6c6f723d7768697465266c6f676f53697a653d6175746f266c6162656c436f6c6f723d30313026636f6c6f723d333838653363) [![Release](https://camo.githubusercontent.com/8187e383b7ea85361b367989ac34a40b318fe9ba18680de937564ad0953f4395/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f736d6e616e6472652f70616e646f633f6c6162656c3d537461626c65266c6f676f436f6c6f723d7768697465266c6f676f53697a653d6175746f266c6162656c436f6c6f723d30313026636f6c6f723d343361303437)](https://camo.githubusercontent.com/8187e383b7ea85361b367989ac34a40b318fe9ba18680de937564ad0953f4395/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f736d6e616e6472652f70616e646f633f6c6162656c3d537461626c65266c6f676f436f6c6f723d7768697465266c6f676f53697a653d6175746f266c6162656c436f6c6f723d30313026636f6c6f723d343361303437) [![GitHub Sponsors](https://camo.githubusercontent.com/4cd34878abecbe785785110fd775ebfaa34e5bb3812b45e661a42c5e632a0f0c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73706f6e736f72732f736d6e616e6472653f6c6f676f3d6769746875622d73706f6e736f7273266c6f676f436f6c6f723d363662623661266c6f676f53697a653d6175746f266c6162656c3d25323053706f6e736f72266c6162656c436f6c6f723d30313026636f6c6f723d613564366137)](https://github.com/sponsors/smnandre) [![License](https://camo.githubusercontent.com/216ba5d4ace8ce7c455e5fdd83b470c204b84f43fcc420bee8f6c7588ee2b193/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f736d6e616e6472652f70616e646f633f6c6162656c3d4c6963656e7365266c6f676f436f6c6f723d7768697465266c6f676f53697a653d6175746f266c6162656c436f6c6f723d30313026636f6c6f723d326537643332)](https://camo.githubusercontent.com/216ba5d4ace8ce7c455e5fdd83b470c204b84f43fcc420bee8f6c7588ee2b193/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f736d6e616e6472652f70616e646f633f6c6162656c3d4c6963656e7365266c6f676f436f6c6f723d7768697465266c6f676f53697a653d6175746f266c6162656c436f6c6f723d30313026636f6c6f723d326537643332)

PHP [Pandoc](https://pandoc.org/) Document Converter. Typed, tested &amp; mockable.

---

Why Pandoc PHP
--------------

[](#why-pandoc-php)

Replace shell commands with type-safe objects and structured error handling.

```
$html = Pandoc::convert('# Hello', 'html');     // Static facade for quick tasks

$html = new Converter()                         // ConverterInterface
    ->file('document.txt')
    ->to('html')
    ->getContent();                             // Traceable, testable, mockable

Pandoc::file('reporting.md')                    // Options, variables,  metadata
    ->option('toc', true)
    ->metadata('author', 'John Doe')
    ->variable('title', 'My Document')
```

- **60+ formats** - **Markdown**, **HTML**, PDF, DOCX, LaTeX, **RST**, EPUB, RTF, and more
- **Dual API Design** - Static `Pandoc` facade for quick tasks, `ConverterInterface` for dependency injection
- **Smart Format Detection** - Auto-detects input/output formats from file extensions and content patterns
- **Unit Testing** - `MockConverter` for tests without Pandoc installation, real Pandoc for integration
- **Production Ready** - PHPStan level 10, full test coverage, rich error handling

---

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

[](#installation)

### Requirements

[](#requirements)

- PHP 8.3 or higher
- Pandoc (see below)

### Pandoc PHP

[](#pandoc-php)

Install via Composer.

```
composer require smnandre/pandoc
```

### Pandoc Binary

[](#pandoc-binary)

Install Pandoc first using your system package manager.

```
# Ubuntu/Debian
sudo apt-get install pandoc

# macOS
brew install pandoc
```

---

Usage
-----

[](#usage)

### Input

[](#input)

Specify source content using string input, file paths, or explicit format overrides when automatic detection fails.

```
// String content with automatic format detection
$converter = Pandoc::content('# Hello World');

// File input with extension-based format detection
$converter = Pandoc::file('document.md');

// Override automatic format detection when needed
$converter = Pandoc::content($data)->from('markdown');

// Check format compatibility
if (Pandoc::supports('markdown', 'pdf')) {
    // Proceed with conversion
}
```

### Output

[](#output)

Control conversion targets through format specification, file destinations, or directory outputs.

```
// Get content as string
$htmlContent = Pandoc::file('doc.md')->to('html')->convert()->getContent();

// Save to specific file path
Pandoc::file('doc.md')->to('pdf')->output('reports/final-report.pdf')->convert();

// Save to directory (filename inferred from input)
Pandoc::file('doc.md')->to('pdf')->output('reports/')->convert();

// Save to file (format inferred from extension)
Pandoc::file('doc.md')->output('documentation.html')->convert();
```

### Options

[](#options)

Configure Pandoc behavior using individual options, batch option arrays, template variables, or document metadata.

```
// Individual option configuration
$converter->option('toc', true)->option('standalone', true);

// Batch option setting
$converter->options(['toc' => true, 'number-sections' => true]);

// Template variables and metadata
$converter->variable('title', 'Report')->variable('grade', 'A+');

// Document metadata for publishing workflows
$converter->metadata('author', 'The Team')->metadata('date', '2025-01-01');
```

### Configuration

[](#configuration)

Framework integration and custom binary paths for non-standard installations.

```
// Custom binary path configuration
$binary = PandocBinary::fromPath('/opt/pandoc/bin/pandoc');
Pandoc::setDefaultBinary($binary);

// Check supported formats
Pandoc::getInputFormats();  // Get all supported input formats
Pandoc::getOutputFormats(); // Get all supported output formats
Pandoc::supports('markdown', 'html'); // Check if conversion is supported
```

### Test

[](#test)

Unit testing with `MockConverter` records method calls without requiring Pandoc installation. Integration testing verifies actual Pandoc functionality.

```
// Unit testing with MockConverter
class DocumentServiceTest extends TestCase
{
    public function testGenerateReport(): void
    {
        $mock = new MockConverter();
        $service = new DocumentService($mock);
        $service->createReport('# Test');

        // Verify calls without external dependencies
        $calls = $mock->getCalls();

        $this->assertCount(1, $calls);
        $this->assertEquals('# Test', $calls[0]['inputContent']);
        $this->assertEquals('html', $calls[0]['outputFormat']);
        $this->assertTrue($calls[0]['options']['standalone']);
    }
}

// Integration testing with real Pandoc
class PandocIntegrationTest extends TestCase
{
    public function testConversion(): void
    {
        if (!Pandoc::isInstalled()) {
            $this->markTestSkipped('Pandoc not installed');
        }

        $html = Pandoc::content('# Test')->to('html')->convert()->getContent();
        $this->assertStringContainsString('Test', $html);
    }
}
```

---

Examples
--------

[](#examples)

Convert **multiple Markdown files** to **HTML**

```
$posts = glob('content/*.md');
foreach ($posts as $post) {
    Pandoc::convertFile($post, 'public/'.basename($post, '.md').'.html');
}
```

Generate **academic paper** with **citations and formatting**

```
$result = Pandoc::file('manuscript.md')
    ->to('pdf')
    ->option('toc', true)
    ->option('bibliography', 'references.bib')
    ->variable('title', 'Research Paper')
    ->variable('author', 'The Team')
    ->convert();
```

Handle **document conversion** in web applications with **error handling**

```
class DocumentController {
    public function convert(Request $request): Response {
        $result = $this->converter->content($request->getContent())->to('html')->convert();

        if (!$result->isSuccess()) {
            throw new BadRequestHttpException($result->getError());
        }

        return new Response($result->getContent());
    }
}
```

---

Troubleshooting
---------------

[](#troubleshooting)

### Pandoc not found

[](#pandoc-not-found)

This error occurs when the library cannot find the pandoc executable in your system PATH.

```
// Solution: Specify custom binary path
use Pandoc\PandocBinary;

$binary = PandocBinary::fromPath('/opt/pandoc/bin/pandoc');
Pandoc::setDefaultBinary($binary);
```

### Format detection

[](#format-detection)

Automatic format detection fails when file extensions are missing or content format is ambiguous.

```
// Solution: Explicitly specify input and output formats
$result = Pandoc::content($unknownContent)
    ->from('markdown')  // Override input format detection
    ->to('html')        // Ensure output format is clear
    ->convert();
```

### Conversion errors

[](#conversion-errors)

Pandoc conversion failures provide detailed error messages through the result object.

```
$result = Pandoc::file('input.md')->to('pdf')->convert();

if (!$result->isSuccess()) {
    // Access detailed error information from Pandoc
    $errorMessage = $result->getError();
    echo "Conversion failed: " . $errorMessage;
}
```

---

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

[](#contributing)

Contributions are welcome! Please start by creating an issue to discuss your changes.

---

Credits
-------

[](#credits)

Built on [Pandoc](https://pandoc.org/) by John MacFarlane.

Created and maintained by [Simon André](https://github.com/smnandre).

Tip

This library is developed and maintained by a single developer in their free time.

To ensure continued maintenance and improvements, consider [sponsoring development](https://github.com/sponsors/smnandre).

---

License
-------

[](#license)

MIT License - see [LICENSE](LICENSE) file for details.

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance54

Moderate activity, may be stable

Popularity28

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 95.2% 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 ~200 days

Total

2

Last Release

316d ago

Major Versions

v1.0.0 → v2.0.02025-08-21

### Community

Maintainers

![](https://www.gravatar.com/avatar/a8951d49b371d210280a58ce48969a07d1f49924810f8a1fab3a9343eb46fdc9?d=identicon)[simonandre](/maintainers/simonandre)

---

Top Contributors

[![smnandre](https://avatars.githubusercontent.com/u/1359581?v=4)](https://github.com/smnandre "smnandre (20 commits)")[![Copilot](https://avatars.githubusercontent.com/in/1143301?v=4)](https://github.com/Copilot "Copilot (1 commits)")

---

Tags

converterconvertiondocumentdocxhtmllatexmarkdownpandocpandoc-phppdfphp-cliphp-libraryrstsymfony-consoleclipdfconverthtmlconvertermarkdownmdphp-clirstpandoc

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/smnandre-pandoc/health.svg)

```
[![Health](https://phpackages.com/badges/smnandre-pandoc/health.svg)](https://phpackages.com/packages/smnandre-pandoc)
```

###  Alternatives

[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.7k38.9k](/packages/matomo-matomo)[laravel/framework

The Laravel Framework.

34.8k543.8M20.1k](/packages/laravel-framework)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[drupal/core

Drupal is an open source content management platform powering millions of websites and applications.

21866.0M1.7k](/packages/drupal-core)[pontedilana/php-weasyprint

PHP library allowing PDF generation from an url or a html page. Wrapper for Kozea/WeasyPrint.

781.2M17](/packages/pontedilana-php-weasyprint)[drupal/core-recommended

Locked core dependencies; require this project INSTEAD OF drupal/core.

6942.5M421](/packages/drupal-core-recommended)

PHPackages © 2026

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