承接 usaikoo/littlepdf-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

usaikoo/littlepdf-php

Composer 安装命令:

composer require usaikoo/littlepdf-php

包简介

Minimal PDF creation library for PHP. ~700 lines of code, zero dependencies, makes real PDFs.

README 文档

README

Latest Version Total Downloads License

Minimal PDF creation library for PHP — ~700 lines of code, zero dependencies, makes real PDFs.

Port of littlepdf from TypeScript to PHP.

🎯 Philosophy

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.

📸 Preview

Invoice Example

Invoice Example

Shapes & Images Example

Shapes & Images Example

✨ What's Included

  • 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

❌ What's Not Included

Custom fonts, PNG/GIF/SVG, vector graphics, forms, encryption, compression, HTML-to-PDF

💡 Need those features? Check out FPDF or TCPDF.

📦 Installation

composer require usaikoo/littlepdf-php

🚀 Getting Started

Basic Example

<?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());

Working with Images

$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());

Text Measurement

use LittlePdf\LittlePdf;

LittlePdf::measureText('Hello', 12); // => 27.34 (points)

Markdown Conversion

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);

📚 API Reference

LittlePdf

Main factory class for creating PDF documents.

Methods

static function create(): PDFBuilderInterface

Create a new PDF builder

static function measureText(string $str, float $size): float

Measure text width in points

PDFBuilderInterface

Methods

function page(float|callable $widthOrFn, ?float $height = null, ?callable $fn = null): void

Add a page to the document

function build(): string

Build and return the final PDF content

function measureText(string $str, float $size): float

Measure text width in points

PageContextInterface

Context provided to page callbacks for drawing operations.

Methods

function text(string $str, float $x, float $y, float $size, ?TextOptions $opts = null): void

Render text at specified position

function rect(float $x, float $y, float $w, float $h, string $fill): void

Draw a filled rectangle

function line(float $x1, float $y1, float $x2, float $y2, string $stroke, float $lineWidth = 1.0): void

Draw a line

function image(string $jpegBytes, float $x, float $y, float $w, float $h): void

Render a JPEG image

TextOptions

Value object for text rendering options.

Constructor

new TextOptions(
    TextAlign $align = TextAlign::LEFT,
    ?float $width = null,
    string $color = '#000000'
)

Properties

public TextAlign $align

Text alignment (LEFT, CENTER, RIGHT)

public ?float $width

Width constraint for alignment

public string $color

Text color in hex format (e.g., '#ff0000')

MarkdownConverter

Convert markdown documents to PDF.

Constructor

new MarkdownConverter(
    float $width = 612,
    float $height = 792,
    float $margin = 72
)

Methods

function convert(string $markdown): string

Convert markdown to PDF content

Supported Markdown Features

  • Headers (H1-H3): #, ##, ###
  • Unordered lists: - or *
  • Ordered lists: 1., 2., etc.
  • Horizontal rules: ---, ***, ___
  • Paragraphs with automatic word wrapping
  • Blank lines for spacing

Coordinate System

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

🛠️ Development

Requirements

  • PHP 8.1 or higher
  • Composer

Installation

composer install

Run Example

php examples/example.php

This generates example.pdf and markdown-example.pdf in the examples/ directory.

Static Analysis

composer test:types

📄 License

MIT

Credits

This is a PHP port of littlepdf by Sai Ko.

littlepdf-php

usaikoo/littlepdf-php 适用场景与选型建议

usaikoo/littlepdf-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 12 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「markdown」 「pdf」 「document」 「minimal」 「pdf-generation」 「zero-dependencies」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 usaikoo/littlepdf-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 usaikoo/littlepdf-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 usaikoo/littlepdf-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 13
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-23