8ctopus/pigeon-invoice

创建 PDF 和 HTML 发票

5.0.1 2024-05-29 06:10 UTC

README

packagist downloads min php version license tests code coverage badge lines of code

创建 PDF 和 HTML 发票

功能

  • 创建 PDF 和 HTML 发票
  • 由于使用 Twig 模板,可完全自定义
  • 可本地化(演示显示阿拉伯语、法语、德语、希腊语、日语、韩语、俄语和繁体中文)
  • 可扩展以包含自定义数据(见 extend 目录)
  • 可选的 PDF 引擎:Dompdf 使用 CPDFPDFLib 以及 wk<html>topdf
  • 调整纸张大小

invoice demo screenshot

要求

  • php > 8.0 且已安装 ext-domext-mbstring

演示

  • 克隆项目
composer install
php demo.php
  • 检查生成的发票 invoice.pdfinvoice.html

安装

  • 安装包
composer require 8ctopus/pigeon-invoice
  • resources 目录复制到您的项目中
use Oct8pus\Invoice\Company;
use Oct8pus\Invoice\Discount;
use Oct8pus\Invoice\Invoice;
use Oct8pus\Invoice\Item;
use Oct8pus\Invoice\Person;
use Oct8pus\Invoice\Shipping;
use Oct8pus\Invoice\Tax;

require_once __DIR__ . '/vendor/autoload.php';

$invoice = (new Invoice([
    'rootDir' => __DIR__ . DIRECTORY_SEPARATOR . 'resources',
    'templatesDir' => 'templates',
    'locale' => 'en'
]))
    ->setSeller((new Company())
        ->setName('Widgets LLC')
        ->setWebsite('https://www.widgets.ru')
        ->setEmail('hello@widgets.ru')
        ->setStreet1('16 Leo Tolstoy Street')
        ->setZip('119021')
        ->setCity('Moscow')
        ->setCountry('Russia'))

    ->setBuyer((new Person())
        ->setFirstName('Yuri')
        ->setLastName('Kamasov')
        ->setStreet1('Krasnoarmeyskaya 1')
        ->setZip('620026')
        ->setCity('Yekaterinburg')
        ->setCountry('Russia'))

    ->setDate(new DateTime('28-04-2022'))
    ->setNumber('EN43UD6JA7I2LNBC17')
    ->setCurrency('EUR')

    // add items
    ->addItem((new Item())->setName('Item 1')->setPrice(4.99)->setQuantity(1))
    ->addItem((new Item())->setName('Item 2')->setPrice(9.99)->setQuantity(2))
    ->addItem((new Item())->setName('Item 3')->setPrice(3.99)->setQuantity(3))

    ->setDiscount((new Discount())->setName('Special Offer')->setPrice(10.00))

    ->setShipping((new Shipping())->setName('Shipping')->setPrice(5.00))

    ->setTax((new Tax())->setName('VAT')->setPercentage(0.21))

    ->setCustomFields([
        'notes' => 'Thank you for shopping with us!',
    ]);

$html = $invoice->renderHtml();

file_put_contents('invoice.html', $html);

$pdf = $invoice->renderPdf([
    'paper' => 'A4',
    'orientation' => 'portrait',
    // allow to download content from the internet such as fonts
    'isRemoteEnabled' => true,
    // valid options: CPDF, PDFLib, GD, wkhtmltopdf and auto
    'pdfBackend' => 'CPDF',
]);

file_put_contents('invoice.pdf', $pdf);

PDF 引擎

  • Dompdf 包含三个引擎

    • CPDF(与 Dompdf 一起捆绑)
    • PDFLib 是一个具有比 CPDF 更先进渲染的商业库。它以 PHP 扩展的形式提供,您需要将其添加到 PHP 配置中。
    • GD 将生成包含图像的 PDF
  • wk<html>topdf

要使用 wk<html>topdf 引擎,您需要为您的系统 下载二进制文件 并将其添加到当前工作目录 getcwd() 或系统路径。

Twig 模板参考文档

https://twig.symfony.ac.cn/doc/3.x/

自定义字体

字体可以是 HTML head 中的样式表链接提供,这需要 isRemoteEnabled 权限

<link href="https://fonts.googleapis.com/css2?family=Noto+Sans:wght@300;700&display=swap" rel="stylesheet">

或从本地驱动器使用 css 提供,但仅限于 TrueType *.ttf 格式

@font-face {
    font-family: 'Tangerine';
    src: url('../fonts/tangerine-400.ttf');
    font-weight: normal;
}

@font-face {
    font-family: 'Tangerine';
    src: url('../fonts/tangerine-700.ttf');
    font-weight: bold;
}

我建议您在生产环境中使用第二种变体。

更多语言支持

字体支持有限数量的语言,因此如果您想支持许多不同的语言,您需要添加具有语言字符符号的字体。以下是一个示例,其中默认字体为 Segoe UI(拉丁语语言),然后回退到 Meiyro UI 用于日语,以及 Malgun Gothic 用于韩语

font-family: 'Segoe UI', 'Meiryo UI', 'Malgun Gothic', sans-serif;

更多信息:https://github.com/dompdf/dompdf/wiki/UnicodeHowTo

致谢

测试

composer test

注意:测试不检查 PDF 输出,似乎它尚未在 DomPDF 中准备好

代码整洁

composer fix