Repository navigation
Expand file tree
/
Copy pathengine.go
More file actions
59 lines (49 loc) · 1.53 KB
/
Copy pathengine.go
File metadata and controls
59 lines (49 loc) · 1.53 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
// Copyright 2026 Anna Veretennykova
//
// SPDX-License-Identifier: MIT
package pdfengine
import (
"context"
"fmt"
"github.com/annavetech/pdfengine/internal/engine"
)
// Engine converts documents to PDF. Create one with New and reuse it across
// conversions; it is safe for concurrent use and holds no per-conversion state.
type Engine struct {
pipeline *engine.Pipeline
}
// New builds an Engine from the built-in default configuration, applying
// any Option given, and returns an error if a config file fails to load.
func New(opts ...Option) (*Engine, error) {
o := &options{}
for _, opt := range opts {
opt(o)
}
var configOpts []engine.ConfigOption
if o.stylePath != "" {
configOpts = append(configOpts, engine.WithStyleFile(o.stylePath))
}
if o.limitsPath != "" {
configOpts = append(configOpts, engine.WithLimitsFile(o.limitsPath))
}
if o.style != nil {
configOpts = append(configOpts, engine.WithStyleOverride(toStyleOverride(*o.style)))
}
cfg, err := engine.LoadConfig(configOpts...)
if err != nil {
return nil, fmt.Errorf("pdfengine: %w", err)
}
return &Engine{pipeline: engine.NewPipeline(cfg)}, nil
}
// Convert renders data as f and returns the resulting PDF bytes. Pass
// FormatAuto to detect the format from the content instead of naming it.
func (e *Engine) Convert(ctx context.Context, data []byte, f Format) ([]byte, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
pdf, err := e.pipeline.Run(string(data), f.toInternal())
if err != nil {
return nil, translateError(err)
}
return pdf, nil
}