-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (124 loc) · 3.42 KB
/
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
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
146
147
148
149
150
package main
import (
"encoding/base64"
"fmt"
"github.com/cbroglie/mustache"
"github.com/google/uuid"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
)
const footerTemplate = `
<style> .footer{ padding: 20px; font-size: 8px; width: 297mm; }</style>
<div class="footer">
<p>Page Number: <span class="pageNumber"></span></p>
</div>
`
const headerTemplate = `
<style>.pdf-header{ text-align: left; width: 297mm; margin-left: 10mm } img { width: auto; height: 10mm}</style>
<div class=pdf-header>
<img src=%s alt=medium-logo>
</div>
`
const HTMLPath = "tmp/html/%s.html"
const PdfPath = "tmp/pdfs/%s.pdf"
const PeopleTemplate = "templates/new-people.mustache"
type Person struct {
ID uuid.UUID
FirstName string
LastName string
Age int
City string
}
func main() {
fmt.Println("PDF Generation with puppeteer")
people := make([]Person, 0)
for i := 0; i < 50; i++ {
person := Person{
ID: uuid.New(),
FirstName: fmt.Sprintf("User %d", i),
LastName: "lastname",
Age: 25,
City: "Negombo",
}
people = append(people, person)
}
data := map[string]interface{}{
"filename": "people",
"title": "pdf testing doc",
"people": people,
}
templatePath := generateAbsolutePath(PeopleTemplate)
htmlFilepath, err := generateHtmlFile(data, templatePath)
if err != nil {
log.Fatalf("err while html file generation: %v", err.Error())
}
pdfSavingPth := generateAbsolutePath(fmt.Sprintf(PdfPath, "people"))
err = generatePDF(htmlFilepath, pdfSavingPth)
if err != nil {
log.Fatalf("error while pdf generation: %v", err.Error())
}
}
func imageToBase64(imgPath string) (string, error) {
bytes, err := os.ReadFile(imgPath)
if err != nil {
return "", err
}
base64Str := base64.StdEncoding.EncodeToString(bytes)
mimeType := http.DetectContentType(bytes)
switch mimeType {
case "image/jpeg":
base64Str = "data:image/jpeg;base64," + base64Str
case "image/png":
base64Str = "data:image/png;base64," + base64Str
default:
base64Str = ""
}
return base64Str, nil
}
func saveFile(filepath string, data []byte) error {
err := os.WriteFile(filepath, data, 0o600)
return err
}
func generateAbsolutePath(path string) string {
absolutePath, err := filepath.Abs(path)
if err != nil {
return ""
}
return absolutePath
}
func generateHtmlFile(data map[string]interface{}, templatePath string) (string, error) {
templatePath = generateAbsolutePath(templatePath)
htmlOutput, err := mustache.RenderFile(templatePath, data)
if err != nil {
return "", err
}
filename := data["filename"]
filePath := generateAbsolutePath(fmt.Sprintf(HTMLPath, filename))
err = saveFile(filePath, []byte(htmlOutput))
if err != nil {
return "", err
}
return filePath, nil
}
func generatePDF(htmlPath, pdfPath string) error {
imgPath := generateAbsolutePath("templates/images/medium-logo.png")
imgStr, err := imageToBase64(imgPath)
if err != nil {
return err
}
headerImgTemplate := fmt.Sprintf(headerTemplate, imgStr)
command := "puppeteer"
commandArgs := []string{"print", htmlPath, pdfPath, "--sandbox", "false", "--wait-until", "networkidle0",
"--display-header-footer", "true", "--footer-template", footerTemplate, "--header-template", headerImgTemplate,
"--margin-top", "30mm", "--margin-bottom", "50mm"}
cmd := exec.Command(command, commandArgs...)
output, err := cmd.CombinedOutput()
if err != nil {
return err
}
log.Println("puppeteer cli output", string(output))
return nil
}