php snappy,Snappy:从一个URL或html页面生成缩略图,快照或PDF的PHP5库

Snappy

Snappy is a PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. It uses the excellent webkit-based wkhtmltopdf and wkhtmltoimage available on OSX, linux, windows.

You will have to download wkhtmltopdf 0.12.x in order to use Snappy.

Please, check FAQ before opening a new issue. Snappy is a tiny wrapper around wkhtmltox, so lots of issues are already answered, resolved or wkhtmltox ones.

e3ddab43877419e6cdbd248a45f26885.png

Installation using Composer

$ composer require knplabs/knp-snappy

Usage

require __DIR__ . '/vendor/autoload.php';

use Knp\Snappy\Pdf;

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');

// or you can do it in two steps

$snappy = new Pdf();

$snappy->setBinary('/usr/local/bin/wkhtmltopdf');

// Display the resulting pdf in the browser

// by setting the Content-type header to pdf

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');

header('Content-Type: application/pdf');

header('Content-Disposition: attachment; filename="file.pdf"');

echo $snappy->getOutput('http://www.github.com');

// Merge multiple urls into one pdf

// by sending an array of urls to getOutput()

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');

header('Content-Type: application/pdf');

header('Content-Disposition: attachment; filename="file.pdf"');

echo $snappy->getOutput(array('http://www.github.com','http://www.knplabs.com','http://www.php.net'));

// .. or simply save the PDF to a file

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');

$snappy->generateFromHtml('

Bill

You owe me money, dude.

', '/tmp/bill-123.pdf');

// Pass options to snappy

// Type wkhtmltopdf -H to see the list of options

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');

$snappy->setOption('disable-javascript', true);

$snappy->setOption('no-background', true);

$snappy->setOption('allow', array('/path1', '/path2'));

$snappy->setOption('cookie', array('key' => 'value', 'key2' => 'value2'));

$snappy->setOption('cover', 'pathToCover.html');

// .. or pass a cover as html

$snappy->setOption('cover', '

Bill cover

');

$snappy->setOption('toc', true);

$snappy->setOption('cache-dir', '/path/to/cache/dir');

wkhtmltopdf binary as composer dependencies

If you want to download wkhtmltopdf and wkhtmltoimage with composer you add to composer.json:

$ composer require h4cc/wkhtmltopdf-i386 0.12.x

$ composer require h4cc/wkhtmltoimage-i386 0.12.x

or this if you are in 64 bit based system:

$ composer require h4cc/wkhtmltopdf-amd64 0.12.x

$ composer require h4cc/wkhtmltoimage-amd64 0.12.x

And then you can use it

use Knp\Snappy\Pdf;

$myProjectDirectory = '/path/to/my/project';

$snappy = new Pdf($myProjectDirectory . '/vendor/h4cc/wkhtmltopdf-i386/bin/wkhtmltopdf-i386');

// or

$snappy = new Pdf($myProjectDirectory . '/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64');

N.B. These static binaries are extracted from Debian7 packages, so it might not be compatible with non-debian based linux distros

Some use cases

If you want to generate table of contents and you want to use custom XSL stylesheet, do the following:

$snappy = new Pdf('/path/to/binary');

$snappy->setOption('toc', true);

$snappy->setOption('xsl-style-sheet', 'http://path/to/stylesheet.xsl') //or local file;

$snappy->generateFromHtml('

Some content

', 'test.pdf');

Credits

Snappy has been originally developed by the KnpLabs team.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值