From 66083e2d98e23c2c369058dff530472b87734852 Mon Sep 17 00:00:00 2001 From: Bernard Xie Date: Mon, 27 Feb 2023 16:05:41 -0800 Subject: [PATCH] set root fill as transparent on pdf exports --- lib/color/color.go | 5 ++--- lib/pdf/pdf.go | 2 +- main.go | 7 ++++++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/color/color.go b/lib/color/color.go index df433b878..d9f18b03a 100644 --- a/lib/color/color.go +++ b/lib/color/color.go @@ -6,7 +6,6 @@ import ( "regexp" "strconv" - "github.com/davecgh/go-spew/spew" "github.com/lucasb-eyer/go-colorful" "github.com/mazznoer/csscolorparser" ) @@ -161,7 +160,6 @@ func (c *RGB) IsLight() bool { hsp := math.Sqrt(0.299*math.Pow(r, 2) + 0.587*math.Pow(g, 2) + 0.114*math.Pow(b, 2)) - spew.Dump(hsp) return hsp > 130 } @@ -170,9 +168,10 @@ func Hex2RGB(hex string) (RGB, error) { var rgb RGB if hex[0:1] == "#" { hex = hex[1:] + } else { + return RGB{}, nil } values, err := strconv.ParseUint(hex, 16, 32) - if err != nil { return RGB{}, err } diff --git a/lib/pdf/pdf.go b/lib/pdf/pdf.go index 5aed48d98..1a05ac8a3 100644 --- a/lib/pdf/pdf.go +++ b/lib/pdf/pdf.go @@ -34,7 +34,7 @@ func Init() *GoFPDF { } func (g *GoFPDF) GetFillRGB(fill string) (color.RGB, error) { - if fill == "" { + if fill == "" || strings.ToLower(fill) == "transparent" { return color.RGB{ Red: 255, Green: 255, diff --git a/main.go b/main.go index e566fb30c..05bb3a02d 100644 --- a/main.go +++ b/main.go @@ -458,6 +458,11 @@ func renderPDF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, ske } if !diagram.IsFolderOnly { + rootFill := diagram.Root.Fill + // gofpdf will print the png img with a slight filter + // strip out the background fill within the png so that the background is uniform in the exported pdf + diagram.Root.Fill = "transparent" + svg, err = d2svg.Render(diagram, &d2svg.RenderOpts{ Pad: int(pad), Sketch: sketch, @@ -484,7 +489,7 @@ func renderPDF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, ske return svg, err } - err = pdf.AddPDFPage(pngImg, currBoardPath, diagram.Root.Fill) + err = pdf.AddPDFPage(pngImg, currBoardPath, rootFill) if err != nil { return svg, err }