Minimal PDF creation library for PHP β ~700 lines of code, zero dependencies, makes real PDFs.
Port of littlepdf from TypeScript to PHP.
Ultra-lightweight by design. We stripped away everything non-essential: TTF fonts, PNG/SVG support, HTML-to-PDF conversion, forms, encryption, and compression. What remains is the core 95% use case: putting text and images on a page.
Perfect for building: invoices, receipts, reports, shipping labels, tickets, certificates, contracts, and data exports.
- β Text β Helvetica font, any size, hex colors, left/center/right alignment
- β Shapes β Rectangles and lines
- β Images β JPEG support (photos, logos, signatures)
- β Pages β Multiple pages with custom sizes
- β Markdown β Convert markdown to PDF with headers, lists, and rules
Custom fonts, PNG/GIF/SVG, vector graphics, forms, encryption, compression, HTML-to-PDF
composer require usaikoo/littlepdf-php<?php
require_once __DIR__ . '/vendor/autoload.php';
use LittlePdf\LittlePdf;
use LittlePdf\TextOptions;
use LittlePdf\TextAlign;
$doc = LittlePdf::create();
$doc->page(function($ctx) {
$ctx->rect(50, 700, 200, 40, '#2563eb'); // blue rectangle
$ctx->text('Hello PDF!', 60, 712, 24, new TextOptions(color: '#ffffff'));
$ctx->line(50, 680, 250, 680, '#000000', 1); // black line
});
file_put_contents('output.pdf', $doc->build());$doc = LittlePdf::create();
$doc->page(function($ctx) {
$logo = file_get_contents('logo.jpg');
$ctx->image($logo, 50, 700, 100, 50);
});
file_put_contents('output.pdf', $doc->build());use LittlePdf\LittlePdf;
LittlePdf::measureText('Hello', 12); // => 27.34 (points)use LittlePdf\MarkdownConverter;
$markdown = <<<MD
# Hello World
A minimal PDF from markdown.
## Features
- Headers (h1, h2, h3)
- Bullet lists
- Numbered lists
- Horizontal rules
---
Automatic word wrapping and pagination included.
MD;
$converter = new MarkdownConverter();
$pdf = $converter->convert($markdown);
file_put_contents('output.pdf', $pdf);Main factory class for creating PDF documents.
static function create(): PDFBuilderInterfaceCreate a new PDF builder
static function measureText(string $str, float $size): floatMeasure text width in points
function page(float|callable $widthOrFn, ?float $height = null, ?callable $fn = null): voidAdd a page to the document
function build(): stringBuild and return the final PDF content
function measureText(string $str, float $size): floatMeasure text width in points
Context provided to page callbacks for drawing operations.
function text(string $str, float $x, float $y, float $size, ?TextOptions $opts = null): voidRender text at specified position
function rect(float $x, float $y, float $w, float $h, string $fill): voidDraw a filled rectangle
function line(float $x1, float $y1, float $x2, float $y2, string $stroke, float $lineWidth = 1.0): voidDraw a line
function image(string $jpegBytes, float $x, float $y, float $w, float $h): voidRender a JPEG image
Value object for text rendering options.
new TextOptions(
TextAlign $align = TextAlign::LEFT,
?float $width = null,
string $color = '#000000'
)public TextAlign $alignText alignment (LEFT, CENTER, RIGHT)
public ?float $widthWidth constraint for alignment
public string $colorText color in hex format (e.g., '#ff0000')
Convert markdown documents to PDF.
new MarkdownConverter(
float $width = 612,
float $height = 792,
float $margin = 72
)function convert(string $markdown): stringConvert markdown to PDF content
- Headers (H1-H3):
#,##,### - Unordered lists:
-or* - Ordered lists:
1.,2., etc. - Horizontal rules:
---,***,___ - Paragraphs with automatic word wrapping
- Blank lines for spacing
PDF uses a coordinate system where:
- Origin (0, 0) is at the bottom-left corner
- X increases to the right
- Y increases upward
- Units are in points (1/72 inch)
Common page sizes:
- US Letter: 612 x 792 points
- A4: 595 x 842 points
- PHP 8.1 or higher
- Composer
composer installphp examples/example.phpThis generates example.pdf and markdown-example.pdf in the examples/ directory.
composer test:typesMIT
This is a PHP port of littlepdf by Sai Ko.

