From 5fb35c2d48043b904080b211d2cf8a293472e647 Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Tue, 21 Sep 2021 18:51:52 +0200 Subject: [PATCH] fix: chromium exact colors (#361) --- pkg/modules/chromium/chromium.go | 25 +++++++++++++++++++ pkg/modules/chromium/chromium_test.go | 3 +++ .../testdata/chromium/html/sample1/index.html | 2 +- .../testdata/chromium/html/sample7/index.html | 14 +++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/testdata/chromium/html/sample7/index.html diff --git a/pkg/modules/chromium/chromium.go b/pkg/modules/chromium/chromium.go index 57089ce2..b72f232e 100644 --- a/pkg/modules/chromium/chromium.go +++ b/pkg/modules/chromium/chromium.go @@ -363,6 +363,31 @@ func (mod Chromium) PDF(ctx context.Context, logger *zap.Logger, URL, outputPath return fmt.Errorf("wait for events: %w", err) }), + chromedp.ActionFunc(func(ctx context.Context) error { + // See: + // https://github.com/gotenberg/gotenberg/issues/354 + // https://github.com/puppeteer/puppeteer/issues/2685 + // https://github.com/chromedp/chromedp/issues/520 + script := ` +(() => { + const css = 'html { -webkit-print-color-adjust: exact !important; }'; + + const style = document.createElement('style'); + style.type = 'text/css'; + style.appendChild(document.createTextNode(css)); + document.head.appendChild(style); +})(); +` + + evaluate := chromedp.Evaluate(script, nil) + err := evaluate.Do(ctx) + + if err == nil { + return nil + } + + return fmt.Errorf("add CSS for exact colors: %w", err) + }), chromedp.ActionFunc(func(ctx context.Context) error { if options.WaitDelay > 0 { // We wait for a given amount of time so that JavaScript diff --git a/pkg/modules/chromium/chromium_test.go b/pkg/modules/chromium/chromium_test.go index a0d22855..f9376a11 100644 --- a/pkg/modules/chromium/chromium_test.go +++ b/pkg/modules/chromium/chromium_test.go @@ -321,6 +321,9 @@ func TestChromium_PDF(t *testing.T) { URL: "file:///tests/test/testdata/chromium/html/sample6/index.html", allowFileAccessFromFiles: true, }, + { + URL: "file:///tests/test/testdata/chromium/html/sample7/index.html", + }, } { func() { mod := new(Chromium) diff --git a/test/testdata/chromium/html/sample1/index.html b/test/testdata/chromium/html/sample1/index.html index 8c9bbd51..634d44e8 100644 --- a/test/testdata/chromium/html/sample1/index.html +++ b/test/testdata/chromium/html/sample1/index.html @@ -32,7 +32,7 @@

This image is loaded from a URL

- +
\ No newline at end of file diff --git a/test/testdata/chromium/html/sample7/index.html b/test/testdata/chromium/html/sample7/index.html new file mode 100644 index 00000000..a3c77d91 --- /dev/null +++ b/test/testdata/chromium/html/sample7/index.html @@ -0,0 +1,14 @@ + + + + + Gutenberg + + +

Gutenberg

+

+ It is a press, certainly, but a press from which shall flow in inexhaustible streams...Through it, God will spread His Word. A spring of truth shall flow from it: like a new star it shall scatter the darkness of ignorance, and cause a light heretofore unknown to shine amongst men. +

+ + + \ No newline at end of file