forked from dreddsa5dies/automateGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
49 lines (42 loc) · 907 Bytes
/
main.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
// чтение и запись PDF
package main
import (
"bytes"
"fmt"
"github.com/jung-kurt/gofpdf"
"github.com/ledongthuc/pdf"
)
func main() {
// чтение PDF
content, err := readPdf("basic.pdf") // Read local pdf file
if err != nil {
panic(err)
}
fmt.Println(content)
// создание и запись PDF
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "B", 16)
pdf.Cell(40, 10, content)
err = pdf.OutputFileAndClose("hello.pdf")
return
}
func readPdf(path string) (string, error) {
r, err := pdf.Open(path)
if err != nil {
return "", err
}
totalPage := r.NumPage()
var textBuilder bytes.Buffer
for pageIndex := 1; pageIndex <= totalPage; pageIndex++ {
p := r.Page(pageIndex)
if p.V.IsNull() {
continue
}
t := p.Content().Text
for i := range t {
textBuilder.WriteString(t[i].S)
}
}
return textBuilder.String(), nil
}