-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf.go
159 lines (142 loc) · 3.3 KB
/
pdf.go
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
package main
import (
"bytes"
"context"
"crypto/sha256"
_ "embed"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
)
const maxOutputSize = 100 * 1024 * 1024 // 100MB
var (
gsExe = "gs"
//go:embed emptypage.pdf
emptyPage []byte
)
// PDF is a handle for a pdf file
type PDF struct {
path string
data []byte
}
func newPDF(p string) (PDF, error) {
var pdf PDF
data, err := os.ReadFile(p)
if err != nil {
return pdf, err
}
pdf.path = p
pdf.data = data
return pdf, nil
}
func (p PDF) Path() string {
return p.path
}
func (p PDF) Data() io.Reader {
return bytes.NewBuffer(p.data)
}
// FullText uses ghostscript to extract the full text of the pdf
func (p PDF) FullText(ctx context.Context) ([]byte, error) {
args := []string{
"-dNOPAUSE",
"-dBATCH",
"-dSAFER",
"-dQUIET",
"-sDEVICE=txtwrite",
"-sOutputFile=-",
"-",
}
cmd := exec.CommandContext(ctx, gsExe, args...)
cmd.Stdin = p.Data()
b := newBoundedBuffer(maxOutputSize)
cmd.Stdout = b
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("failed to get full text of %q: %w", p.Path(), err)
}
if err := cmd.Wait(); err != nil {
return nil, fmt.Errorf("failed to get full text of %q: %w", p.Path(), err)
}
if !b.filled {
return b.buf.Bytes(), nil
}
return nil, nil
}
// FullText uses ghostscript to extract the cover of the pdf
func (p PDF) Cover(ctx context.Context) ([]byte, error) {
args := []string{
"-dNOPAUSE",
"-dBATCH",
"-dSAFER",
"-dQUIET",
"-sDEVICE=pdfwrite",
"-sOutputFile=-",
"-dFirstPage=1",
"-dLastPage=1",
"-",
}
cmd := exec.CommandContext(ctx, gsExe, args...)
cmd.Stdin = p.Data()
b := newBoundedBuffer(maxOutputSize)
cmd.Stdout = b
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("failed to get cover of %q: %w", p.Path(), err)
}
if err := cmd.Wait(); err != nil {
return nil, fmt.Errorf("failed to get cover of %q: %w", p.Path(), err)
}
if !b.filled {
return b.buf.Bytes(), nil
}
return emptyPage, nil
}
// Pages uses ghostscript to count the pages of the pdf
func (p PDF) Pages(ctx context.Context) (int, error) {
args := []string{
"-dNOPAUSE",
"-dBATCH",
"-dSAFER",
"-dQUIET",
"-dNODISPLAY",
fmt.Sprintf(`--permit-file-read=%s`, p.Path()),
"-c",
fmt.Sprintf(`(%s) (r) file runpdfbegin pdfpagecount = quit`, p.Path()),
}
cmd := exec.CommandContext(ctx, gsExe, args...)
cmd.Stdin = p.Data()
data, err := cmd.Output()
if err != nil {
return 0, fmt.Errorf("failed to get pages of %q: %w", p.Path(), err)
}
n, err := strconv.Atoi(strings.TrimSpace(string(data)))
if err != nil {
return 0, fmt.Errorf("failed to get pages of %q: %w", p.Path(), err)
}
return n, nil
}
// Sig returns a SHA256 hash of the pdf, useful to find duplicates in the index
func (p PDF) Sig() (string, error) {
h := sha256.New()
if _, err := io.Copy(h, p.Data()); err != nil {
return "", fmt.Errorf("failed to build signature of %q: %w", p.Path(), err)
}
return fmt.Sprintf("%0x", h.Sum(nil)), nil
}
type boundedBuffer struct {
buf bytes.Buffer
limit int
filled bool
}
func newBoundedBuffer(n int) *boundedBuffer {
return &boundedBuffer{limit: n}
}
func (b *boundedBuffer) Write(p []byte) (n int, err error) {
if remain := b.limit - b.buf.Len(); len(p) <= remain {
return b.buf.Write(p)
}
// don't care if we are near the limit
b.filled = true
return len(p), nil
}