mzur/invoiscript

生成简单的PDF发票

v1.1 2021-08-30 19:25 UTC

This package is auto-updated.

Last update: 2024-09-06 17:29:11 UTC


README

使用PHP生成简单的PDF发票。

安装

运行

composer require mzur/invoiscript

用法

示例

use Mzur\InvoiScript\Invoice;

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

$content = [
   'title' => 'Invoice No. 1',
   'beforeInfo' => [
      '<b>Date:</b>',
      'June 10, 2021',
   ],
   'afterInfo' => [
      'All prices in EUR.',
      '',
      'This invoice is due on <b>June 20, 2021</b>.',
   ],
   'clientAddress' => [
      'Jane Doe',
      'Example Street 42',
      '1337 Demo City',
   ],
   'entries' => [
      [
         'description' => 'Hot air',
         'quantity' => 11,
         'price' => 8,
      ],
      [
         'description' => 'Something cool',
         'quantity' => 5,
         'price' => 20,
      ],
   ],
];

$pdf = new Invoice($content);
$pdf->generate('invoice.pdf');

这将生成以下PDF

样式

titlebeforeInfoafterInfo中的内容可以使用类似HTML的基本标签进行样式设置。例如

$content = [
   'title' => 'Invoice No. <b>1</b>',
   'beforeInfo' => [
      '<i>Date:</i>',
      '<u>June 10, 2021</u>',
   ],
   //...
];

可用标签

  • <b></b>:粗体
  • <i></i>:斜体
  • <u></u>:下划线

请参阅布局部分以自定义字体和字体大小。

模板

设置模板文件

$pdf = new Invoice($content);
$pdf->setTemplate(__DIR__.'/template.pdf');

模板可以有多个页面,这些页面将用于匹配发票的页面。如果发票的页面数多于模板,则模板的最后一页将被重复使用。

语言

设置语言

$pdf = new Invoice($content);
$pdf->setLanguage('de');

可用语言为ende。默认为en

变量

可以在titlebeforeInfoafterInfo中使用变量。例如

$content = [
   'title' => 'Invoice No. {number}',
   'beforeInfo' => [
      '<b>Date:</b>',
      '{createdDate}',
   ],
   //...
];

$variables = [
   'number' => 1,
   'createdDate' => 'June 10, 2021',
];

$pdf = new Invoice($content);
$pdf->setVariables($variables);

以下变量始终可用

  • {total}:发票的总金额。
  • {page}:当前页码。
  • {pages}:总页数。

布局

默认间距、字体、字体大小等可以通过自定义布局进行覆盖。例如

$layout = [
   'font' => 'helvetica',
];

$pdf = new Invoice($content);
$pdf->setLayout($layout);

请参阅源代码以获取所有可用布局选项和默认值。

致谢

感谢FPDFFPDI的创造者!