Call Symfony Controller from Gotenberg, itself called from an other Symfony Controller

61 Views Asked by At

I encountered the following problem using Gotenberg 7 and Symfony 6.4 together.

It seems like I cannot make a Request in a Controller which awaits for an other Request's answer. The first Request calls Gotenberg API and waits for its answer, while Gotenberg makes a Request to an other Controller and waits for its answer.

According to Gotenberg-php documentation and Gotenberg's, If I want to generate a PDF I have two choices: sending an HTML string to Gotenberg, or sending an URL to it. The latter feels simpler since I don't have to inject Webpack's generated script and style files while modifying their path in my HTML string.

For development purposes, I'm using Symfony-CLI server, which serves itself on port 8000. I haven't tested this in Prod yet. Gotenberg is ran from inside a Docker container, on port 3000.

So, in my Symfony project there is this static webpage that I created, it's rendered when going to https:/localhost:8000/home.

If I go to https:/localhost:8000/pdf/, I want a Gotenberg-generated PDF to be displayed.

Here's my HomeController:

<?php

namespace App\Controller;

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

class DefaultController extends AbstractController
{
    #[Route(path: "/home", name: "home")]
    public function showHome(): Response
    {
        return $this->render('Website/home.html.twig');
    }
}

And here's my PdfController:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Gotenberg\Gotenberg;

class PdfController extends AbstractController
{

    $request = Gotenberg::chromium('http://localhost:3000/')
        ->url('https://localhost:8000/home');

    try {
        $gotenbergResponse = Gotenberg::send($request);

        $contents = $gotenbergResponse->getBody()->getContents();

        return Response(
            $content,
            Response::HTTP_OK,
            [
                'Content-Type' => 'application/pdf',
                'Content-Disposition' => 'inline; filename="home.pdf"'
            ]
        )
    } catch (Exception $e) {
        throw new RuntimeException('Error: ' . $e->getMessage());
    }
}

If I put a breakpoint in DefaultController and an other one in ̀PdfController, I can see that:

  • PdfController is correctly hit;
  • In my Gotenberg logs, the route http://localhost:3000 is correctly hit and Gotenberg tries to access https://localhost:8000/home;
  • After a while, after no answer from https://localhost:8000/home, Gotenberg returns a 503 HTTP Response to my PdfController, which returns with an error;
  • At this precise moment, the breakpoint in DefaultController stops my Symfony application, showing that it was correctly hit.

It seems like my Symfony application, under this configuration, cannot handle more than one request at the same time. Is it because of my application, do I have to sue an other specific configuration for it to work, is it because I'm using Symfony server, or is it by design?

0

There are 0 best solutions below