forked from raykov/mdtopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdtopdf.go
64 lines (48 loc) · 1.27 KB
/
mdtopdf.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
package mdtopdf
import (
"io"
"github.com/Acke0/mdtopdf/document"
"github.com/Acke0/mdtopdf/renderer"
"github.com/raykov/gofpdf"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"
)
// Convert your Markdown to PDF
func Convert(r io.Reader, w io.Writer, extensions ...func(*gofpdf.Fpdf)) error {
md, err := io.ReadAll(r)
if err != nil {
return err
}
markdown := goldmark.New(
goldmark.WithRenderer(renderer.NewRenderer()),
goldmark.WithExtensions(
extension.NewTable(),
extension.Strikethrough,
),
)
pdf := gofpdf.New("P", "pt", "A4", ".")
for _, extension := range extensions {
extension(pdf)
}
pdf.AddPage()
style := SetStyleFromPdf(pdf)
d := document.NewDocument(pdf, style)
if err = markdown.Convert(md, d); err != nil {
return err
}
err = pdf.Output(w)
if err != nil {
return err
}
return nil
}
// Use styles defined in PDF extensions
func SetStyleFromPdf(pdf *gofpdf.Fpdf) (style *document.Style) {
style = document.DefaultStyle
style.FontSize, _ = pdf.GetFontSize()
style.FillColor.R, style.FillColor.G, style.FillColor.B = pdf.GetFillColor()
style.TextColor.R, style.TextColor.G, style.TextColor.B = pdf.GetTextColor()
style.LeftMargin, _, _, _ = pdf.GetMargins()
style.CellMargin = pdf.GetCellMargin()
return
}