-
Notifications
You must be signed in to change notification settings - Fork 48
/
genPdf.go
73 lines (61 loc) · 1.42 KB
/
genPdf.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
package utils
import (
"bytes"
"fmt"
"os"
"runtime"
"github.com/SebastiaanKlippert/go-wkhtmltopdf"
)
type PdfOption struct {
FileName string
CoverPath string
PageSize string
Toc bool
}
func (p *PdfOption) GenPdf(buf *bytes.Buffer) (err error) {
wkhtmltopdf.SetPath(WkToPdfDir)
pdfg, _ := wkhtmltopdf.NewPDFGenerator()
page := wkhtmltopdf.NewPageReader(buf)
page.FooterFontSize.Set(10)
page.FooterRight.Set("[page]")
page.DisableSmartShrinking.Set(true)
page.EnableLocalFileAccess.Set(true)
pdfg.AddPage(page)
if p.CoverPath != "" {
pdfg.Cover.EnableLocalFileAccess.Set(true)
if runtime.GOOS == "windows" {
pdfg.Cover.Input = p.CoverPath
} else {
pdfg.Cover.Input = "file://" + p.CoverPath
}
}
pdfg.Dpi.Set(300)
if p.Toc {
pdfg.TOC.Include = true
pdfg.TOC.TocHeaderText.Set("目 录")
pdfg.TOC.HeaderFontSize.Set(18)
pdfg.TOC.TocLevelIndentation.Set(15)
pdfg.TOC.TocTextSizeShrink.Set(0.9)
pdfg.TOC.DisableDottedLines.Set(false)
pdfg.TOC.EnableTocBackLinks.Set(true)
}
pdfg.PageSize.Set(wkhtmltopdf.PageSizeA4)
pdfg.MarginTop.Set(15)
pdfg.MarginBottom.Set(15)
pdfg.MarginLeft.Set(15)
pdfg.MarginRight.Set(15)
err = pdfg.Create()
if err != nil {
fmt.Printf("pdfg create err: %#v\n", err)
return
}
// Write buffer contents to file on disk
err = pdfg.WriteFile(p.FileName)
if err != nil {
return
}
if p.CoverPath != "" {
err = os.Remove(p.CoverPath)
}
return
}