forked from SebastiaanKlippert/go-wkhtmltopdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplesample_test.go
82 lines (63 loc) · 1.64 KB
/
simplesample_test.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
package wkhtmltopdf
import (
"bytes"
"fmt"
"log"
"strings"
)
func ExampleNewPDFGenerator() {
// Create new PDF generator
pdfg, err := NewPDFGenerator()
if err != nil {
log.Fatal(err)
}
// Set global options
pdfg.Dpi.Set(300)
pdfg.Orientation.Set(OrientationLandscape)
pdfg.Grayscale.Set(true)
// Create a new input page from an URL
page := NewPage("https://godoc.org/github.com/SebastiaanKlippert/go-wkhtmltopdf")
// Set options for this page
page.FooterRight.Set("[page]")
page.FooterFontSize.Set(10)
page.Zoom.Set(0.95)
// Add to document
pdfg.AddPage(page)
// Create PDF document in internal buffer
err = pdfg.Create()
if err != nil {
log.Fatal(err)
}
// Write buffer contents to file on disk
err = pdfg.WriteFile("./simplesample.pdf")
if err != nil {
log.Fatal(err)
}
fmt.Println("Done")
// Output: Done
}
func ExampleNewPDFGeneratorFromJSON() {
const html = `<!doctype html><html><head><title>WKHTMLTOPDF TEST</title></head><body>HELLO PDF</body></html>`
// Client code
pdfg := NewPDFPreparer()
pdfg.AddPage(NewPageReader(strings.NewReader(html)))
pdfg.Dpi.Set(600)
// The html string is also saved as base64 string in the JSON file
jsonBytes, err := pdfg.ToJSON()
if err != nil {
log.Fatal(err)
}
// The JSON can be saved, uploaded, etc.
// Server code, create a new PDF generator from JSON, also looks for the wkhtmltopdf executable
pdfgFromJSON, err := NewPDFGeneratorFromJSON(bytes.NewReader(jsonBytes))
if err != nil {
log.Fatal(err)
}
// Create the PDF
err = pdfgFromJSON.Create()
if err != nil {
log.Fatal(err)
}
// Use the PDF
fmt.Printf("PDF size %d bytes", pdfgFromJSON.Buffer().Len())
}