feat: add lazy template rendering to stream HTML at send time#279
Open
Jean-Beru wants to merge 2 commits into
Open
feat: add lazy template rendering to stream HTML at send time#279Jean-Beru wants to merge 2 commits into
Jean-Beru wants to merge 2 commits into
Conversation
ff39f8d to
372a890
Compare
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>
372a890 to
1ea348d
Compare
8af3d0d to
75085fa
Compare
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>
75085fa to
0143140
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When calling
content($template, $context)(orheader()/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
$lazyargument tocontent(),header()andfooter(). When set totrue, 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
Screenshots
Example: 5000 lines containing an inlined svg + a paragraph.
Before
After