heyday / silverstripe-wkhtml
在 SilverStripe 中提供 Wkhtml 功能
3.0.0
2023-11-24 18:10 UTC
Requires
Requires (Dev)
- mikey179/vfsstream: ^1
- phpunit/phpunit: ^9.5
README
本模块为 Snappy 和 wkhtml 提供一个以 SilverStripe 为主的封装。
要求
- Wkhtml 二进制文件,wkhtmltopdf 或 wkhtmltoimage
安装
$ composer require "heyday/silverstripe-wkhtml"
如何使用
生成 PDF 或图像需要四个要素
Knp\Snappy\GeneratorInterface
,wkhtmltopdf 或 wkhtmltoimage 的封装器Heyday\SilverStripe\WkHtml\Input\InputInterface
,提供 HTML 内容Heyday\SilverStripe\WkHtml\Output\OutputInterface
,以不同方式输出 PDF 或图像Heyday\SilverStripe\WkHtml\Generator
,将所有内容粘合在一起
可用的输入
- 请求(从请求生成内容)
- 文本字符串(内容由字符串指定)
- 模板(从 SilverStripe 模板生成内容)
- URL(从 URL 的 GET 请求生成内容)
- 查看器(从 SSViewer 实例生成内容)
可用的输出
- 浏览器(输出到浏览器)
- 文件(输出到文件)
- 随机文件(输出到随机文件名)
- 文本字符串(输出到字符串)
可用的生成器
- Image
示例
完整示例(从控制器操作)
SilverStripe\Core\Injector\Injector: # Create PDF generator as an injector service # This allows you to specify the binary path once and have it set up # automatically by getting the service from the injector. Knp\Snappy\Pdf: constructor: - '/bin/wkhtmltopdf' # Path to your WKTHMLTOPDF binary. Use '`SOME_ENV_VAR`' to define the binary path in .env
use Heyday\SilverStripe\WkHtml; use SilverStripe\Core\Injector\Injector; $generator = WkHtml\Generator::create( // Use Injector->get(Pdf::class) if you don't need to modify options // Use Injector->create() to create a transient service for modifications (e.g. setOption) // Using Injector->get() and making changes will cause changes to be made for all uses of get(Pdf::class) for the entire request Injector::inst()->create(\Knp\Snappy\Pdf::class), WkHtml\Input\Url::create('/'), WkHtml\Output\Browser::create('test.pdf', 'application/pdf') ); return $generator->process();
输入
请求
\Heyday\SilverStripe\WkHtml\Input\Request::create( // Controller::curr()->getRequest() is also an option Injector::inst()->get(\SilverStripe\Control\HTTPRequest::class) );
\Heyday\SilverStripe\WkHtml\Input\Request::create( Injector::inst()->get(\SilverStripe\Control\HTTPRequest::class), new Session([ 'arg' => 'value', ]) );
字符串
$html = <<<HTML <h1>Title</h1> HTML; \Heyday\SilverStripe\WkHtml\Input\TextString::create($html);
模板
\Heyday\SilverStripe\WkHtml\Input\Template::create('MyTemplate'); \Heyday\SilverStripe\WkHtml\Input\Template::create( 'MyTemplate', [ 'Var' => 'Hello', ] ); \Heyday\SilverStripe\WkHtml\Input\Template::create( 'MyTemplate', ArrayData::create([ 'Var' => 'Hello', ]) ); \Heyday\SilverStripe\WkHtml\Input\Template::create( '$Var World', ArrayData::create([ 'Var' => 'Hello', ]), true );
查看器
\Heyday\SilverStripe\WkHtml\Input\Viewer::create( SSViewer::create([ 'Template', ]), ArrayData::create([ 'Var' => 'Hello', ]) );
URL
\Heyday\SilverStripe\WkHtml\Input\Url::create('/'); \Heyday\SilverStripe\WkHtml\Input\Url::create('http://google.co.nz/');
输出
浏览器
\Heyday\SilverStripe\WkHtml\Output\Browser::create('test.pdf', 'application/pdf'); // Force download \Heyday\SilverStripe\WkHtml\Output\Browser::create('test.pdf', 'application/pdf', true); // Embeds
文件
\Heyday\SilverStripe\WkHtml\Output\File::create(BASE_PATH . '/test.pdf'); \Heyday\SilverStripe\WkHtml\Output\File::create(BASE_PATH . '/test.pdf', true); // Overwrite
随机文件
\Heyday\SilverStripe\WkHtml\Output\RandomFile::create(BASE_PATH);
字符串
\Heyday\SilverStripe\WkHtml\Output\TextString::create();
单元测试
$ composer install $ phpunit
许可证
本项目遵循 MIT 许可证