How to Use Gotenberg PHP Client

Dec 11, 2023 | Programming

If you’re looking to integrate the Gotenberg API into your PHP application, the Gotenberg PHP client is your go-to solution. This article will guide you through the installation, usage, and troubleshooting of this library, making your development process as smooth as possible.

Step 1: Installation

Before getting started, you need to install a couple of dependencies. First, ensure you have a PSR7 HttpClient for your project. If you don’t have one yet, you can install the Guzzle 6 Adapter with the following command:

bash
$ composer require php-http/guzzle6-adapter

Once you have the adapter, you can install the Gotenberg PHP client:

bash
$ composer require thecodingmachine/gotenberg-php-client

Step 2: Usage

Using the Gotenberg PHP client is straightforward. Let’s break it down using an analogy:

Imagine you are a chef in a kitchen preparing a meal (in this case, generating a PDF). First, you’ll need your ingredients (HTML files and assets). Then you’ll prepare your kitchen (the client). Finally, you’ll follow the recipe (the request) to produce your delightful dish (the PDF).

Here’s how you can do it step by step:

php
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;

// Set up the client to connect to the Gotenberg API
$client = new Client('http://localhost:3000', new HttpAdapterGuzzle6Client());

// Prepare your HTML and assets
$index = DocumentFactory::makeFromPath('index.html', 'path/to/file');
$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'),
];

// Make a new HTML request with all your prepared elements
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 the resulting PDF in a designated location
    $client->store($request, 'path/you/want/the/pdf/to/be/stored.pdf');

    // Optionally, send the response directly to the browser
    // $client->post($request);
} catch (RequestException $e) {
    // Handle request issues related to size or margins
} catch (ClientException $e) {
    // Handle client API errors
}

Troubleshooting Tips

If you encounter any issues while using the Gotenberg PHP client, try the following troubleshooting steps:

  • Ensure that your Gotenberg server is running at the specified URL (http://localhost:3000).
  • Double-check file paths to ensure all the HTML and asset files are correctly referenced.
  • Verify that paper sizes and margins you are using are valid to avoid any exceptions.
  • If you receive an error related to HTTP status codes, examine the server response for more details.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

You are now set up to use the Gotenberg PHP client! This library offers a flexible way to convert your HTML documents into PDFs seamlessly. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

For more complete usages, head to the documentation.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox