konekt/pdf-invoice

用于生成PDF发票的库

1.14.0 2023-11-28 16:07 UTC

README

Tests StyleCI Packagist Stable Version Packagist downloads GPL Software License

这是pdf-invoicr的简化分支:pdf-invoicr.

变更

  • PHP 7.3 - 8.2 支持
  • PSR-4 兼容
  • 作为composer包提供
  • 依赖项通过composer获取

PHP 兼容性

简介

PHP发票是一个简单的面向对象的PHP类,只需几行代码即可生成美观的发票、报价单或订单。使用您自己的标志和主题颜色进行品牌化,添加无限数量的项目并自动分页。您可以将PDF输出发送到用户的浏览器,保存在服务器上或强制下载文件。PHP发票完全可定制,可以集成到任何知名的内容管理系统。

多语言和货币

PHP发票内置了英语、荷兰语、法语、德语、西班牙语和意大利语(如果需要,您可以轻松添加自己的语言)的翻译,并且您可以设置每个文档所需的货币。

额外的标题、段落和徽章

可以将额外的内容(标题和多行段落)添加到文档的底部。您可能用它来添加支付或运输信息或其他所需内容。

安装

composer require konekt/pdf-invoice

示例

该仓库的examples/文件夹中包含3个示例

  • simple.php
  • example1.php
  • example2.php
  • change_timezone.php

创建新的发票

TODO:在代码审查后,更新README文档中的新const。

确保在使用类之前设置默认的php日期时区。

在这个简单的示例中,我们生成一个带有自定义标志和主题颜色的发票。它将包含2个产品,底部有一个包含增值税和总价的面板。然后我们在输出前添加一个“已支付”徽章。

use Konekt\PdfInvoice\InvoicePrinter;

  $invoice = new InvoicePrinter();
  
  /* Header settings */
  $invoice->setLogo("images/sample1.jpg");   //logo image path
  $invoice->setColor("#007fff");      // pdf color scheme
  $invoice->setType("Sale Invoice");    // Invoice Type
  $invoice->setReference("INV-55033645");   // Reference
  $invoice->setDate(date('M dS ,Y',time()));   //Billing Date
  $invoice->setTime(date('h:i:s A',time()));   //Billing Time
  $invoice->setDue(date('M dS ,Y',strtotime('+3 months')));    // Due Date
  $invoice->setFrom(array("Seller Name","Sample Company Name","128 AA Juanita Ave","Glendora , CA 91740"));
  $invoice->setTo(array("Purchaser Name","Sample Company Name","128 AA Juanita Ave","Glendora , CA 91740"));
  
  $invoice->addItem("AMD Athlon X2DC-7450","2.4GHz/1GB/160GB/SMP-DVD/VB",6,0,580,0,3480);
  $invoice->addItem("PDC-E5300","2.6GHz/1GB/320GB/SMP-DVD/FDD/VB",4,0,645,0,2580);
  $invoice->addItem('LG 18.5" WLCD',"",10,0,230,0,2300);
  $invoice->addItem("HP LaserJet 5200","",1,0,1100,0,1100);
  
  $invoice->addTotal("Total",9460);
  $invoice->addTotal("VAT 21%",1986.6);
  $invoice->addTotal("Total due",11446.6,true);
  
  $invoice->addBadge("Payment Paid");
  
  $invoice->addTitle("Important Notice");
  
  $invoice->addParagraph("No item will be replaced or refunded if you don't have the invoice with you.");
  
  $invoice->setFooternote("My Company Name Here");
  
  $invoice->render('example1.pdf','I'); 
  /* I => Display on browser, D => Force Download, F => local path save, S => return document as string */

文档

创建实例

use Konekt\PdfInvoice\InvoicePrinter;

// Default Param: Size: A4, Currency: $, Language: en
$invoice = new InvoicePrinter($size, $currency, $language); 

数字格式化

您希望如何显示您的数字?

$invoice->setNumberFormat($decimalpoint, $seperator, $alignment, $space, $negativeParenthesis);

颜色

设置一个自定义颜色来个性化您的发票。

// Hexadecimal color code
$invoice->setColor($color);

添加标志

$invoice->setLogo($image, $maxwidth, $maxheight);

文档标题

// A string with the document title, will be displayed
// in the right top corner of the document (e.g. 'Invoice' or 'Quote')
$invoice->setType($title);

发票编号

// Document reference number that will be displayed in
// the right top corner of the document (e.g. 'INV29782')
$invoice->setReference($number);

