Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

BE PDFs - target 300dpi for rendered visualizations (#81078)

Nick Fitzpatrick3e83533d0675
diff --git a/src/metabase/channel/render/card.clj b/src/metabase/channel/render/card.clj
index 10c92200f5b..12f50ed06ed 100644
--- a/src/metabase/channel/render/card.clj
+++ b/src/metabase/channel/render/card.clj
@@ -359,10 +359,13 @@
image-bundle/render-img-data-uri))
(mu/defn png-from-render-info :- bytes?
- "Create a PNG file (as a byte array) from rendering info."
- ^bytes [rendered-info :- ::body/RenderedPartCard width]
- ;; TODO huh? why do we need this indirection?
- (png/render-html-to-png rendered-info width))
+ "Create a PNG file (as a byte array) from rendering info. `options` takes the keys
+ [[metabase.channel.render.png/render-html-to-png]] reads, e.g. `:channel.render/scale`."
+ (^bytes [rendered-info :- ::body/RenderedPartCard width]
+ ;; TODO huh? why do we need this indirection?
+ (png/render-html-to-png rendered-info width))
+ (^bytes [rendered-info :- ::body/RenderedPartCard width options]
+ (png/render-html-to-png rendered-info width options)))
(mu/defn defaulted-timezone :- :string
"Returns the timezone ID for the given `card`. Either the report timezone (if applicable) or the JVM timezone."
diff --git a/src/metabase/channel/render/pdf.clj b/src/metabase/channel/render/pdf.clj
index b6baebaa722..2c53f9cffe9 100644
--- a/src/metabase/channel/render/pdf.clj
+++ b/src/metabase/channel/render/pdf.clj
@@ -560,18 +560,6 @@
;; Drawing
;; --------------------------------------------------------------------------------------------
-(defn- card-body-png
- "Rasterize a `:card` part's body (chart/table/scalar) to PNG bytes, WITHOUT its title or description -- those are
- drawn natively at the PDF level.
-
- Used for card types that can't fill an arbitrary box (gauge, funnel, progress, scalar, table, ...); resize
- to fit while preserving aspect."
- ^bytes [timezone {:keys [card dashcard result]} px-width]
- (-> (render.card/render-pulse-card :inline timezone card dashcard result
- {:channel.render/include-title? false
- :channel.render/include-description? false})
- (render.card/png-from-render-info px-width)))
-
(defn- draw-image-in-cell!
"Draw `img`, resizing to fit (preserving aspect) within a cell rectangle, horizontally centered and top-anchored.
Used for card types that can't fill an arbitrary box (scalar, gauge, ...). Horizontal centering keeps a narrow
@@ -591,6 +579,39 @@
(* (double px)
(/ 72.0 common/dpi)))
+(def ^:private body-target-dpi
+ "Device DPI a fixed-size card body aims for: the same a rectangular chart already gets, i.e. [[common/dpi]]
+ laid out and rasterized at [[common/chart-supersample]]."
+ (* common/dpi common/chart-supersample))
+
+(defn- fixed-body-scale
+ "Supersample factor that lands an `iw` x `ih` (logical px) card body at [[body-target-dpi]] once
+ [[draw-image-in-cell!]] has fit it into the `cell-w` x `cell-h` point box.
+
+ The fit cancels the factor out -- `min(cell-w/iw, cell-h/ih)` scales both terms -- so the drawn width
+ follows from the 1:1 dims, and with it the factor. No cap needed: it can't exceed `cell-w`."
+ [cell-w cell-h iw ih]
+ (if (or (zero? iw) (zero? ih))
+ 1.0
+ (let [drawn-w-pt (min (double cell-w) (* iw (/ (double cell-h) ih)))
+ target-px (/ (* drawn-w-pt body-target-dpi) 72.0)]
+ (max 1.0 (/ target-px iw)))))
+
+(defn- card-body-png
+ "Rasterize a `:card` part's body (chart/table/scalar) to PNG bytes, WITHOUT its title or description -- those
+ are drawn natively at the PDF level.
+
+ Used for card types that can't fill an arbitrary box (gauge, funnel, progress, scalar, table, ...), which
+ [[draw-image-in-cell!]] fits into the `cell-w` x `cell-h` point box preserving aspect. They lay out at their
+ intrinsic size, so they're supersampled by the magnification that fit will apply -- see [[fixed-body-scale]]."
+ ^bytes [timezone {:keys [card dashcard result]} cell-w cell-h]
+ (-> (render.card/render-pulse-card :inline timezone card dashcard result
+ {:channel.render/include-title? false
+ :channel.render/include-description? false})
+ ;; the px width is CSSBox's layout viewport, not the output size -- the scale fn decides that
+ (render.card/png-from-render-info (-> cell-w pt->px (min 2200) (max 240) long)
+ {:channel.render/scale (partial fixed-body-scale cell-w cell-h)})))
+
(defn- effective-display
"The display type the card actually renders as. A visualizer dashcard overrides the underlying card's `:display`
(e.g. a smartscalar card shown as a `bar`), so prefer the visualizer display when present. This is the same
@@ -1153,7 +1174,7 @@
;; other types (and charts whose render produced no SVG): title-less body PNG,
;; fit preserving aspect
:else
- (when-let [body (card-body-png timezone part (-> cell-w pt->px (min 2200) (max 240) long))]
+ (when-let [body (card-body-png timezone part cell-w body-h)]
(draw-image-in-cell! cs (PDImageXObject/createFromByteArray doc body "card")
x body-top cell-w body-h)))))))
diff --git a/src/metabase/channel/render/png.clj b/src/metabase/channel/render/png.clj
index 3de8f847872..3c4251d69a2 100644
--- a/src/metabase/channel/render/png.clj
+++ b/src/metabase/channel/render/png.clj
@@ -50,12 +50,19 @@
^long [px scale]
(long (Math/ceil (* (double px) (double scale)))))
+(defn- painted-px
+ "`px` scaled by `scale`, rounded *down* to a whole pixel (at least one) -- the canvas must not reach past
+ where the scaled graphics paints. Rounding up leaves a fractional `scale`'s last row/column untouched, and
+ that transparent edge fringes grey in viewers that resample without premultiplying (poppler does)."
+ ^long [px scale]
+ (max 1 (long (Math/floor (* (double px) (double scale))))))
+
(defn- redraw-at-scale!
"Re-render the already-laid-out boxes of `graphics-engine` into a fresh image `scale`x the device size
of `base`, for crisp supersampled output. Returns the new [[BufferedImage]]."
^java.awt.image.BufferedImage [^GraphicsEngine graphics-engine ^java.awt.image.BufferedImage base scale]
- (let [big (BufferedImage. (scale-px (.getWidth base) scale)
- (scale-px (.getHeight base) scale)
+ (let [big (BufferedImage. (painted-px (.getWidth base) scale)
+ (painted-px (.getHeight base) scale)
BufferedImage/TYPE_INT_ARGB)]
(.setImage graphics-engine big)
(.redrawBoxes graphics-engine)
@@ -63,21 +70,27 @@
(defn- render-to-png
"Render `html` to a [[BufferedImage]] at `width` logical pixels. `scale` > 1 rasterizes to that many
- device pixels for crispness (via scaled device graphics, since CSSBox ignores CSS `zoom`/`transform`)."
+ device pixels for crispness (via scaled device graphics, since CSSBox ignores CSS `zoom`/`transform`).
+
+ `scale` may instead be a function of the laid-out content's width and height in logical px, returning a
+ positive factor -- for callers that can only choose one once they know how big the content turned out."
(^java.awt.image.BufferedImage [^String html width]
(render-to-png html width 1.0))
(^java.awt.image.BufferedImage [^String html width scale]
(style/register-fonts-if-needed!)
(with-open [is (ByteArrayInputStream. (.getBytes html StandardCharsets/UTF_8))
doc-source (StreamDocumentSource. is nil "text/html; charset=utf-8")]
- (let [scale (double scale)
+ ;; `setupGraphics` runs for the measuring layout and again for the redraw, but a function `scale` can't be
+ ;; resolved until the first has happened -- so the factor it applies lives here, set just before the redraw.
+ (let [device-scale (volatile! 1.0)
dimension (Dimension. width 1)
doc (.parse (DefaultDOMSource. doc-source))
da (dom-analyzer doc doc-source dimension)
graphics-engine (proxy [GraphicsEngine] [(.getRoot da) da (.getURL doc-source)]
(setupGraphics [^Graphics2D g]
- (when (not= 1.0 scale)
- (.scale g scale scale))
+ (let [s (double @device-scale)]
+ (when (not= 1.0 s)
+ (.scale g s s)))
(doto g
(.setRenderingHint RenderingHints/KEY_RENDERING
RenderingHints/VALUE_RENDER_QUALITY)
@@ -93,9 +106,14 @@
;; CSSBox voodoo -- sometimes maximal width < minimal width, no idea why
content-width (max (int (.getMinimalWidth viewport))
(int (.getMaximalWidth viewport)))
+ ;; a function `scale` sees the dims a 1:1 render would return: cropped width, full height
+ scale (if (ifn? scale)
+ (double (scale (min content-width (.getWidth base)) (.getHeight base)))
+ (double scale))
image (if (= 1.0 scale)
base
- (redraw-at-scale! graphics-engine base scale))
+ (do (vreset! device-scale scale)
+ (redraw-at-scale! graphics-engine base scale)))
crop-width (scale-px content-width scale)]
;; Crop the image to the actual size of the rendered content so that tables don't have a ton of whitespace.
(if (< crop-width (.getWidth image))
diff --git a/test/metabase/channel/render/pdf_test.clj b/test/metabase/channel/render/pdf_test.clj
index 8f4fa16c869..e4e2865b530 100644
--- a/test/metabase/channel/render/pdf_test.clj
+++ b/test/metabase/channel/render/pdf_test.clj
@@ -848,6 +848,46 @@
:rows [["x" "y"]]}}}]
(is (fills-cell-width? (width fallback 1000) 1000))))))
+(deftest ^:parallel fixed-body-scale-test
+ (let [scale-for @#'pdf/fixed-body-scale
+ cell-w 261.6
+ cell-h 180.0]
+ (testing "a body far smaller than its cell is scaled up to reach the target DPI"
+ ;; a 93x64 scalar drawn into this cell is width-bound, so it needs ~11x to hit 300 DPI
+ (is (< 11.0 (scale-for cell-w cell-h 93 64) 12.5)))
+ (testing "a body already big enough is never scaled down"
+ (is (= 1.0 (scale-for cell-w cell-h 4000 200))))
+ (testing "a degenerate body doesn't divide by zero"
+ (is (= 1.0 (scale-for cell-w cell-h 0 0)))
+ (is (= 1.0 (scale-for cell-w cell-h 10 0))))
+ (testing "the raster is bounded by the cell at the target DPI, so no cap is needed"
+ (doseq [[iw ih] [[93 64] [239 110] [10 10] [500 20] [1 300]]]
+ (let [device-w (* iw (scale-for cell-w cell-h iw ih))
+ ceiling (* cell-w (/ (double @#'pdf/body-target-dpi) 72.0))]
+ (is (<= device-w (inc ceiling))
+ (format "a %dx%d body must not rasterize wider than its cell at the target DPI" iw ih)))))))
+
+;; not ^:parallel: exercises the real CSSBox HTML rendering path
+(deftest ^:synchronized card-body-png-target-dpi-test
+ (testing "a fixed-size card body rasterizes at the target DPI for its cell, not at its intrinsic size"
+ (let [cell-w 261.6
+ cell-h 180.0]
+ (doseq [[display data] [[:scalar {:cols [{:name "n" :display_name "N" :base_type :type/Integer}]
+ :rows [[1004]]}]
+ [:smartscalar {:cols [{:name "d" :display_name "D" :base_type :type/Date
+ :unit :month}
+ {:name "n" :display_name "N" :base_type :type/Integer}]
+ :rows [["2026-01-01" 4] ["2026-02-01" 19]]}]]]
+ (testing display
+ (let [part {:card {:display display :name "Total"} :dashcard nil :result {:data data}}
+ ^BufferedImage img (ImageIO/read (io/input-stream (#'pdf/card-body-png nil part cell-w cell-h)))
+ w (.getWidth img)
+ h (.getHeight img)
+ ;; what draw-image-in-cell! will fit it to -- independent of the supersample factor
+ drawn-w-pt (min cell-w (* w (/ (double cell-h) h)))
+ ppi (/ w (/ drawn-w-pt 72.0))]
+ (is (<= 290.0 ppi 310.0)
+ (format "%s body is %dx%d, drawn %.1fpt wide => %.0f PPI" display w h drawn-w-pt ppi))))))))
(deftest ^:synchronized table-body-png-dispatches-on-chart-type-test
(testing "table-body-png renders each table-like card with its own renderer, not always the flat table"
(let [data {:cols [{:name "n" :display_name "N" :base_type :type/Integer}]
diff --git a/test/metabase/channel/render/png_test.clj b/test/metabase/channel/render/png_test.clj
index 10b60e1fcc4..8001484adb8 100644
--- a/test/metabase/channel/render/png_test.clj
+++ b/test/metabase/channel/render/png_test.clj
@@ -93,3 +93,39 @@
^BufferedImage png2x (render-with-wrapping content 200 {:channel.render/scale 2.0})]
(is (= (* 2 (.getWidth png1x)) (.getWidth png2x)))
(is (= (* 2 (.getHeight png1x)) (.getHeight png2x))))))
+
+(deftest render-html-to-png-scale-fn-test
+ (testing "a function :channel.render/scale is handed the laid-out content size and its factor applied"
+ (let [content [:div {:style "width: 100px; height: 40px;"} "hello"]
+ ^BufferedImage png1x (render-with-wrapping content 200)
+ seen (atom nil)
+ ^BufferedImage png3x (render-with-wrapping content 200
+ {:channel.render/scale (fn [w h]
+ (reset! seen [w h])
+ 3.0)})]
+ (testing "it sees the dimensions a 1:1 render would have produced"
+ (is (= [(.getWidth png1x) (.getHeight png1x)] @seen)))
+ (testing "and the factor it returns is what the raster is scaled by"
+ (is (= (* 3 (.getWidth png1x)) (.getWidth png3x)))
+ (is (= (* 3 (.getHeight png1x)) (.getHeight png3x)))))))
+
+(defn- edge-alphas
+ "The set of alpha values along `img`'s last column and last row."
+ [^BufferedImage img]
+ (let [w (.getWidth img)
+ h (.getHeight img)
+ alpha (fn [x y] (bit-and (bit-shift-right (.getRGB img x y) 24) 0xFF))]
+ (into (sorted-set)
+ (concat (map #(alpha (dec w) %) (range h))
+ (map #(alpha % (dec h)) (range w))))))
+
+(deftest render-html-to-png-fractional-scale-edge-test
+ (testing "a fractional scale leaves no unpainted edge -- the canvas never reaches past what CSSBox paints"
+ ;; A canvas rounded *up* to a whole pixel leaves its last row/column transparent, which viewers that
+ ;; resample without premultiplying (poppler) fringe grey. Only fractional factors hit this; an integer
+ ;; one lands the paint exactly on the canvas edge.
+ (let [content [:div {:style "width: 137px; height: 43px;"} "hello"]]
+ (doseq [factor [1.4667844117506315 1.5964107160215022 2.000303023900741 3.9005602730645073]]
+ (testing (str "scale " factor)
+ (is (= #{255}
+ (edge-alphas (render-with-wrapping content 233 {:channel.render/scale factor})))))))))