thecodingmachine/gotenberg-php-client

此包已被废弃且不再维护。没有推荐替代包。

用于发送文件到 Gotenberg API 的客户端


README

⚠️ 对于 Gotenberg 7.x,请使用 gotenberg-/gotenberg-php

此库不再维护,将不会收到更新。请考虑迁移到 Gotenberg 7.x 及相应的新的库。

Gotenberg PHP 客户端

一个用于与 Gotenberg API 交互的简单 PHP 客户端。

安装

除非你的项目已经包含 PSR7 HttpClient,请安装 php-http/guzzle6-adapter

$ composer require php-http/guzzle6-adapter

然后是 PHP 客户端

$ composer require thecodingmachine/gotenberg-php-client

使用方法

use TheCodingMachine\Gotenberg\Client;
use TheCodingMachine\Gotenberg\ClientException;
use TheCodingMachine\Gotenberg\DocumentFactory;
use TheCodingMachine\Gotenberg\HTMLRequest;
use TheCodingMachine\Gotenberg\Request;
use TheCodingMachine\Gotenberg\RequestException;
use GuzzleHttp\Psr7\LazyOpenStream;

# create the client.
$client = new Client('http://localhost:3000', new \Http\Adapter\Guzzle6\Client());
# ... or the following if you want the client to discover automatically an installed implementation of the PSR7 `HttpClient`.
$client = new Client('http://localhost:3000');

# prepare the files required for your conversion.

# from a path.
$index = DocumentFactory::makeFromPath('index.html', '/path/to/file');
# ... or from your own stream.
$stream = new LazyOpenStream('/path/to/file', 'r');
$index = DocumentFactory::makeFromStream('index.html', $stream);
// ... or from a string.
$index = DocumentFactory::makeFromString('index.html', '<html>Foo</html>');

$header = DocumentFactory::makeFromPath('header.html', '/path/to/file');
$footer = DocumentFactory::makeFromPath('footer.html', '/path/to/file');
$assets = [
    DocumentFactory::makeFromPath('style.css', '/path/to/file'),
    DocumentFactory::makeFromPath('img.png', '/path/to/file'),
];

try {
    $request = new HTMLRequest($index);
    $request->setHeader($header);
    $request->setFooter($footer);
    $request->setAssets($assets);
    $request->setPaperSize(Request::A4);
    $request->setMargins(Request::NO_MARGINS);
    $request->setScale(0.75);
    
    # store method allows you to... store the resulting PDF in a particular destination.
    $client->store($request, 'path/you/want/the/pdf/to/be/stored.pdf');
    
    # if you wish to redirect the response directly to the browser, you may also use:
    $client->post($request);          
} catch (RequestException $e) {
    # this exception is thrown if given paper size or margins are not correct.
} catch (ClientException $e) {
    # this exception is thrown by the client if the API has returned a code != 200.
}

有关更完整的用法,请访问 文档

徽章

Travis CI Scrutinizer