Video Tutorial Generate PDF with Gotenberg


In this video I propose you to discover Gotenberg, a docker-based tool that will allow you to quickly set up an API to convert different types of documents to PDF.

The use is simplified thanks to the use of a container docker.

docker run --rm -p 3000: 3000 thecodingmachine / gotenberg: 5

Then simply call the different endpoints of the API according to what you want to do. For example to convert an HTML document to PDF:

curl --request POST 
    --url http: // localhost: 3000 / convert / html 
    --header 'Content-Type: multipart / form-data' 
    --form files =@index.html 
    -o result.pdf

In addition to the docker image, gotenberg offers a client for go and PHP languages, which simplifies interactions with the API:

use TheCodingMachine  Gotenberg  Client;
use TheCodingMachine  Gotenberg  HTMLRequest;

// We generate the PDF code
$ faker =  Faker  Factory :: create ('en_US');
ob_start ();
require ( 'pdf.php');
$ content = ob_get_clean ();

// Create the request to API
$ index = DocumentFactory :: makeFromString ('index.html', $ content);
$ assets = (
    DocumentFactory :: makeFromPath ('pdf.css', 'pdf.css')
);
$ request = new HTMLRequest ($ index);
$ Request-> setAssets ($ assets);
$ request-> setFooter (DocumentFactory :: makeFromPath ('footer.html', 'footer.html'));

// generate the answer
$ client = new Client ('http: // localhost: 3000', new  Http  Adapter  Guzzle6  Client ());
$ response = $ client-> post ($ request);
$ response = $ response-> withHeader ('Content-Disposition', 'inline; filename = "invoice.pdf"');

// And we send it back to the user
 Http  Response  send ($ response);