From 31c16cd7636891b220b60efbaa57b84cf84146ba Mon Sep 17 00:00:00 2001 From: Sacha Al Himdani Date: Wed, 25 Feb 2026 15:15:56 +0100 Subject: [PATCH] Fix long pdf exports Signed-off-by: Sacha Al Himdani --- pkg/html2pdf/converter.go | 40 +++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/pkg/html2pdf/converter.go b/pkg/html2pdf/converter.go index 4bedce6ea..e36b533c2 100644 --- a/pkg/html2pdf/converter.go +++ b/pkg/html2pdf/converter.go @@ -18,7 +18,6 @@ import ( "bytes" "context" _ "embed" - "encoding/base64" "fmt" "io" "strings" @@ -134,11 +133,6 @@ func (c *Converter) GeneratePDF(ctx context.Context, htmlDocument []byte, cfg Re ctx, cancel = chromedp.NewContext(allocCtx) defer cancel() - dataURL := fmt.Sprintf( - "data:text/html;base64,%s", - base64.StdEncoding.EncodeToString(htmlDocument), - ) - width, height := getPageDimensions(cfg.PageFormat, cfg.Orientation) marginTop := cfg.MarginTop.ToInches() @@ -166,23 +160,33 @@ func (c *Converter) GeneratePDF(ctx context.Context, htmlDocument []byte, cfg Re log.Bool("printBackground", cfg.PrintBackground), ) - err := chromedp.Run(ctx, - chromedp.Navigate(dataURL), + htmlContent := string(htmlDocument) + + err := chromedp.Run( + ctx, + chromedp.Navigate("about:blank"), + chromedp.ActionFunc(func(ctx context.Context) error { + frameTree, err := page.GetFrameTree().Do(ctx) + if err != nil { + return fmt.Errorf("cannot get frame tree: %w", err) + } + return page.SetDocumentContent(frameTree.Frame.ID, htmlContent).Do(ctx) + }), chromedp.WaitReady("body"), chromedp.ActionFunc(waitUntilDocumentReady), chromedp.ActionFunc( func(ctx context.Context) (err error) { pdfBytes, _, err = page.PrintToPDF(). - WithPrintBackground(cfg.PrintBackground). - WithPaperWidth(width). - WithPaperHeight(height). - WithMarginTop(marginTop). - WithMarginBottom(marginBottom). - WithMarginLeft(marginLeft). - WithMarginRight(marginRight). - WithScale(scale). - WithPreferCSSPageSize(false). - Do(ctx) + WithPrintBackground(cfg.PrintBackground). + WithPaperWidth(width). + WithPaperHeight(height). + WithMarginTop(marginTop). + WithMarginBottom(marginBottom). + WithMarginLeft(marginLeft). + WithMarginRight(marginRight). + WithScale(scale). + WithPreferCSSPageSize(false). + Do(ctx) return err },