Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Gotenberg Bundle Laravel Package

sensiolabs/gotenberg-bundle

Symfony bundle to generate PDFs and screenshots via the Gotenberg API. Convert from URL, HTML, Markdown, or Office files, then stream or save outputs locally. Supports source-specific options, advanced usage, and profiler/testing integrations.

View on GitHub
Deep Wiki
Context7

Latest Version Total Downloads Monthly Downloads Software License Static analysis Tests

Generate PDFs and screenshots with Symfony!

This bundle allows you to generate, stream and save PDF locally from URL, HTML, Markdown or any Office file. Different options are available depending on the source.

It also helps you to generate, stream and save images locally from URL, HTML and Markdown by taking a screenshot.

[!NOTE] This bundle interacts with the amazing Gotenberg API which is used under the hood.

๐Ÿ“ฆ How to install

โญ Basic Usage

๐ŸŒŸ Advanced Usage

๐Ÿ”Ž Profiler

โœ… Testing

๐Ÿ™‹ FAQ

โค๏ธ Credits

๐Ÿ“ƒ Licence

How to install

[!NOTE] You first need to install and configure Gotenberg 8.x by yourself.

Install the bundle using composer:

composer require sensiolabs/gotenberg-bundle

With Symfony Flex

If you accept the Symfony Flex recipe during installation:

  • The bundle will be automatically registered.
  • A configuration skeleton file will be created.
  • Docker Compose will be updated with a new gotenberg service.
  • The .env file will be updated with a GOTENBERG_DSN value pointing to gotenberg:3000. You can update this value if your Gotenberg instance is hosted elsewhere.

Without Symfony Flex

Manually enable the bundle by adding it to the list of registered bundles in your config/bundles.php file:

// config/bundles.php

return [
    // ...
    Sensiolabs\GotenbergBundle\SensiolabsGotenbergBundle::class => ['all' => true],
];

Create a configuration and adapt to your needs:

# ./config/packages/sensiolabs_gotenberg.yaml

framework:
    http_client:
        scoped_clients:
            gotenberg.client:
                base_uri: 'http://gotenberg:3000'

sensiolabs_gotenberg:
    http_client: 'gotenberg.client'

Basic Usage

PDF

You can generate a PDF locally from URL, HTML, Markdown or any Office files.

URL

After injecting GotenbergPdfInterface you simply need to call the method url, which will return a UrlPdfBuilder instance.

UrlPdfBuilder lets you pass the URL of the page you want to convert into PDF to the method url.

namespace App\Controller;

use Sensiolabs\GotenbergBundle\GotenbergPdfInterface;

class YourController
{
    public function yourControllerMethod(GotenbergPdfInterface $gotenberg): Response
    {
        return $gotenberg->url()
            ->url('https://sensiolabs.com/fr/')
            ->generate()
            ->stream() // will return directly a stream response
        ;
    }
}

[!TIP] For more information go to Gotenberg documentations.

Twig

[!WARNING] Every Twig template you pass to Gotenberg must have the following structure. Even Header or Footer parts.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>My PDF</title>
    </head>
    <body>
        <!-- Your code goes here -->
    </body>
</html>
namespace App\Controller;

use Sensiolabs\GotenbergBundle\GotenbergPdfInterface;

class YourController
{
    public function yourControllerMethod(GotenbergPdfInterface $gotenberg): Response
    {
        return $gotenberg->html()
            ->content('twig_simple_pdf.html.twig', [
                'my_var' => 'value'
            ])
            ->generate()
            ->stream() // will return directly a stream response
        ;
    }
}

If a template needs to link to a static asset (e.g. an image), this bundle provides a {{ gotenberg_asset() }} Twig function to generate the correct path AND add it to the builder automatically.

This function work as asset() Twig function and fetch your assets in the assets folder of your application. If your files are in another folder, you can override the default value of assets_directory in your configuration file config/sensiolabs_gotenberg.yml. The path provided can be relative as well as absolute.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <title>PDF body</title>
</head>
<body>
    <main>
        <h1>Hello world!</h1>
        <img src="{{ gotenberg_asset('public/img/ceo.jpeg') }}" alt="CEO"/>
        <img src="{{ gotenberg_asset('public/img/admin.jpeg') }}" alt="Admin"/>
    </main>
</body>
</html>

[!TIP] For more information go to Gotenberg documentations.

Screenshot

You can generate a screenshot locally from URL, HTML and Markdown.

URL

