/
pdf.go
145 lines (133 loc) · 4.13 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
package lib
import (
"github.com/go-pdf/fpdf"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/renderer"
"io"
)
type rFunc func(source []byte, node ast.Node, entering bool) (ast.WalkStatus, error)
type pdfRenderer struct {
pdf *fpdf.Fpdf
renderFuncs map[ast.NodeKind]rFunc
}
func (p *pdfRenderer) Render(w io.Writer, source []byte, n ast.Node) error {
n.Dump(source, 2)
err := ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
s := ast.WalkStatus(ast.WalkContinue)
if cb, ok := p.renderFuncs[n.Kind()]; ok {
return cb(source, n, entering)
}
return s, nil
})
return err
}
func (p *pdfRenderer) AddOptions(option ...renderer.Option) {
//TODO implement me
panic("implement me")
}
const p1 = 12
const baseHeight = 6
const fontSize = 14
func Pdf(path string, content string) error {
pdf := fpdf.New("P", "mm", "A4", "")
pdf.AddPage()
lineIndentLevel := 0
baseMargin := pdf.GetX()
renderFuncs := map[ast.NodeKind]rFunc{
ast.KindHeading: func(source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
hd := node.(*ast.Heading)
sizeMap := map[int]float64{
1: 24,
2: 18,
3: 16,
4: 14,
}
if entering {
pdf.SetFont("Arial", "", sizeMap[hd.Level])
pdf.MultiCell(0, baseHeight, string(node.Text(source)), "", "", false)
} else {
pdf.MultiCell(0, p1, "", "", "", false)
pdf.SetFont("Arial", "", fontSize)
}
return ast.WalkSkipChildren, nil
},
ast.KindLink: func(source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
// TODO make link clickable
pdf.SetTextColor(0, 0, 255)
pdf.SetFont("Arial", "U", fontSize)
href := node.(*ast.Link).Destination
pdf.WriteLinkString(baseHeight, string(node.Text(source)), string(href))
} else {
pdf.SetTextColor(0, 0, 0)
pdf.SetFont("Arial", "", fontSize)
}
return ast.WalkSkipChildren, nil
},
ast.KindText: func(source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
pdf.Write(baseHeight, string(node.Text(source)))
}
return ast.WalkSkipChildren, nil
},
ast.KindParagraph: func(source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
pdf.MultiCell(0, p1, "", "", "", false)
}
return ast.WalkContinue, nil
},
ast.KindCodeBlock: func(source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
pdf.SetFont("Courier", "", fontSize)
pdf.MultiCell(0, p1, "", "", "", false)
pdf.MultiCell(0, baseHeight, string(node.Text(source)), "", "", false)
pdf.MultiCell(0, p1, "", "", "", false)
}
return ast.WalkContinue, nil
},
ast.KindList: func(source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
// if it's previous sibling is a paragraph then delete that paragraph margin!!
if node.PreviousSibling() != nil && node.PreviousSibling().Kind() == ast.KindParagraph {
pdf.SetY(pdf.GetY() - 1.5*baseHeight)
}
lineIndentLevel += 1
} else {
pdf.Ln(baseHeight)
pdf.SetLeftMargin(baseMargin)
lineIndentLevel -= 1
}
return ast.WalkContinue, nil
},
ast.KindListItem: func(source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
pdf.Ln(baseHeight)
pdf.SetLeftMargin(baseMargin + float64(lineIndentLevel)*2)
pdf.Write(baseHeight, "- ")
}
return ast.WalkContinue, nil
},
ast.KindFencedCodeBlock: func(source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
// y u no text content?? :(:(
cd := node.(*ast.FencedCodeBlock)
txt := string(cd.Info.Text(source))
// TODO make text content render properly...
pdf.SetFont("Courier", "", fontSize)
pdf.MultiCell(0, p1, "", "", "", false)
pdf.MultiCell(0, baseHeight, txt, "", "", false)
pdf.MultiCell(0, p1, "", "", "", false)
}
return ast.WalkContinue, nil
},
}
rdr := goldmark.New(goldmark.WithRenderer(&pdfRenderer{pdf, renderFuncs}))
var wr io.Writer
err := rdr.Convert([]byte(content), wr)
if err != nil {
return err
}
err = pdf.OutputFileAndClose(path)
return err
}