forked from tuupke/cuproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf.go
More file actions
139 lines (114 loc) · 3.16 KB
/
Copy pathpdf.go
File metadata and controls
139 lines (114 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"slices"
"strings"
"github.com/jung-kurt/gofpdf"
"github.com/rs/zerolog"
"github.com/tuupke/utils/env"
)
var (
pdfUnit = env.String("PDF_UNIT", "mm")
pdfSize = env.String("PDF_PAGE_SIZE", "A4")
pdfFontDir = env.String("PDF_FONT_DIR", "")
pdfInLandscape = env.Bool("PDF_LANDSCAPE", false)
pdfLeftMargin = env.Float("PDF_LEFT_MARGIN", 10)
pdfTopMargin = env.Float("PDF_TOP_MARGIN", 10)
fontSize = env.Float("PDF_FONT_SIZE", 12)
pdfLineHeight = pointsToUnits(fontSize) * env.Float("PDF_LINE_HEIGHT", 1.2)
font = env.String("PDF_FONT_FAMILY", "Arial")
imgDpi = float64(env.Int("IMAGE_PPI", 120))
)
func pointsToUnits(points float64) float64 {
switch pdfUnit {
case "mm", "":
return points * 0.352778
case "cm":
return points * 0.0352778
case "pt":
return points
case "in":
return points * 0.0138889
}
panic("Unknown pdf unit: " + pdfUnit)
}
func BannerPage(log zerolog.Logger, outWrite io.Writer, data *Props, keys ...string) error {
if len(keys) == 1 && keys[0] == "*" {
keys = make([]string, 0, 100)
data.Range(func(key, _ string) bool {
keys = append(keys, key)
return true
})
slices.Sort(keys)
}
orientation := "P"
if pdfInLandscape {
orientation = "L"
}
log.Info().Bool("landscape", pdfInLandscape).Int("num_keys", len(keys)).Msg("rendering new banner")
pdf := gofpdf.New(orientation, pdfUnit, pdfSize, pdfFontDir)
if bannerOnBack {
pdf.AddPage()
}
pdf.AddPage()
pdf.SetFont(font, "", fontSize)
yTop := pdfTopMargin
for _, k := range keys {
val, ok := data.Load(k)
if !ok {
continue
}
if slices.Contains(imageKeys, k) && val != "" {
if err := renderImage(log, pdf, k, val, &yTop); err != nil {
log.Err(err).Str("key", k).Str("value", val).Msg("could not render image, skipping")
}
continue
}
pdf.Text(pdfLeftMargin, yTop+pdfLineHeight, fmt.Sprintf("%v: %v", k, val))
yTop += pdfLineHeight
}
return pdf.Output(outWrite)
}
func renderImage(log zerolog.Logger, pdf *gofpdf.Fpdf, key, path string, yTop *float64) error {
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(path), "."))
if ext == "jpeg" {
ext = "jpg"
}
if ext != "jpg" && ext != "png" && ext != "gif" {
return fmt.Errorf("unsupported image extension %q for %q", ext, path)
}
image, err := os.Open(path)
if err != nil {
return fmt.Errorf("open image %q: %w", path, err)
}
defer image.Close()
iopts := gofpdf.ImageOptions{ReadDpi: true, ImageType: ext}
info := pdf.RegisterImageOptionsReader(key, iopts, image)
if info == nil {
if pdfErr := pdf.Error(); pdfErr != nil {
return fmt.Errorf("gofpdf rejected image %q: %w", path, pdfErr)
}
return fmt.Errorf("gofpdf returned nil image info for %q", path)
}
info.SetDpi(imgDpi)
pageW, _ := pdf.GetPageSize()
maxW := pageW - 2*pdfLeftMargin
w, h := info.Width(), info.Height()
if w > maxW {
h = h * maxW / w
w = maxW
}
pdf.ImageOptions(key, pdfLeftMargin, *yTop, w, h, true, iopts, 0, "")
*yTop += h + pdfLineHeight
log.Debug().
Str("key", key).
Str("path", path).
Float64("drawn_w", w).
Float64("drawn_h", h).
Float64("y", *yTop).
Msg("rendered image")
return nil
}