After injecting GotenbergScreenshotInterface you simply need to call the method url, which will return a UrlScreenshotBuilder instance.

UrlScreenshotBuilder lets you pass the URL of the page you want to convert into screenshot to the method url.

namespace App\Controller;

use Sensiolabs\GotenbergBundle\GotenbergScreenshotInterface;

class YourController
{
    public function yourControllerMethod(GotenbergScreenshotInterface $gotenberg): Response
    {
        return $gotenberg->url()
             ->url('https://sensiolabs.com/fr/')
             ->generate()
             ->stream()
        ;
    }
}

Twig

After injecting GotenbergScreenshotInterface you simply need to call the method html, which will return a HtmlScreenshotBuilder instance.

HtmlScreenshotBuilder lets you pass the content of the page you want to convert into screenshot to the method content.

namespace App\Controller;

use Sensiolabs\GotenbergBundle\GotenbergScreenshotInterface;

class YourController
{
    public function yourControllerMethod(GotenbergScreenshotInterface $gotenberg): Response
    {
        return $gotenberg->html()
            ->content('twig_simple_pdf.html.twig', [
                 'my_var' => 'value'
            ])
            ->generate()
            ->stream()
        ;
    }
}

[!TIP] For more information go to Gotenberg documentations.

Advanced Usage

  1. Configuration
  2. Processing (saving for example)
  3. Working with assets
  4. Async & Webhooks
  5. Working with fonts

PDF

  1. Add header / footer

  2. HTML Builder

  3. Markdown Builder

  4. Url Builder

  5. Office Builder (available extensions for conversion below)

    ๐Ÿ“ doc, docx, docm, dot, dotx, dotm, odt, ott, sdw, stw, sxw, sxg, fodt, rtf, txt,

    abw, zabw, cwk, psw, lwp, mcw, wpd, wps, pages, hwp, uof, uot

    ๐Ÿ“Š xls, xlsx, xlsm, xlsb, xlt, xltx, xltm, xlw, ods, ots, sdc, stc, sxc, uos, csv,

    dif, slk, 123, wk1, wks, wb2

    ๐Ÿ“ฝ๏ธ ppt, pptx, pptm, pot, potx, potm, pps, odp, otp, sdd, sdp, sxi, sti, uop, key

    ๐Ÿ–ผ๏ธ svg, cdr, odg, otg, sda, sxd, std, svm, fodg, eps, emf, wmf, dxf, cgm, cmx, met,

    mml, vdx, vsd, vsdx, vsdm, vor, bmp, gif, jpeg, jpg, png, tif, tiff, pbm, pgm,

    ppm, ras, pcx, pcd, pct, psd, tga, xbm, xpm, wpg

    ๐Ÿ“š epub, pdf, odd, odm, oth, html, htm, xhtml, xml, pub, pwp, bib, ltx

    ๐Ÿ—ƒ๏ธ dbf, pdb, wb2, mw

    ๐Ÿงฉ swf, smf

    ๐Ÿ—๏ธ dxf, vdx, vsd, vsdx, vsdm

    ๐Ÿงช sxm, mml, ltx, mw

  6. Merge Builder

  7. Convert Builder

  8. Split Builder

  9. Flatten Builder

  10. Encrypt Builder

  11. Embed Builder

Screenshot

  1. HTML Builder
  2. Markdown Builder
  3. Url Builder

Profiler

Comes with a built-in profiler panel to help you during your development.

Testing

This bundle provides classes to assist with testing when using PHPUnit.

  1. Creating mock results
  2. Builder Testing Support

FAQ

--- a/compose.yaml
+++ b/compose.yaml
@@ -1,6 +1,9 @@
services:
     gotenberg:
         image: 'gotenberg/gotenberg:8'
+         command:
+             - 'gotenberg'
+             - '--chromium-ignore-certificate-errors'

It can also be because from Gotenberg PoV the URL of your Symfony app is not reachable. Let's say you are using symfony CLI to run your project locally with Gotenberg running in Docker. You need to configure the request_context like so:

--- a/config/packages/gotenberg.yaml
+++ b/config/packages/gotenberg.yaml
@@ -6,5 +6,5 @@ framework:

sensiolabs_gotenberg:
    http_client: 'gotenberg.client'
+    request_context:
+        base_uri: 'http://host.docker.internal:8000' # 8000 is the port Symfony CLI is running my app on.

Upgrade

Credits

This bundle was inspired by Gotenberg PHP.

Licence

MIT License (MIT): see the License File for more details.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky