Skip to content

feat: add lazy template rendering to stream HTML at send time#279

Open
Jean-Beru wants to merge 2 commits into
sensiolabs:1.xfrom
Jean-Beru:feat/lazy-template-rendering
Open

feat: add lazy template rendering to stream HTML at send time#279
Jean-Beru wants to merge 2 commits into
sensiolabs:1.xfrom
Jean-Beru:feat/lazy-template-rendering

Conversation

@Jean-Beru

@Jean-Beru Jean-Beru commented Jun 5, 2026

Copy link
Copy Markdown
Contributor
Q A
Gotenberg API version ? 8.x
Bug fix ? no
New feature ? yes
BC break ? no

Description

When calling content($template, $context) (or header() / footer()), the Twig template is rendered immediately and the resulting HTML is kept in memory until the request is sent to Gotenberg.

This PR adds a $lazy argument to content(), header() and footer(). When set to true, the template is only rendered while the HTTP request body is sent. Peak memory usage is then a single Twig output chunk instead of the full document.

Lazy rendering is disabled by default to keep BC promise.

Example

public function __invoke(GotenbergPdfInterface $gotenberg): Response
{
    return $gotenberg->html()
        ->content('invoice.html.twig', ['invoice' => $invoice], lazy: true)
        ->generate()
        ->stream();
}

Screenshots

Example: 5000 lines containing an inlined svg + a paragraph.

Before

image

After

image

@Jean-Beru Jean-Beru force-pushed the feat/lazy-template-rendering branch from ff39f8d to 372a890 Compare June 5, 2026 17:26
Add a $lazy argument to content(), header() and footer() to defer Twig
rendering until the HTTP request body is sent, streaming the HTML chunk
by chunk to Gotenberg instead of buffering it in memory. Disabled by
default to avoid any BC break.

Assisted-by: Claude Code:claude-opus-4-8
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Jean-Beru <lenoir.hubert@gmail.com>
@Jean-Beru Jean-Beru force-pushed the feat/lazy-template-rendering branch from 372a890 to 1ea348d Compare June 5, 2026 17:51
@Jean-Beru Jean-Beru marked this pull request as draft June 5, 2026 18:02
@Jean-Beru Jean-Beru force-pushed the feat/lazy-template-rendering branch from 8af3d0d to 75085fa Compare June 6, 2026 04:09
Twig templates yield lots of tiny chunks (one per text node or
expression). Each one was sent by curl as a separate HTTP chunk,
slowing the transfer down until Gotenberg aborted the request with
"get multipart form: i/o timeout". Buffer chunks to 16KB before
yielding them.

Assisted-by: Claude Code:claude-opus-4-8
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Jean-Beru <lenoir.hubert@gmail.com>
@Jean-Beru Jean-Beru force-pushed the feat/lazy-template-rendering branch from 75085fa to 0143140 Compare June 8, 2026 12:48
@Jean-Beru Jean-Beru marked this pull request as ready for review June 8, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant