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
    评论
Sentinel-1是一种合成孔径雷达(SAR)传感器,它可以用于获取地表反射率图像。它的数据格式是一种特殊的二进制格式,包含了很多元数据信息。在这里,我们可以使用snappy来读取Sentinel-1的数据,使用numpy来处理数据,最后再使用snappy将数据写回成Sentinel-1的文件格式。 首先,我们需要使用snappy来读取Sentinel-1的数据。以下是一个简单的例子: ```python import snappy # 读取Sentinel-1数据 product = snappy.ProductIO.readProduct('path/to/sentinel-1/file') # 获取数据集 dataset = product.getBand('Intensity_VV') # 获取数据集的宽度和高度 width = dataset.getRasterWidth() height = dataset.getRasterHeight() # 将数据集转换成numpy数组 data = dataset.readPixels(0, 0, width, height, None) # 将数据转换成二维数组 data = data.reshape((height, width)) ``` 接下来,我们可以使用numpy来处理数据。以下是一个简单的例子: ```python import numpy as np # 对数据进行一些处理 data = np.clip(data, 0, 1) data = np.round(data * 255).astype(np.uint8) ``` 最后,我们可以使用snappy将数据写回成Sentinel-1的文件格式。以下是一个简单的例子: ```python # 将数据转换成一维数组 data = data.flatten() # 创建一个新的数据集 new_dataset = snappy.ProductData.createInstance(width, height) # 将处理后的数据写入数据集 new_dataset.setPixels(0, 0, width, height, data) # 创建一个新的产品 new_product = snappy.Product('new_product', 'new_product_type', width, height) # 将数据集添加到产品中 new_product.addBand('new_band', new_dataset) # 将产品写入文件 snappy.ProductIO.writeProduct(new_product, 'path/to/new/sentinel-1/file', 'SENTINEL-1', 'DIMAP') ``` 这样,我们就可以使用snappy和numpy一个二维数据写回成Sentinel-1的文件格式了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值