internetnextstep / phpgears
使用 HTML 或 DOCX 模板创建 PDF 的工具。
Requires
- gears/di: *
- gears/string: ^0.6.0
- jakoch/phantomjs-installer: 1.9.8
- symfony/filesystem: 2.*
- symfony/process: 2.*
Requires (Dev)
- codegyre/robo: *
- google/apiclient: 1.*
- guzzlehttp/guzzle: 4.*
- phpunit/phpunit: 4.*
- sgh/pdfbox: dev-master
- symfony/finder: 2.*
Suggests
- google/apiclient: Install to support the Google Docx Converter.
This package is not auto-updated.
Last update: 2024-09-19 17:31:20 UTC
README
该项目最初是一个 DOCX 模板引擎。现在它已经发展,也支持使用无头版本的 webkit,phantomjs 将 HTML 转换为 PDF。
DOCX 模板非常适合那些客户需要随着时间的推移更新和管理文档的文档,尤其是文字密集型文档。例如,我使用它来自动生成一些法律合同,其中对像姓氏、公司名称和地址这样的属性进行简单替换。客户,一家保险公司,可以提供更新后的模板 Word 文档,可能包含对政策和其他条件的细微变化。
HTML 到 PDF 引擎非常适合需要更多文档设计控制的场景。它也更适合我们程序员使用,使用标准的 HTML 和 CSS,加上一些 JavaScript。
如何安装
通过 composer 安装很简单
composer require gears/pdf:*
您还需要将以下内容添加到您的根目录的 `composer.json` 文件中。
"scripts":
{
"post-install-cmd": ["PhantomInstaller\\Installer::installPhantomJS"],
"post-update-cmd": ["PhantomInstaller\\Installer::installPhantomJS"]
}
DOCX:如果您将使用 DOCX 模板,您需要在您的服务器上安装 libre-office-headless 或 unoconv。
如何使用,基础
通过主 `Pdf` 类访问这两个 API。
要将文档转换为 PDF 而不使用模板
$pdf = Gears\Pdf::convert('/path/to/document.docx');
将生成的 PDF 保存到文件中
Gears\Pdf::convert('/path/to/document.docx', '/path/to/document.pdf');
将 HTML 文档转换为 PDF
$pdf = Gears\Pdf::convert('/path/to/document.html');
注意:保存到文件对 HTML 文档同样适用。
DOCX 模板
默认情况下,DOCX 后端默认使用 `libre-office-headless`,要使用 `unoconv`,可以像这样覆盖转换器
$document = new Gears\Pdf('/path/to/document.docx');
$document->converter = function()
{
return new Gears\Pdf\Docx\Converter\Unoconv();
};
$document->save('/path/to/document.pdf');
注意:当前 HTML 后端只使用 phantomjs。
DOCX 引擎有几个模板方法。第一个是 setValue,它会替换所有实例
```php
$document->setValue('FOO', 'BAR');
```
To clone an entire block of DOCX xml, you surround your block with tags like:
```${BLOCK_TO_CLONE}``` & ```${/BLOCK_TO_CLONE}```. Whatever content is
contained inside this block will be repeated 3 times in the generated PDF.
```php
$document->cloneBlock('BLOCK_TO_CLONE', 3);
```
If you need to replace an entire block with custom DOCX xml you can.
But you need to make sure your XML conforms to the DOCX standards.
This is a very low level method and I wouldn't normally use this.
```php
$document->replaceBlock('BLOCK_TO_REPLACE', '<docx><xml></xml></docx>');
```
To delete an entire block, for example you might have particular
sections of the document that you only want to show to certian users.
```php
$document->deleteBlock('BLOCK_TO_DELETE');
```
Finally the last method is useful for adding new rows to tables.
Similar to the ```cloneBlock``` method. You place the tag in first cell
of the table. This row is the one that gets cloned.
```php
$document->cloneRow('ROW_TO_CLONE', 5);
```
__For more examples please see the [Unit Tests](https://github.com/phpgearbox/pdf/tree/master/tests).
These contain the PHP code to generate the final PDF along with the original DOCX templates.__
> NOTE: The HTML to PDF converter does not have these same templating functions.
> Obviously it's just standard HTML that you can template how ever you like.
HTML PhantomJs Print Environment
--------------------------------------------------------------------------------
This is still in development and subject to radical change.
So I won't document this section just yet...
Credits
--------------------------------------------------------------------------------
The DOCX templating code originally came from
[PHPWord](https://github.com/PHPOffice/PHPWord)
You may still like to use _PHPWord_ to generate your DOCX documents.
And then use this package to convert the generated document to PDF.
--------------------------------------------------------------------------------
Developed by Brad Jones - brad@bjc.id.au