此包已被废弃且不再维护。未建议替代包。

一个简单的库,用于使用 PhantomJS 将 HTML 文件转换为 PDF

v2.0.0 2014-07-20 04:51 UTC

This package is auto-updated.

Last update: 2020-11-26 00:28:56 UTC


README

轻松将您的 HTML 文件转换为 PDF。感谢 PhantomJS,您可以使用 CSS3、HTML5 和 JavaScript,并且它将以您在浏览器中看到的方式转换和打印文件。

如何使用

操作非常简单,但首先您必须根据您的系统(Windows、Mac、Linux-X86 或 Linux-X86_64)下载 PhantomJS 二进制文件,并将其放入正确的 bin 文件夹。

您可以从这里下载:http://phantomjs.org/download.html

入门

use H2P\Converter\PhantomJS;

$converter = new PhantomJS();

// Convert destination accepts H2P\TempFile or string with the path to save the file
$converter->convert('http://www.google.com/', '/home/user/Documents/page.pdf');

如果您想转换一个 HTML 字符串,可以这样操作

use H2P\Converter\PhantomJS;
use H2P\TempFile;

$converter = new PhantomJS();
$input = new TempFile('<b>test string to pdf</b>', 'html'); // Make sure the 2nd parameter is 'html'

// Convert destination accepts H2P\TempFile or string with the path to save the file
$converter->convert($input, '/home/user/Documents/page.pdf');

高级使用

use H2P\Converter\PhantomJS;
use H2P\TempFile;
use H2P\Request;
use H2P\Request\Cookie;

$converter = new PhantomJS(array(
    // You should use 'search_paths' when you want to point the phantomjs binary to somewhere else
    // 'search_paths' => shell_exec('which phantomjs'),
    'orientation' => PhantomJS::ORIENTATION_LANDSCAPE,
    'format' => PhantomJS::FORMAT_A4,
    'zoomFactor' => 2,
    'border' => '1cm',
    'header' => array(
        'height' => '1cm',
        'content' => "<h1>{{pageNum}} / {{totalPages}}</h1>",
    ),
    'footer' => array(
        'height' => '1cm',
        'content' => "<h1>{{pageNum}} / {{totalPages}}</h1>",
    ),
));

// Create a full custom request
$request = new Request(
    'http://www.google.com/',
    Request::METHOD_POST,
    array('param' => 'value'), // POST params
    array('X-Header' => 'value'), // Custom headers
    array(
        new Cookie('Cookie', 'value', 'domain'), // Create a basic cookie
    )
);

$destination = new TempFile();
$converter->convert($request, $destination);

您可以在 samples 文件夹中找到更多示例。

Composer

只需将 { "kriansa/h2p": "dev-master" } 放入您的 require 属性。

许可

  • MIT 许可证