-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf.go
96 lines (84 loc) · 1.91 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
package davepdf
import (
"bytes"
"fmt"
"github.com/phpdave11/gofpdi"
)
type Pdf struct {
n int
w *bytes.Buffer
catalog *PdfCatalog
resources *PdfResources
fonts []*PdfFont
images []*PdfImage
pageTree *PdfPageTree
functions []*PdfFunction
shadings []*PdfShading
xref *PdfXrefTable
fpdi *gofpdi.Importer
tplObjIds map[string]int
offsets map[int]int
objects []*PdfObject
fontFamily string
fontSize int
x float64
y float64
k float64
h float64
page *PdfPage
fillColor *Color
strokeColor *Color
lineWidth float64
lineCapStyle *LineCap
lineJoinStyle *LineJoin
lineDashPattern *LineDash
}
func NewPdf() *Pdf {
pdf := &Pdf{}
pdf.offsets = make(map[int]int, 0)
pdf.w = new(bytes.Buffer)
pdf.catalog = pdf.newCatalog()
pdf.pageTree = pdf.newPageTree()
pdf.resources = pdf.newResources()
pdf.catalog.pageTree = pdf.pageTree
pdf.fpdi = gofpdi.NewImporter()
pdf.tplObjIds = make(map[string]int, 0)
pdf.functions = make([]*PdfFunction, 0)
pdf.shadings = make([]*PdfShading, 0)
pdf.fonts = make([]*PdfFont, 0)
pdf.images = make([]*PdfImage, 0)
pdf.xref = pdf.newXrefTable()
pdf.k = 1.0
pdf.h = 792.0
pdf.outln("%PDF-1.4\n")
return pdf
}
func (pdf *Pdf) SetXY(x, y float64) {
pdf.x = x
pdf.y = y
}
func (pdf *Pdf) outln(s string) {
pdf.w.WriteString(s)
pdf.w.WriteString("\n")
}
func (pdf *Pdf) out(s string) {
pdf.w.WriteString(s)
}
func (pdf *Pdf) outbyte(b byte) {
pdf.w.WriteByte(b)
}
func (pdf *Pdf) Write() {
pdf.writeCatalog()
pdf.writePageTree()
pdf.writeResources()
pdf.writeFonts()
pdf.writeImages()
pdf.writeFunctions()
pdf.writeShadings()
pdf.writePage()
pdf.writeContents()
pdf.writeXref()
pdf.writeTrailer()
// output PDF
fmt.Print(string(pdf.w.Bytes()))
}