Code Monkey home page Code Monkey logo

gotenberg-php-client's Introduction

⚠️ For Gotenberg 7.x, use gotenberg-/gotenberg-php instead.

This library is unmaintained and will not receive updates. Consider migrating to Gotenberg 7.x and the according new library instead.

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

gotenberg-php-client's People

Contributors

bendavies avatar florentdestremau avatar gulien avatar lcp0578 avatar mdjward avatar rodnaph avatar tigitz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gotenberg-php-client's Issues

Helpers to generate pdf directly from html string

Expected Behavior

Helpers to generate pdf directly from html string

Current Behavior

Helpers only exists to generate from a file

Possible Solution

DocumentFactory::makeFromString(string $fileName, string $html)

public static function makeFromString(string $fileName, string $htmlContent): Document
    {
        $stream = fopen('php://temp','r+');
        fwrite($stream, $html);
        
        return new Document($fileName, stream_for($stream));
    }

Context

Using a templating engine to produce the html, requiring to retrieve the html from a file through DocumentFactory::makeFromPath(string $html) would imply an unnecessary I/O operation to store the html on the hard drive and reload it from it while you already have the html content in php memory.

I can make a PR if solution seems fine to you

How to increase waitTimeout?

Hi there

I am trying to convert a rather "large" html file (lots of small images) into a PDF.

Could anybody point out how I could increase the waitTimeout?

{"message":"'waitTimeout' should be \u003c '30.000000', got '60.000000'"}

Currently I am useing this code:

        $opts=array(
            "ssl"=>array(
                "verify_peer"=>false,
                "verify_peer_name"=>false,
            ),
        );

        $resource = fopen($url, 'r', false, stream_context_create($opts));
        $stream = new Stream($resource);


        $client = new Client('http://gotenberg:3000');
        $index = DocumentFactory::makeFromStream($filename, $stream);

        $request = new HTMLRequest($index);
        $request->setPaperSize(Request::A4);
        $request->setMargins(Request::NO_MARGINS);
        $request->setScale(1);
        $request->setWaitTimeout(60);
```

BR wucherpfennig

Page size error

Hi,

I'm trying to generate a PDF using your examples, when I try to generate a pdf setting the Page size or the page scale, I got this error:

thecodingmachine/gotenberg-php-client/src/Client.php' on line 106 : 
{"message":"'paperWidth' is not a float, got '8,27'"}

I set those information getting it from static values in PHP's Gotenberg client, as provided in examples:

                        $request->setPaperSize(Request::A4);
                        $request->setMargins(Request::NO_MARGINS);
                       $request->setScale(0.75);

I'm using PHP 7.3 from Debian Bursty version.
Is there any explanation or is it a bug ?

Anyway, thank for you work around pdf generation!

composer require Your requirements could not be resolved to an installable set of packages.

chunpengs-MacBook-Pro:wateroa lcp0578$ composer7 require thecodingmachine/gotenberg-php-client
Using version ^3.1 for thecodingmachine/gotenberg-php-client
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Installation request for thecodingmachine/gotenberg-php-client ^3.1 -> satisfiable by thecodingmachine/gotenberg-php-client[3.1.0].
    - Conclusion: remove php-http/httplug v2.0.0
    - Conclusion: don't install php-http/httplug v2.0.0
    - thecodingmachine/gotenberg-php-client 3.1.0 requires php-http/httplug ^1.0 -> satisfiable by php-http/httplug[v1.0.0, v1.1.0].
    - Can only install one of: php-http/httplug[v1.0.0, v2.0.0].
    - Can only install one of: php-http/httplug[v1.1.0, v2.0.0].
    - Installation request for php-http/httplug (locked at v2.0.0) -> satisfiable by php-http/httplug[v2.0.0].


Installation failed, reverting ./composer.json to its original content.
chunpengs-MacBook-Pro:wateroa lcp0578$
chunpengs-MacBook-Pro:wateroa lcp0578$
chunpengs-MacBook-Pro:wateroa lcp0578$ php7 -v
PHP 7.2.4 (cli) (built: Apr  4 2018 09:44:31) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
chunpengs-MacBook-Pro:wateroa lcp0578$

Allow "guzzlhttp/psr7": "^2"

Current Behavior

I have deprecation warnings because guzzlhttp/psr7 (a dependency of this project) is still in v1. I would like to use v2, but the version constraints don't allow this.

Possible Solution

Loosen the requirements from "guzzlehttp/psr7": "^1.4.2" to guzzlehttp/psr7": "^1.4.2|^2.0

Random 404 not found crashes (how to debug?)

Not sure it's directly related, but I sometimes have 404 error on HTML generation cli batch process:

PHP log:

 A total of 296 invoice(s) will be validated. Confirm? (yes/no) [no]:
 > yes
  48/296 [▓▓▓▓░░░░░░░░░░░░░░░░░░░░░░░░]  16%[2020-03-06 23:13:52] console.ERROR: Error thrown while running command "{command}". Message: "{message}" {"exception":"[object] (TheCodingMachine\\Gotenberg\\ClientException(code: 404): 404 page not found\n at vendor/php/thecodingmachine/gotenberg-php-client/src/Client.php:106)","command":"nexy:invoice:validate","message":"404 page not found\n"} []
In Client.php line 106:
  404 page not found

Server log:

{"bytes_in":"22190","bytes_out":"49904","host":"pdf.domain.dev","latency":"2978634042","latency_human":"2.978634042s","level":"info","method":"POST","msg":"request handled","op":"context.Context.LogRequestResult","path":"/convert/html","referer":"","remote_ip":"178.170.104.70","status":200,"time":"2020-03-06T22:24:26Z","trace":"2246dyEJKiCmxJW7T540Qc1c6IucrXx5","uri":"/convert/html","user_agent":"GuzzleHttp/6.3.3 curl/7.65.0 PHP/7.3.11"}
{"bytes_in":"9288","bytes_out":"34048","host":"pdf.domain.dev","latency":"2744331022","latency_human":"2.744331022s","level":"info","method":"POST","msg":"request handled","op":"context.Context.LogRequestResult","path":"/convert/html","referer":"","remote_ip":"178.170.104.70","status":200,"time":"2020-03-06T22:26:05Z","trace":"jl5XmA38KllYHzR6LbBcPWv22ws15pkf","uri":"/convert/html","user_agent":"GuzzleHttp/6.3.3 curl/7.65.0 PHP/7.3.11"}

As you can see, the error details is not very helpful.

Does this library has option about how to debug a bit more what was done during the request? 🤔

how to convert a remote office url file?

Hi!.
I have used aws s3 to store uploaded file. There are doc & docx file. I can get url of file from s3.
How can i convert file with DocumentFactory?
Thanks!

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.