quickshiftin / php-pdf-invoice
使用 PHP 为您的应用程序发票生成 PDF 文件
1.10.0
2018-05-20 02:59 UTC
Requires
- bombayworks/zendframework1: 1.*
- phpunit/phpunit: 4.8.35
This package is auto-updated.
Last update: 2024-09-20 08:23:08 UTC
README
本项目使用了一个从 Magento 中提取的 PDF 发票生成器,通过 Composer 实现了一般用途。您可以将现有的领域模型轻松集成,并开始生成如下所示的 PDF 发票
特性
- 生成发票 PDF 文档
- 通过实现订单和订单项接口轻松集成您的领域模型
- Composer 分发
- 更改背景和字体颜色、字体类型、行颜色并提供自定义标志
- 根据行项目数量自动创建多页
通过 Composer 安装
./composer.phar require quickshiftin/php-pdf-invoice
用法
集成样板 - 将现有领域模型连接到生成器
要集成您现有应用程序的订单,只需提供两个类,这两个类 实现 Quickshiftin\Pdf\Invoice\Spec\Order 和 Quickshiftin\Pdf\Invoice\Spec\OrderItem
订单项接口实现
namespace MyApp; use Quickshiftin\Pdf\Invoice\Spec\OrderItem; // Implement the Order Item methods below class MyOrderItem interface implements OrderItem { /** * The name or description of the product * @return string */ public function getName(); /** * The 'SKU' or unique identifier for your product * @return string */ public function getSku(); /** * The quantity sold * @return int */ public function getQuantity(); /** * The price per unit * @return float */ public function getPricePerUnit(); /** * The price including tax * @return flaot */ public function getPrice(); /** * The sales tax amount in dollars * @return float */ public function getSalesTaxAmount(); }
订单接口实现
use Quickshiftin\Pdf\Invoice\Spec\Order; // Implement the order methods below class MyOrder implements Order { /** * Get the sub-total, eclusive of shipping and tax. * @return float */ public function getPriceBeforeShippingNoTax(); /** * Get the shipping charge if any * @return float */ public function getCustomerShipCharge(); /** * Get the sales tax amount, eg .08 for 8% * @return float */ public function getSalesTaxAmount(); /** * Get the total cost including shipping and tax * @return float */ public function getTotalCost(); /** * Get the full billing address for the customer * @return string */ public function getFullBillingAddress(); /** * Get the payment method, EG COD, Visa, PayPal etc * @return string */ public function getPaymentMethod(); /** * Get the full shipping address for the order * @return string */ public function getFullShippingAddress(); /** * Get the name of the shipping method, EG UPS, FedEx, etc * @return string */ public function getShippingMethodName(); /** * Get an array of OrderItem objects * @note This should return an array of instances of a class where you implement Quickshiftin\Pdf\Invoice\Spec\OrderItem * @return array */ public function getOrderItems(); /** * Get the id of the order * @return int|string */ public function getOrderId(); /** * Get the date of the sale * @return DateTime */ public function getSaleDate(); }
实现建议
由于这些都是 接口,您可以创建一个新的类来包装您现有的 OrderItem 对象。如果没有名称冲突,您也可以考虑直接在现有的 OrderItem 类中实现。
构建和样式化您的发票 PDF
此系统在底层使用 Zend_Pdf(来自 ZF1)。该包提供了 Quickshiftin\Pdf\Invoice\Factory,它是用于实例化来自 Zend_Pdf 的类的包装器。您将使用这些对象来自定义 PDF 的外观。
我们还假设您有一个实现 Quickshiftin\Pdf\Invoice\Spec\Order 的订单对象实例,如上所述,存储在名为 $myOrder 的变量中。
use Quickshiftin\Pdf\Invoice\Invoice as PdfInvoice; use Quickshiftin\Pdf\Invoice\Factory as InvoiceFactory; $oInvoiceFactory = new InvoiceFactory(); $oInvoicePdf = new PdfInvoice(); // Configure fonts - just put ttf font files somewhere your project can access them $oInvoicePdf->setRegularFontPath(__DIR__ . '/../assets/Arial.ttf'); $oInvoicePdf->setBoldFontPath(__DIR__ . '/../assets/Arial Bold.ttf'); $oInvoicePdf->setItalicFontPath(__DIR__ . '/../assets/Arial Italic.ttf'); // Set Colors $red = '#d53f27'; $yellow = '#e8e653'; // Title section of invoice // Background color for title section of invoice, the default is white $oInvoicePdf->setTitleBgFillColor($oInvoiceFactory->createColorHtml($yellow)); $oInvoicePdf->setTitleFontColor($oInvoiceFactory->createColorHtml('black')); // Header sections of invoice $oInvoicePdf->setHeaderBgFillColor($oInvoiceFactory->createColorHtml($red)); $oInvoicePdf->setBodyHeaderFontColor($oInvoiceFactory->createColorHtml('white')); // Body section of invoice $oInvoicePdf->setBodyFontColor($oInvoiceFactory->createColorHtml('black')); // Line color of invoice $oInvoicePdf->setLineColor($oInvoiceFactory->createColorGrayscale(0)); // Configure logo $oInvoicePdf->setLogoPath(__DIR__ . '/../assets/fake-logo.jpg'); // Build the PDF // $oPdf is an instance of Zend_Pdf $oPdf = $oInvoicePdf->getPdf($myOrder); // A string rendition, you could echo this to the browser with headers to implement a download $pdf = $oPdf->render(); // You can also simply save it to a file file_put_contents('/tmp/test.pdf', $pdf);
注意
您可以在测试目录中查看使用见解。欢迎问题和 PR!