iwasherefirst2 / pretty-pdf
用PHP创建精美的发票
v2.1.0
2022-05-01 17:30 UTC
Requires
- php: >=7.4
- setasign/tfpdf: ^1.32
Requires (Dev)
- phpunit/phpunit: 8.5.x-dev
README
此包可以帮助您使用PHP创建带有UTF8支持的发票和信件。
此包最好的地方是发票看起来绝对精美,并且此外,您的信件/发票的地址完全适合您的信封窗口!
默认格式为A4。如果您需要其他格式,只需使用FPDF标准方法。
所需PHP版本
安装
通过composer安装
composer require iwasherefirst2/pretty-pdf
示例
$item = new \PrettyPdf\Partials\Invoice\Data\Item(); $item->description = 'A new currency'; $item->quantity = 5; $item->name = 'Bitcoin'; $item->unitPrice = 2031.23; $paymentInfoDate = new \PrettyPdf\Partials\Invoice\Data\PaymentInfo(); $paymentInfoDate->title = 'A really good title'; $paymentInfoDate->description = 'A long description comes in here'; $paymentInfoDate->bank = 'ING'; $paymentInfoDate->bic = 'BICXXX'; $paymentInfoDate->iban = 'DE42 4242 4242 4242 4242 24'; $paymentInfoDate->name = 'Beauty Bill Creator'; $bill = new \PrettyPdf\PrettyPdf(); $bill->logo('/path/to/your/logo.png') ->headerInfoBox(['1600 Pennsylvania Ave NW', 'Washington', 'DC 20500', 'United States', 'Beauty Bill Package', 'info@drnielsen.de']) ->returnAddress('Dr. Schwadam, Schwinterfeldschraße 99, 10777 Berlin, Germany') ->receiverAddress(['Michael Jackson', 'Colorado Hippo Zoo', '5225 Figueroa Mountain Rd', 'Los Olivos', 'CA 93441', 'United States']) ->invoiceBox(['Date' => 'Today', 'Invoice' => 'I 2020-03-22', 'Tax-Number' => '18/455/12345']) ->items([$item], 19) ->paymentInfo($paymentInfoDate) ->additionalNote('Optioanl note. Nothing important here.') ->output('/path/where/you/want/to/store/file.pdf');
方法
最重要的函数是输出函数,它渲染PDF,无论是将其保存为文件还是通过浏览器查看PDF,或者将其保存为字符串。
您可以从FPDF调用所有其他函数。
此外,还提供以下函数
我如何添加自己的样式?
简单创建一个继承自PrettyPdf\Partials\Drawable
的类。
<?php namespace MyApp\Styles; class MyOwnStyle extends \PrettyPdf\Partials\Drawable { public function draw(): void { // Here you can use all functions from http://www.fpdf.org/ // to draw on your pdf. $this->MultiCell(....); $this->Cell(...); $this->Rect(..); } }
接下来,您需要将您的类或类注册到PrettyPdf
中
$prettyPdf = new \PrettyPdf\PrettyPdf(); $prettyPdf->addCustomPartials(['\MyApp\Styles\MyOwnStyle']);
现在,您可以通过在$prettyPdf
上调用类名来使用该方法
$prettyPdf->myOwnStyle() ->output('/path/to/file.pdf');
测试是如何工作的?
测试期间将创建一个PDF并与现有的PDF进行比较。由于比较PDF文档是不可行的,因为文件本身包含始终不同的元数据,我们的测试将PDF转换为图像并比较图像。
因此,我们要求测试ImageMagic和GD库。为了使您的生活更轻松,此包附带一个Docker文件。
您可以这样使用它
docker-compose up -d --build
接下来,进入容器安装composer并运行phpunit
docker-compose exec app bash
composer install
php vendor/bin/phpunit
创建一个继承自BeautyBillTestCase的测试类。
这里有一个例子
<?php namespace Tests; use BeautyBill\Partials\Body\Item; class BeautyBillBodyTest extends BeautyBillTestCase { public function test_body_basic() { $this->storeOnly = true; $item = new Item(); $item->description = 'A new currency'; $item->quantity = 5; $item->title = 'Bitcoin'; $item->unitPrice = 2031.23; $this->bill->items([$item]); $this->assertEqualPDFs('Test.pdf'); } }
当storeOnly
设置为true时,它不会比较PDF,而只是将PDF存储在Test.pdf
或您在assertEqualPDFs
方法中指定的任何名称中。一旦PDF看起来像您想要的样子,您可以删除storeOnly
行。