Skip to content

[Bug]: Could not render template due to missing CSS asset #218

@dloubere

Description

@dloubere

Reproducer

Install Symfony 7.4

$ git clone https://github.com/dunglas/symfony-docker.git sf74
$ cd sf74/
$ docker compose build --pull --no-cache
$ SYMFONY_VERSION=7.4.* docker compose up --wait

Install Asset Mapper, Gotenberg bundle, Bootstrap

$ docker compose exec php composer require symfony/asset-mapper symfony/asset symfony/twig-pack
$ docker compose exec php composer require sensiolabs/gotenberg-bundle 1.1
$ docker compose exec php bin/console importmap:require bootstrap

Add Bootstrap in assets/app.js

import 'bootstrap/dist/css/bootstrap.min.css';

Modify templates/base.html.twig

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        {% block javascripts %}
            {% block importmap %}{{ importmap('app') }}{% endblock %}
        {% endblock %}
    </head>
    <body>
    <div class="col-lg-8 mx-auto p-4 py-md-5">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">Lorem Ipsum</h5>
                <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
                    incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
                    laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
                    velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
                    in culpa qui officia deserunt mollit anim id est laborum.</p>
                <a href="{{ path('test_pdf') }}" target="_blank" class="card-link">Generate PDF</a>
            </div>
        </div>
    </body>
</html>

Add templates/doc.html.twig

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <link href="{{ gotenberg_asset('vendor/bootstrap/dist/css/bootstrap.min.css') }}" rel="stylesheet"/>
    </head>
    <body>
    <div class="col-lg-8 mx-auto p-4 py-md-5">
        <div class="card">
            <div class="card-body">
                <h5 class="card-title">Lorem Ipsum</h5>
                <p class="card-text">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
                    incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco
                    laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
                    velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
                    in culpa qui officia deserunt mollit anim id est laborum.</p>
            </div>
        </div>
    </body>
</html>

Create controller

<?php

namespace App\Controller;

use Sensiolabs\GotenbergBundle\GotenbergPdfInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class TestController extends AbstractController
{
    #[Route('/test', name: 'test')]
    public function test(): Response
    {
        return $this->render('base.html.twig');
    }

    #[Route('/test/pdf', name: 'test_pdf')]
    public function testPdf(GotenbergPdfInterface $gotenberg): Response
    {
        return $gotenberg->html()
            ->content('doc.html.twig')
            ->generate()
            ->stream();
    }
}

Rebuild and test it

$ docker compose up --build

=> https://localhost/test

The app crash:

Could not render template "doc.html.twig" into PDF part "index.html". An exception has been thrown during the rendering of a template ("File "assets/vendor/bootstrap/dist/css/bootstrap.min-z0zQt-h.css" not found in assets directories: "/app/assets".") in doc.html.twig at line 5.

Now, if you downgrade the bundle to v1.0 (docker compose exec php composer require sensiolabs/gotenberg-bundle 1.0), you'll see doc rendering works as expected.

Description

Within a Docker context and asset mapper symfony/asset-mapper used to manage Bootstrap files, the problem occurs at rendering during the execution of gotenberg_asset Twig function.

Notes

  1. Rendering works as expected with v1.0.0 (bug started with v1.1.0)
  2. Whereas the reproducer uses Bootstrap as CSS to demonstrate the bug, it should break as well with other assets

Gotenberg Version

8.25.1

Gotenberg Bundle Version

1.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions