Code Monkey home page Code Monkey logo

gotenberg-php-client's Introduction

⚠️ Not working for Gotenberg >= 7 ⚠️

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

For more complete usages, head to the documentation.

Badges

Travis CI Scrutinizer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.