-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpdf.go
More file actions
411 lines (352 loc) · 11.4 KB
/
pdf.go
File metadata and controls
411 lines (352 loc) · 11.4 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package picoloom
import (
"context"
"fmt"
"html"
"io"
"os"
"strings"
"sync"
"time"
"github.com/alnah/picoloom/v2/internal/fileutil"
"github.com/alnah/picoloom/v2/internal/hints"
"github.com/alnah/picoloom/v2/internal/pipeline"
"github.com/alnah/picoloom/v2/internal/process"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
)
// browserCloseTimeout is the maximum time to wait for browser.Close() before force-killing.
const browserCloseTimeout = 5 * time.Second
// pdfConverter abstracts HTML to PDF conversion to allow different backends.
type pdfConverter interface {
ToPDF(ctx context.Context, htmlContent string, opts *pdfOptions) ([]byte, error)
Close() error
}
// pdfRenderer abstracts PDF rendering from an HTML file to enable testing without a browser.
type pdfRenderer interface {
RenderFromFile(ctx context.Context, filePath string, opts *pdfOptions) ([]byte, error)
}
// pdfOptions holds options for PDF generation.
type pdfOptions struct {
Footer *pipeline.FooterData
Page *PageSettings
}
// footerMarginExtra is added to bottom margin when footer is active.
const footerMarginExtra = 0.25
// Page dimensions in inches (ISO/ANSI standards).
const (
letterWidthInches = 8.5
letterHeightInches = 11.0
a4WidthInches = 8.27 // 210mm
a4HeightInches = 11.69 // 297mm
legalWidthInches = 8.5
legalHeightInches = 14.0
)
// Footer styling constants.
const (
footerFontSize = "10px"
footerColor = "#aaa"
footerPaddingH = "0.5in"
)
// pageDimensions maps page size to (width, height) in inches.
var pageDimensions = map[string]struct{ width, height float64 }{
PageSizeLetter: {letterWidthInches, letterHeightInches},
PageSizeA4: {a4WidthInches, a4HeightInches},
PageSizeLegal: {legalWidthInches, legalHeightInches},
}
// rodRenderer implements pdfRenderer using go-rod.
// Rod automatically downloads Chromium on first run if not found.
type rodRenderer struct {
browser *rod.Browser
launcher *launcher.Launcher
timeout time.Duration
closeOnce sync.Once
}
// newRodRenderer creates a rodRenderer with the given timeout.
func newRodRenderer(timeout time.Duration) *rodRenderer {
return &rodRenderer{timeout: timeout}
}
// ensureBrowser lazily connects to the browser.
// Uses rod's managed Chromium (~/.cache/rod/browser/) for complete isolation
// from the user's Chrome installation. This prevents corruption of Chrome.app
// state that would require a system restart to fix.
func (r *rodRenderer) ensureBrowser(ctx context.Context) error {
if r.browser != nil {
return nil
}
if err := ctx.Err(); err != nil {
return err
}
// Configure launcher
// Leakless(false) prevents hanging on macOS - see github.com/go-rod/rod/issues/210
// We compensate by explicitly calling Kill() and Cleanup() in Close().
l := launcher.New().Context(ctx).Headless(true).Leakless(false).Set("disable-gpu")
// Allow disabling sandbox for CI/Docker environments that lack kernel support.
if os.Getenv("ROD_NO_SANDBOX") == "1" {
l = l.NoSandbox(true)
}
// Optional: allow explicit browser override for CI/debugging.
// DO NOT auto-detect system Chrome - it causes corruption issues.
if bin := os.Getenv("ROD_BROWSER_BIN"); bin != "" {
l = l.Bin(bin)
}
u, err := l.Launch()
if err != nil {
if ctxErr := ctx.Err(); ctxErr != nil {
return ctxErr
}
return fmt.Errorf("%w: %w%s", ErrBrowserConnect, err, hints.ForBrowserConnect())
}
// Store launcher reference for cleanup in Close()
r.launcher = l
browser := rod.New().ControlURL(u).Context(ctx)
if err := browser.Connect(); err != nil {
r.launcher.Kill()
r.launcher.Cleanup()
r.browser = nil
r.launcher = nil
if ctxErr := ctx.Err(); ctxErr != nil {
return ctxErr
}
return fmt.Errorf("%w: %w%s", ErrBrowserConnect, err, hints.ForBrowserConnect())
}
// Keep a neutral context on the shared browser handle; operations use per-call contexts.
r.browser = browser.Context(context.Background())
return nil
}
// Close releases browser resources.
// Safe to call multiple times (idempotent via sync.Once).
// Uses a timeout to avoid hanging indefinitely if browser.Close() blocks.
func (r *rodRenderer) Close() error {
var closeErr error
r.closeOnce.Do(func() {
// Get PID before any cleanup - we'll need it to kill the process group
var pid int
if r.launcher != nil {
pid = r.launcher.PID()
}
// Try graceful close first with timeout
if r.browser != nil {
done := make(chan error, 1)
go func() {
done <- r.browser.Close()
}()
// Use NewTimer instead of time.After to avoid timer leak.
// time.After creates a timer that runs until expiration even if
// the select chooses another case, leaking memory temporarily.
timer := time.NewTimer(browserCloseTimeout)
select {
case closeErr = <-done:
// Browser closed normally - stop timer to prevent leak
timer.Stop()
case <-timer.C:
// Timeout - will be force-killed below
}
r.browser = nil
}
// Force kill the Chrome process group (kills all child processes too)
if pid > 0 {
// Kill the entire process group to ensure GPU, renderer,
// and other Chrome child processes are terminated.
process.KillProcessGroup(pid)
}
// Also call launcher.Kill() as fallback and cleanup user-data-dir
if r.launcher != nil {
r.launcher.Kill()
r.launcher.Cleanup()
r.launcher = nil
}
})
return closeErr
}
// RenderFromFile opens a local HTML file in headless Chrome and renders it to PDF.
// Returns explicit errors instead of panicking when browser operations fail.
func (r *rodRenderer) RenderFromFile(ctx context.Context, filePath string, opts *pdfOptions) ([]byte, error) {
// Check context before starting
if err := ctx.Err(); err != nil {
return nil, err
}
if err := r.ensureBrowser(ctx); err != nil {
return nil, err
}
page, err := r.browser.Page(proto.TargetCreateTarget{URL: "file://" + filePath})
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrPageCreate, err)
}
defer func() { _ = page.Close() }()
renderCtx, cancel, err := renderOperationContext(ctx, r.timeout)
if err != nil {
return nil, err
}
defer cancel()
pageWithCtx := page.Context(renderCtx)
if err := pageWithCtx.WaitLoad(); err != nil {
if ctxErr := ctx.Err(); ctxErr != nil {
return nil, ctxErr
}
return nil, fmt.Errorf("%w: %w%s", ErrPageLoad, err, hints.ForTimeout())
}
// Check context after page load
if err := ctx.Err(); err != nil {
return nil, err
}
// Build PDF options
pdfOpts := r.buildPDFOptions(opts)
// Generate PDF
reader, err := pageWithCtx.PDF(pdfOpts)
if err != nil {
if ctxErr := ctx.Err(); ctxErr != nil {
return nil, ctxErr
}
return nil, fmt.Errorf("%w: %w", ErrPDFGeneration, err)
}
defer func() { _ = reader.Close() }()
pdfBuf, err := io.ReadAll(reader)
if err != nil {
if ctxErr := ctx.Err(); ctxErr != nil {
return nil, ctxErr
}
return nil, fmt.Errorf("%w: reading PDF stream: %w", ErrPDFGeneration, err)
}
return pdfBuf, nil
}
// renderOperationContext keeps browser operations tied to caller cancellation
// so Ctrl+C can stop long-running page work instead of waiting for fallback timeouts.
func renderOperationContext(ctx context.Context, fallbackTimeout time.Duration) (context.Context, context.CancelFunc, error) {
if err := ctx.Err(); err != nil {
return nil, nil, err
}
if _, hasDeadline := ctx.Deadline(); hasDeadline || fallbackTimeout <= 0 {
return ctx, func() {}, nil
}
opCtx, cancel := context.WithTimeout(ctx, fallbackTimeout)
return opCtx, cancel, nil
}
// resolvePageDimensions returns width, height, margin, and bottom margin.
// Applies defaults for nil/zero values, swaps for landscape, adds footer space.
func resolvePageDimensions(page *PageSettings, hasFooter bool) (w, h, margin, bottomMargin float64) {
// Apply defaults
size := PageSizeLetter
orientation := OrientationPortrait
margin = DefaultMargin
if page != nil {
if page.Size != "" {
size = strings.ToLower(page.Size)
}
if page.Orientation != "" {
orientation = strings.ToLower(page.Orientation)
}
if page.Margin > 0 {
margin = page.Margin
}
}
// Get dimensions for size
dims, ok := pageDimensions[size]
if !ok {
dims = pageDimensions[PageSizeLetter] // fallback
}
w, h = dims.width, dims.height
// Swap for landscape
if orientation == OrientationLandscape {
w, h = h, w
}
// Bottom margin: add extra space for footer
bottomMargin = margin
if hasFooter {
bottomMargin = margin + footerMarginExtra
}
return w, h, margin, bottomMargin
}
// buildPDFOptions constructs proto.PagePrintToPDF with page settings and optional footer.
func (r *rodRenderer) buildPDFOptions(opts *pdfOptions) *proto.PagePrintToPDF {
hasFooter := opts != nil && opts.Footer != nil
var page *PageSettings
if opts != nil {
page = opts.Page
}
w, h, margin, bottomMargin := resolvePageDimensions(page, hasFooter)
pdfOpts := &proto.PagePrintToPDF{
PaperWidth: toFloatPtr(w),
PaperHeight: toFloatPtr(h),
MarginTop: toFloatPtr(margin),
MarginBottom: toFloatPtr(bottomMargin),
MarginLeft: toFloatPtr(margin),
MarginRight: toFloatPtr(margin),
PrintBackground: true,
}
if hasFooter {
pdfOpts.DisplayHeaderFooter = true
pdfOpts.HeaderTemplate = "<span></span>" // Empty header
pdfOpts.FooterTemplate = buildFooterTemplate(opts.Footer)
}
return pdfOpts
}
// buildFooterTemplate generates an HTML template for Chrome's native footer.
// Supports pageNumber, totalPages, date placeholders via CSS classes.
func buildFooterTemplate(data *pipeline.FooterData) string {
if data == nil {
return "<span></span>"
}
var parts []string
if data.ShowPageNumber {
parts = append(parts, `<span class="pageNumber"></span>/<span class="totalPages"></span>`)
}
if data.Date != "" {
parts = append(parts, html.EscapeString(data.Date))
}
if data.Status != "" {
parts = append(parts, html.EscapeString(data.Status))
}
if data.DocumentID != "" {
parts = append(parts, html.EscapeString(data.DocumentID))
}
if data.Text != "" {
parts = append(parts, html.EscapeString(data.Text))
}
if len(parts) == 0 {
return "<span></span>"
}
content := strings.Join(parts, " - ")
// Position: left, center, or right (default)
textAlign := "right"
switch data.Position {
case "left":
textAlign = "left"
case "center":
textAlign = "center"
}
return fmt.Sprintf(`<div style="font-size: %s; font-family: %s; color: %s; width: 100%%; text-align: %s; padding: 0 %s;">%s</div>`,
footerFontSize, defaultFontFamily, footerColor, textAlign, footerPaddingH, content)
}
// toFloatPtr returns a pointer to a float64 value.
func toFloatPtr(v float64) *float64 {
return &v
}
// rodConverter converts HTML to PDF using headless Chrome via go-rod.
type rodConverter struct {
renderer *rodRenderer
}
// newRodConverter creates a rodConverter with production renderer.
func newRodConverter(timeout time.Duration) *rodConverter {
return &rodConverter{
renderer: newRodRenderer(timeout),
}
}
// ToPDF converts HTML content to PDF bytes using headless Chrome.
// Page dimensions are configured via opts.Page (defaults to US Letter, portrait, 0.5in margins).
func (c *rodConverter) ToPDF(ctx context.Context, htmlContent string, opts *pdfOptions) ([]byte, error) {
tmpPath, cleanup, err := fileutil.WriteTempFile(htmlContent, "html")
if err != nil {
return nil, err
}
defer cleanup()
return c.renderer.RenderFromFile(ctx, tmpPath, opts)
}
// Close releases browser resources.
func (c *rodConverter) Close() error {
if c.renderer != nil {
return c.renderer.Close()
}
return nil
}