将给定的一个URL或HTML页面生成缩略图,快照或PDF的PHP5类库:Snappy
jopen
10年前
Snappy是一个PHP5类库用于将一个给定的URL或HTML页面生成缩略图,快照或PDF文件。它采用了优秀的基于WebKit的wkhtmltopdf和wkhtmltoimage。可用在 OSX, linux, windows系统中。
<?php require_once '/path/to/snappy/src/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('<h1>Bill</h1><p>You owe me money, dude.</p>', '/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'); $snappy->setOption('toc', true);