This repository was archived by the owner on Apr 6, 2026. It is now read-only.
forked from MhmoudGit/html2pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml2pdf.go
More file actions
175 lines (151 loc) · 4.61 KB
/
html2pdf.go
File metadata and controls
175 lines (151 loc) · 4.61 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package html2pdf
import (
"fmt"
"html/template"
"io"
"os"
"path/filepath"
"strings"
"github.com/go-rod/rod"
"github.com/go-rod/rod/lib/launcher"
"github.com/go-rod/rod/lib/proto"
"github.com/pdfcpu/pdfcpu/pkg/api"
)
type Generator[T any] struct {
OutputPath string // directory path for generated data
FinalPdf string // the merged pdf name (make sure to include .pdf in the name)
Template *template.Template // html template
Data []T // valid data for feeding the template
browser *rod.Browser // rod browser for auto generating pdf from html views
HtmlFiles []*os.File // list of generated html files
PdfFiles []string // list of generated pdf files
SingleHtmlFile bool // If you want the template to be single file only
Linux bool // set this to true if running on linux to allow NoSandbox option on Rod luncher
}
// Generate pdf file from multible html templates
func (g *Generator[T]) CreatePdf() error {
err := g.GenerateTemplates()
if err != nil {
return err
}
l := launcher.New().Headless(true).Leakless(true).NoSandbox(g.Linux)
g.browser = rod.New().ControlURL(l.MustLaunch()).MustConnect()
defer g.browser.MustClose()
for i, file := range g.HtmlFiles {
defer file.Close()
pdfFilePath := fmt.Sprintf("./%s/output%d.pdf", g.OutputPath, i)
cd, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting current directory path : %v", err.Error())
}
filePath := filepath.Join(cd, file.Name())
filePath = strings.ReplaceAll(filePath, "\\", "/")
// Construct the URL using the file path
url := "file://" + filePath
err = g.CapturePDF(g.browser, url, pdfFilePath)
if err != nil {
return fmt.Errorf("error capturing PDF files: %v", err.Error())
}
g.PdfFiles = append(g.PdfFiles, pdfFilePath)
}
if !g.SingleHtmlFile {
// Merge the PDF files
err = g.MergePDFs(g.PdfFiles, g.FinalPdf)
if err != nil {
return fmt.Errorf("error merging PDF files: %v", err.Error())
}
} else {
output := fmt.Sprint("./" + g.FinalPdf)
copyFile(g.PdfFiles[0], output)
}
return nil
}
// Generate html templates from the given data and save them into .g.OutputPath
func (g *Generator[T]) GenerateTemplates() error {
if g.SingleHtmlFile {
file, err := g.CreateHtmlFile(1)
if err != nil {
return err
}
err = g.Template.Execute(file, g.Data)
if err != nil {
return fmt.Errorf("error generating templates: %v", err.Error())
}
g.HtmlFiles = append(g.HtmlFiles, file)
return nil
}
// for multible html files
for i, v := range g.Data {
file, err := g.CreateHtmlFile(i)
if err != nil {
return err
}
err = g.Template.Execute(file, v)
if err != nil {
return fmt.Errorf("error generating templates: %v", err.Error())
}
g.HtmlFiles = append(g.HtmlFiles, file)
}
return nil
}
func (g *Generator[T]) CreateHtmlFile(id int) (*os.File, error) {
os.Mkdir(g.OutputPath, 0755)
name := fmt.Sprintf("./%s/output%d.html", g.OutputPath, id)
file, err := os.Create(name)
if err != nil {
return nil, fmt.Errorf("error creating html files: %v", err.Error())
}
return file, nil
}
// Delete html and pdf files except the merged pdf
func (g *Generator[T]) DeleteFiles() error {
err := os.RemoveAll(g.OutputPath)
if err != nil {
return fmt.Errorf("error deleting files directory: %v", err)
}
return nil
}
// Automate opening a prowser then capture the html page as single pdf file
func (g *Generator[T]) CapturePDF(browser *rod.Browser, htmlUrl, outputPath string) error {
page, err := browser.Page(proto.TargetCreateTarget{URL: htmlUrl})
if err != nil {
return fmt.Errorf("error creating browser page: %v", err)
}
page.MustWaitLoad().MustPDF(outputPath)
fmt.Println(htmlUrl, ":::::", outputPath)
return nil
}
// Merging all generated pdf together and create the output file
func (g *Generator[T]) MergePDFs(inputFiles []string, outputFile string) error {
// Merge the PDF files
err := api.MergeCreateFile(inputFiles, outputFile, false, api.LoadConfiguration())
if err != nil {
return fmt.Errorf("error merging PDF files: %v", err.Error())
}
return nil
}
func copyFile(src, dst string) error {
// Open the source file
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
// Create the destination file
destFile, err := os.Create(dst)
if err != nil {
return err
}
defer destFile.Close()
// Copy the content from source to destination
_, err = io.Copy(destFile, sourceFile)
if err != nil {
return err
}
// Flush file to disk
err = destFile.Sync()
if err != nil {
return err
}
return nil
}