thecodingmachine/gotenberg-php-client


PHP client for the Gotenberg API

License: MIT

Language: PHP

Keywords: api, excel, gotenberg, html, markdown, pdf, pdf-converter, php, powerpoint, word


Gotenberg PHP client

A simple PHP client for interacting with a Gotenberg API.

Install

Unless your project already have a PSR7 HttpClient, install php-http/guzzle6-adapter:

$ composer require php-http/guzzle6-adapter

Then the PHP client:

$ composer require thecodingmachine/gotenberg-php-client

Usage

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);
    
    # 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.
}

For more complete usages, head to the documentation.

Badges

Travis CI Scrutinizer

Project Statistics

Sourcerank 5
Repository Size 398 KB
Stars 56
Forks 6
Watchers 9
Open issues 0
Dependencies 16
Contributors 3
Tags 16
Created
Last updated
Last pushed

Top Contributors See all

Julien Neuhart Philippe SEGATORI lcpeng

Packages Referencing this Repo

thecodingmachine/gotenberg-php-client
A client for sending files to a Gotenberg API
Latest release 6.1.0 - Updated - 56 stars

Recent Tags See all

6.1.0 December 10, 2019
6.0.1 October 07, 2019
6.0.0 August 23, 2019
5.0.0 April 14, 2019
4.3.0 February 01, 2019
4.1.0 February 01, 2019
4.0.0 January 28, 2019
3.2.0 January 11, 2019
3.1.1 December 19, 2018
3.1.0 December 12, 2018
3.0.0 December 10, 2018
3.0.0 December 10, 2018
2.0.1 May 30, 2018
2.0.0 April 19, 2018
1.0.1 April 19, 2018

Something wrong with this page? Make a suggestion

Last synced: 2019-12-10 15:22:59 UTC

Login to resync this repository