This repository has been archived by the owner on Jan 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
wkhtmltopdf.go
69 lines (44 loc) · 1.71 KB
/
wkhtmltopdf.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
/*
Package wkhtmltopdf implements a wrapper for the html to pdf converter wkhtmltopdf.
For more information on wkhtmltopdf see: http://wkhtmltopdf.org/
Creating Documents
Creating a pdf document is simple:
doc := wkhtmltopdf.NewDocument()
pg := wkhtmltopdf.NewPage("www.google.com")
doc.AddPages(pg)
doc.WriteToFile("google.pdf")
Pages can be sourced from both URLs and local filenames.
Applying Options
You can apply options to both the document and individual pages when creating them.
doc := wkhtmltopdf.NewDocument(wkhtmltopdf.Grayscale(), wkhtmltopdf.PageSize("A4"))
pg := wkhtmltopdf.NewPage("www.google.com", wkhtmltopdf.DefaultHeader())
doc.AddPages(pg)
doc.WriteToFile("google.pdf")
Using Readers and Writers
As well as URLs/filenames, you can source pages from an io.Reader, and write them to an
io.Writer.
If a single reader is provided, this is piped to wkhtmltopdf through stdin. If multiple
readers are provided, the contents are written to a temporary directory and used from there.
The location of the temporary directories can be changed by setting the TempDir variable.
doc := wkhtmltopdf.NewDocument()
buf := bytes.NewBufferString("<html><body><h1>Test Page</h1></body></html>")
pg, err := wkhtmltopdf.NewPageReader(buf)
if err != nil {
log.Fatal("Error reading from reader.")
}
doc.AddPages(pg)
output := &bytes.Buffer{}
err := doc.Write(output)
if err != nil {
log.Fatal("Error writing to writer.")
}
*/
package wkhtmltopdf
var (
// Executable is the command to run wkhtmltopdf. If wkhtmltopdf
// cannot be found on your path, amend this to its location.
Executable = "wkhtmltopdf"
// TempDir is where the directories for creating temporary
// files are created.
TempDir = "."
)