dlin / snappy-bundle
Symfony Bundle for Snappy ( wkhtmltopdf )
v0.9.3
2014-01-22 23:27 UTC
Requires
- php: >=5.3.8
- google/wkhtmltopdf-amd64: 0.11.0-RC1
- google/wkhtmltopdf-i386: 0.11.0-RC1
- knplabs/knp-snappy: *
Requires (Dev)
- symfony/finder: 2.3.*
- symfony/framework-bundle: >=2.0
This package is not auto-updated.
Last update: 2024-09-23 14:13:09 UTC
README
Dlin Symfony Snappy Bundle 是对 Snapy 的包装扩展
Snapy 是一个PHP5库,允许从URL或HTML页面生成缩略图、快照或PDF。它使用基于webkit的wkhtmltopdf和wkhtmltoimage,这些在OSX、linux、windows上都可以使用。
Dlin Symfony Snappy Bundle 提供了一个可配置的服务来处理PDF文件
安装
使用 Composer 安装
注意:不幸的是,Composer不支持嵌套依赖的仓库,我们必须在根composer.json中包含依赖和仓库。
将以下内容添加到您的 composer.json
json
{
"require" : {
....
"dlin/snappy-bundle": "dev-master",
"google/wkhtmltopdf-amd64": "0.11.0-RC1",
"google/wkhtmltopdf-i386": "0.11.0-RC1",
},
....
"repositories": [
{
"type": "package",
"package": {
"name": "google/wkhtmltopdf-amd64",
"version": "0.11.0-RC1",
"dist": {
"url": "http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2",
"type": "tar"
}
}
},
{
"type": "package",
"package": {
"name": "google/wkhtmltopdf-i386",
"version": "0.11.0-RC1",
"dist": {
"url": "http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-i386.tar.bz2",
"type": "tar"
}
}
}
]
}
在 AppKernel.php 中启用此扩展包
public function registerBundles()
{
$bundles = array(
...
new Dlin\Bundle\SnappyBundle\DlinSnappyBundle(),
...
}
配置
您可以指定wkhtmltopdf的安装位置
#app/config/config.yml
dlin_snappy:
pdf_service:
wkhtmltopdf: /Applications/wkhtmltopdf.app/Contents/MacOS/wkhtmltopdf
对于大多数操作系统,此扩展包将尝试自行下载和安装wkhtmltopdf二进制文件。除非您想使用不同的wkhtmltopdf二进制文件,否则不需要配置。对于Mac服务器,需要下载DMG文件并安装。需要上述配置。
用法
在控制器中获取服务
$pdf = $this->get('dlin.pdf_service');
在 ContainerAwareService 中获取服务
$pdf = $this->container->get('dlin.pdf_service');
使用 "createPdfFromHtml" 方法
#Pdf will be created (replace if already exist) as file '/tmp/test.pdf'
$pdf->createPdfFromHtml('<html><body><h1>hello</h1></body>', '/tmp/test.pdf');
使用 "createPdfFromUrl" 方法
#Pdf will be created (replace if already exist) as file '/tmp/test.pdf'
$pdf->createPdfFromUrl('google.com', '/tmp/test.pdf');
下载到浏览器(将设置HTTP头并终止脚本)
$pdf->sendHtmlAsPdf('<html><body><h1>hello</h1></body>', 'downloadFileName.pdf');
#or
$pdf->sendUrlAsPdf('google.com', 'downloadFileName.pdf');
在浏览器中内联显示PDF(将设置HTTP头并终止脚本)
$pdf->sendHtmlAsPdf('<html><body><h1>hello</h1></body>', 'downloadFileName.pdf', true);
#or
$pdf->sendUrlAsPdf('google.com', 'downloadFileName.pdf', true);
注意
- MAMP用户可能在使用wkhtmltopdf时遇到问题。请在此处解决问题
- MacOS X需要自己的wkhtmltopdf二进制文件。您可以从此处下载。
- 在您的 *nix 服务器上,您可能需要安装一个库(sudo apt-get install libxrender1)
许可证
MIT
自由软件,是的!