phpcfdi / cfditopdf
从 CFDI 3.3 & 4.0 创建通用的 PDF 文件(包含 CLI)
v0.4.2
2024-09-02 19:33 UTC
Requires
- php: >=7.3
- eclipxe/cfdiutils: ^2.29
- league/plates: ^3.5
- phpcfdi/cfdi-cleaner: ^1.3.3
- spipu/html2pdf: ^5.2.8
Requires (Dev)
- phpunit/phpunit: ^9.5
README
从 CFDI 3.3 & 4.0 创建通用的 PDF 文件
在某些情况下,您可能只需要从墨西哥的 CFDI(Comprobante Fiscal Digital por Internet)生成一个 PDF 文件。这个工具可以帮助您创建一个通用的文件。您也可以用它来构建自己的美观格式。
安装
使用 composer,请运行
composer require phpcfdi/cfditopdf
基本 CLI 使用方法
$ bin/cfditopdf [options] <cfdi-file> [<pdf-file>]
-h, --help Show this help
-V, --version Show command version
-d, --dirty Do not try to clean up the cfdi file
-f, --fonts-dir Path where TCPDF fonts are located
-l, --resource-location Use this path to store the xml resources locally,
if none then it will always download xlst resources
cfdi-file Path of the XML file (input file)
pdf-file Path of the PDF file (output file) if none then it will remove
".xml" extension and suffix ".pdf" extension
作为 PHP 库的基本使用方法
<?php declare(strict_types=1); $cfdifile = 'datafiles/cfdi.xml'; $xml = file_get_contents($cfdifile); // clean cfdi $xml = \PhpCfdi\CfdiCleaner\Cleaner::staticClean($xml); // create the main node structure $comprobante = \CfdiUtils\Nodes\XmlNodeUtils::nodeFromXmlString($xml); // create the CfdiData object, it contains all the required information $cfdiData = (new \PhpCfdi\CfdiToPdf\CfdiDataBuilder()) ->build($comprobante); // create the converter $converter = new \PhpCfdi\CfdiToPdf\Converter( new \PhpCfdi\CfdiToPdf\Builders\Html2PdfBuilder() ); // create the invoice as output.pdf $converter->createPdfAs($cfdiData, 'output.pdf');
要更改从 CfdiData
到 HTML 的数据转换方式,可以在构建对象时为 Html2PdfBuilder
提供一个专门的翻译器。
以下示例使用默认的 HTML 翻译器,它使用 Plates,仅更改模板所在目录和模板名称。预期结果必须与 Html2Pdf 兼容。
<?php declare(strict_types=1); $htmlTranslator = new \PhpCfdi\CfdiToPdf\Builders\HtmlTranslators\PlatesHtmlTranslator( 'directory_where_templates_are_located', 'main_template_name' ); $converter = new \PhpCfdi\CfdiToPdf\Converter( new \PhpCfdi\CfdiToPdf\Builders\Html2PdfBuilder($htmlTranslator) );
PHP 支持
此库至少与最老的 PHP 支持版本 兼容,并具有 活跃 支持。请尽量使用 PHP 的全部潜力。
我们遵循 语义版本控制。我们不会在主要版本中引入任何向后不兼容的更改。
内部类(使用 @internal
注解)不属于此协议,因为它们必须仅存在于此项目中。不要在您的项目中使用它们。
贡献
欢迎贡献!请阅读 CONTRIBUTING 以获取详细信息,并不要忘记查看 TODO 和 CHANGELOG 文件。
版权和许可
phpcfdi/cfditopdf
库版权所有 © PHPCFDI,许可使用 MIT 许可证(MIT)。有关更多信息,请参阅 LICENSE。