pdf exports
This commit is contained in:
parent
565b379a98
commit
890a7d8b2c
4 changed files with 201 additions and 3 deletions
1
go.mod
generated
1
go.mod
generated
|
|
@ -33,6 +33,7 @@ require (
|
|||
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
|
||||
github.com/go-stack/stack v1.8.1 // indirect
|
||||
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||
github.com/jung-kurt/gofpdf v1.16.2 // indirect
|
||||
github.com/klauspost/compress v1.10.3 // indirect
|
||||
github.com/mattn/go-colorable v0.1.9 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
|
|
|
|||
9
go.sum
generated
9
go.sum
generated
|
|
@ -11,6 +11,7 @@ github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbf
|
|||
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
|
||||
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
|
||||
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
|
||||
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||
|
|
@ -96,6 +97,9 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
|
|||
github.com/h2non/filetype v1.1.1/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
|
||||
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
|
||||
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
|
||||
github.com/jung-kurt/gofpdf v1.16.2 h1:jgbatWHfRlPYiK85qgevsZTHviWXKwB1TTiKdz5PtRc=
|
||||
github.com/jung-kurt/gofpdf v1.16.2/go.mod h1:1hl7y57EsiPAkLbOwzpzqgx1A30nQCk/YmFV8S2vmK0=
|
||||
github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8=
|
||||
github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
|
|
@ -121,8 +125,10 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OH
|
|||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/phpdave11/gofpdi v1.0.7/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
|
||||
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU=
|
||||
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/playwright-community/playwright-go v0.2000.1 h1:2JViSHpJQ/UL/PO1Gg6gXV5IcXAAsoBJ3KG9L3wKXto=
|
||||
github.com/playwright-community/playwright-go v0.2000.1/go.mod h1:1y9cM9b9dVHnuRWzED1KLM7FtbwTJC8ibDjI6MNqewU=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
|
|
@ -130,11 +136,13 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
|||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
|
||||
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
|
||||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
|
|
@ -162,6 +170,7 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
|
|||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9 h1:yZNXmy+j/JpX19vZkVktWqAo7Gny4PBWYYK3zskGpx4=
|
||||
golang.org/x/exp v0.0.0-20221126150942-6ab00d035af9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
|
||||
golang.org/x/image v0.0.0-20190910094157-69e4b8554b2a/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/image v0.3.0 h1:HTDXbdK9bjfSWkPzDJIw89W8CAtfFGduujWs33NLLsg=
|
||||
golang.org/x/image v0.3.0/go.mod h1:fXd9211C/0VTlYuAcOhW8dY/RtEJqODXOWBDpmYBf+A=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
|
|
|
|||
98
lib/pdf/pdf.go
Normal file
98
lib/pdf/pdf.go
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
package pdf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math"
|
||||
"strings"
|
||||
|
||||
"github.com/jung-kurt/gofpdf"
|
||||
)
|
||||
|
||||
type GoFPDF struct {
|
||||
pdf *gofpdf.Fpdf
|
||||
}
|
||||
|
||||
func Init() *GoFPDF {
|
||||
newGofPDF := gofpdf.NewCustom(&gofpdf.InitType{
|
||||
UnitStr: "in",
|
||||
FontDirStr: "./d2renderers/d2fonts/ttf",
|
||||
})
|
||||
|
||||
newGofPDF.AddUTF8Font("source", "", "SourceSansPro-Regular.ttf")
|
||||
newGofPDF.AddUTF8Font("source", "B", "SourceSansPro-Bold.ttf")
|
||||
newGofPDF.SetAutoPageBreak(false, 0)
|
||||
newGofPDF.SetLineWidth(0.05)
|
||||
newGofPDF.SetMargins(0, 0, 0)
|
||||
|
||||
fpdf := GoFPDF{
|
||||
pdf: newGofPDF,
|
||||
}
|
||||
|
||||
return &fpdf
|
||||
}
|
||||
|
||||
func (g *GoFPDF) AddPDFPage(png []byte, boardPath []string) error {
|
||||
var opt gofpdf.ImageOptions
|
||||
opt.ImageType = "png"
|
||||
imageInfo := g.pdf.RegisterImageOptionsReader(strings.Join(boardPath, "/"), opt, bytes.NewReader(png))
|
||||
if g.pdf.Err() {
|
||||
return g.pdf.Error()
|
||||
}
|
||||
imageWidth := imageInfo.Width() / 2
|
||||
imageHeight := imageInfo.Height() / 2
|
||||
|
||||
// calculate page dimensions
|
||||
var pageWidth float64
|
||||
var pageHeight float64
|
||||
|
||||
g.pdf.SetFont("source", "B", 14)
|
||||
pathString := strings.Join(boardPath, " / ")
|
||||
headerMargin := 0.3
|
||||
headerWidth := g.pdf.GetStringWidth(pathString) + 2*headerMargin
|
||||
|
||||
minPageDimension := 6.0
|
||||
pageWidth = math.Max(math.Max(minPageDimension, imageWidth), headerWidth)
|
||||
pageHeight = math.Max(minPageDimension, imageHeight)
|
||||
|
||||
// Add page
|
||||
headerHeight := 0.75
|
||||
g.pdf.AddPageFormat("", gofpdf.SizeType{Wd: pageWidth, Ht: pageHeight + headerHeight})
|
||||
|
||||
// Draw header
|
||||
g.pdf.SetFillColor(255, 255, 255)
|
||||
g.pdf.Rect(0, 0, pageWidth, pageHeight, "F")
|
||||
g.pdf.SetTextColor(10, 15, 37) // steel-900
|
||||
g.pdf.SetFont("source", "", 14)
|
||||
|
||||
// Draw board path prefix
|
||||
var prefixWidth float64
|
||||
prefixPath := boardPath[:len(boardPath)-1]
|
||||
if len(prefixPath) > 0 {
|
||||
prefix := strings.Join(boardPath[:len(boardPath)-1], " / ") + " / "
|
||||
prefixWidth = g.pdf.GetStringWidth(prefix)
|
||||
|
||||
g.pdf.SetXY(headerMargin, 0)
|
||||
g.pdf.CellFormat(prefixWidth, headerHeight, prefix, "", 0, "", false, 0, "")
|
||||
}
|
||||
|
||||
// Draw board name
|
||||
boardName := boardPath[len(boardPath)-1]
|
||||
g.pdf.SetFont("source", "B", 14)
|
||||
g.pdf.SetXY(prefixWidth+headerMargin, 0)
|
||||
g.pdf.CellFormat(pageWidth-prefixWidth-headerMargin, headerHeight, boardName, "", 0, "", false, 0, "")
|
||||
|
||||
// Draw image
|
||||
g.pdf.ImageOptions(strings.Join(boardPath, "/"), (pageWidth-imageWidth)/2, headerHeight+(pageHeight-imageHeight)/2, imageWidth, imageHeight, false, opt, 0, "")
|
||||
|
||||
// Draw header/img seperator
|
||||
g.pdf.SetXY(headerMargin, headerHeight)
|
||||
g.pdf.SetLineWidth(0.01)
|
||||
g.pdf.SetDrawColor(10, 15, 37) // steel-900
|
||||
g.pdf.CellFormat(pageWidth-(headerMargin*2), 0.01, "", "T", 0, "", false, 0, "")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GoFPDF) Export(outputPath string) error {
|
||||
return g.pdf.OutputFileAndClose(outputPath)
|
||||
}
|
||||
96
main.go
96
main.go
|
|
@ -28,6 +28,7 @@ import (
|
|||
"oss.terrastruct.com/d2/d2themes/d2themescatalog"
|
||||
"oss.terrastruct.com/d2/lib/imgbundler"
|
||||
ctxlog "oss.terrastruct.com/d2/lib/log"
|
||||
pdflib "oss.terrastruct.com/d2/lib/pdf"
|
||||
"oss.terrastruct.com/d2/lib/png"
|
||||
"oss.terrastruct.com/d2/lib/textmeasure"
|
||||
"oss.terrastruct.com/d2/lib/version"
|
||||
|
|
@ -181,7 +182,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
|
|||
ms.Log.Debug.Printf("using layout plugin %s (%s)", *layoutFlag, plocation)
|
||||
|
||||
var pw png.Playwright
|
||||
if filepath.Ext(outputPath) == ".png" {
|
||||
if filepath.Ext(outputPath) == ".png" || filepath.Ext(outputPath) == ".pdf" {
|
||||
pw, err = png.InitPlaywright()
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -256,10 +257,17 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketc
|
|||
return nil, false, err
|
||||
}
|
||||
|
||||
svg, err := render(ctx, ms, plugin, sketch, pad, inputPath, outputPath, bundle, forceAppendix, page, ruler, diagram)
|
||||
var svg []byte
|
||||
if filepath.Ext(outputPath) == ".pdf" {
|
||||
svg, err = renderPDF(ctx, ms, plugin, sketch, pad, outputPath, page, ruler, diagram, nil, nil, false)
|
||||
} else {
|
||||
svg, err = render(ctx, ms, plugin, sketch, pad, inputPath, outputPath, bundle, forceAppendix, page, ruler, diagram)
|
||||
}
|
||||
if err != nil {
|
||||
return svg, false, err
|
||||
}
|
||||
|
||||
ms.Log.Success.Printf("successfully compiled %v to %v", inputPath, outputPath)
|
||||
return svg, true, nil
|
||||
}
|
||||
|
||||
|
|
@ -287,7 +295,6 @@ func render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketch
|
|||
if err != nil {
|
||||
return svg, err
|
||||
}
|
||||
ms.Log.Success.Printf("successfully compiled %v to %v", inputPath, outputPath)
|
||||
return svg, nil
|
||||
}
|
||||
|
||||
|
|
@ -349,6 +356,89 @@ func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketc
|
|||
return svg, nil
|
||||
}
|
||||
|
||||
func renderPDF(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketch bool, pad int64, outputPath string, page playwright.Page, ruler *textmeasure.Ruler, diagram *d2target.Diagram, pdf *pdflib.GoFPDF, boardPath []string, isScenario bool) ([]byte, error) {
|
||||
var isRoot bool
|
||||
if pdf == nil {
|
||||
pdf = pdflib.Init()
|
||||
isRoot = true
|
||||
}
|
||||
|
||||
var currBoardPath []string
|
||||
// is root board
|
||||
if diagram.Name == "" {
|
||||
ext := filepath.Ext(outputPath)
|
||||
trimmedPath := strings.TrimSuffix(outputPath, ext)
|
||||
splitPath := strings.Split(trimmedPath, "/")
|
||||
rootName := splitPath[len(splitPath)-1]
|
||||
currBoardPath = append(boardPath, rootName)
|
||||
} else {
|
||||
currBoardPath = append(boardPath, diagram.Name)
|
||||
}
|
||||
|
||||
var svg []byte
|
||||
if !isScenario {
|
||||
svg, err := d2svg.Render(diagram, &d2svg.RenderOpts{
|
||||
Pad: int(pad),
|
||||
Sketch: sketch,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
svg, err = plugin.PostProcess(ctx, svg)
|
||||
if err != nil {
|
||||
return svg, err
|
||||
}
|
||||
|
||||
svg, bundleErr := imgbundler.BundleLocal(ctx, ms, svg)
|
||||
svg, bundleErr2 := imgbundler.BundleRemote(ctx, ms, svg)
|
||||
bundleErr = multierr.Combine(bundleErr, bundleErr2)
|
||||
if bundleErr != nil {
|
||||
return svg, bundleErr
|
||||
}
|
||||
svg = appendix.Append(diagram, ruler, svg)
|
||||
|
||||
pngImg, err := png.ConvertSVG(ms, page, svg)
|
||||
if err != nil {
|
||||
return svg, err
|
||||
}
|
||||
|
||||
err = pdf.AddPDFPage(pngImg, currBoardPath)
|
||||
if err != nil {
|
||||
return svg, err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for _, dl := range diagram.Layers {
|
||||
_, err := renderPDF(ctx, ms, plugin, sketch, pad, "", page, ruler, dl, pdf, currBoardPath, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
for _, dl := range diagram.Scenarios {
|
||||
_, err := renderPDF(ctx, ms, plugin, sketch, pad, "", page, ruler, dl, pdf, currBoardPath, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
for _, dl := range diagram.Steps {
|
||||
_, err := renderPDF(ctx, ms, plugin, sketch, pad, "", page, ruler, dl, pdf, currBoardPath, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if isRoot {
|
||||
err := pdf.Export(outputPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return svg, nil
|
||||
}
|
||||
|
||||
func layerOutputPath(outputPath string, d *d2target.Diagram) string {
|
||||
if d.Name == "" {
|
||||
return outputPath
|
||||
|
|
|
|||
Loading…
Reference in a new issue