日期

//A string with the document's date
$invoice->setDate($date);

到期日期

// A string with the document's due date
$invoice->setDue($duedate);

发行者信息

设置您的公司详细信息。

// An array with your company details. The first value of
// the array will be bold on the document so it's suggested
// to use your company's name. You can add as
// many lines as you need.
/** Example: */
$invoice->setFrom([
    'My Company',
    'Address line 1',
    'Address line 2',
    'City and zip',
    'Country',
    'VAT number'    
]);

客户信息

// An array with your clients' details. The first value of the
// array will be bold on the document so we suggest you to use
// the company's name. You can add as many lines as you need.
/** Example */
$invoice->setTo([
   'My Client',
   'Address line 1',
   'Address line 2',
   'City and zip',
   'Country',
   'VAT number'    
]);

翻转

切换公司和客户信息的水平位置。默认情况下,您的公司详细信息位于左侧。

$invoice->flipflop();

发行者和客户标题

隐藏发行者和客户标题行

$invoice->hideToFromHeaders();

添加项目

在公司和客户信息下方添加新的产品或服务行到您的文档中。PHP发票具有自动分页功能,因此绝对没有限制。

$invoice->addItem(name, description, quantity, vat, price, discount, total);
  • name (字符串)
    产品或服务名称的字符串。
  • description (字符串|false)
    带有多行支持的描述字符串。使用<br>\n添加换行符。
  • quantity (decimal|false)
    该行的数量整数。
  • vat (string|decimal|false)
    传递一个字符串(例如“21%”,或您可能喜欢的任何其他文本)或一个十进制数,如果您想显示一个金额(例如124.30)。数值将自动格式化为“货币”。
  • price (decimal|false)
    单位价格的小数。
  • discount (string|decimal|false)
    传递一个字符串(例如“10%”,或您可能喜欢的任何其他文本)或一个十进制数,如果您想显示一个金额(例如50.00)注意:如果任何产品未设置折扣,最终输出将不会显示折扣列。
  • total (十进制|false)
    表示产品或服务总价的十进制数。

字段 descriptionquantityvatpricediscounttotal 都是可选的。要禁用发票行中的任何一项,请将相应的参数传递为 false

项目行描述字体大小

更改产品描述的字体大小。默认为 7。

$invoice->setFontSizeProductDescription(9);

添加总计

在产品和服务下方添加一行,用于计算和总计。您可以添加无限行。

$invoice->addTotal(name,value,background);
  • name {string} 用于显示总计字段显示名称的字符串。
  • value {decimal} 用于值的十进制数。
  • background {boolean} 可选。设置为 true 以将主题颜色设置为行的背景颜色。

添加徽章

在产品和服务下方添加徽章到您的发票。例如,您可以使用此功能显示发票已被支付。

$invoice->addBadge($badge);

badge {string} 包含徽章文本的字符串。

可以将徽章的颜色设置为第二个参数。

$invoice->addBadge('Paid', '#00ff00');
// Short hex variant is also supported
$invoice->addBadge('Payment pending', '#f00');

CSS 颜色名称('red'、'cyan'、'fuchsia' 等)不支持

添加标题

您可以在文档底部添加标题和段落,以显示有关付款详情或运输信息等信息。

$invoice->addTitle($title);

title {string} 包含要显示在徽章中的标题的字符串。

添加段落

您可以在文档底部添加标题和段落,以显示有关付款详情或运输信息等信息。

$invoice->addParagraph($paragraph);

Paragraph {string} 包含具有多行支持的段落文本的字符串。使用 <br>\n 添加换行符。

动态更改语言术语

您可以使用此方法更改语言术语。这将覆盖语言文件中的术语。

$invoice->changeLanguageTerm($term, $new);
$invoice->changeLanguageTerm('date', 'Confirmation Date');

页脚

您想在文档左下角显示的简短文本。

$invoice->setFooternote($note);

note {string} 包含您想在页脚中显示的信息的字符串。

渲染发票

$invoice->render($name, $output);
  • name {string} 包含您的发票名称的字符串。例如:'invoice.pdf'
  • output {string} 选择您希望如何将发票发送给用户的选项。以下选项可用
    • I(将文件内联发送到浏览器)
    • D(发送到浏览器并强制使用给定的名称下载文件)
    • F(保存到本地文件。确保在 name 参数中设置路径)
    • S(将文档作为字符串返回)

致谢