-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhtml2pdf.go
More file actions
202 lines (169 loc) · 6.39 KB
/
html2pdf.go
File metadata and controls
202 lines (169 loc) · 6.39 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package gorestpack
import (
"bytes"
"errors"
"io"
"github.com/eknkc/request"
)
// Create a new Screenshot Client with supplied restpack.io access key
func NewHTMLToPDFClient(accessToken string) HTMLToPDFClient {
return &htmlToPDFClient{
client: &client{
httpClient: request.New(),
accessToken: accessToken,
basePath: "https://restpack.io/api/html2pdf/v5",
},
}
}
// Options supplied to the Restpack Screenshot API for conversion
type HTMLToPDFCaptureOptions struct {
// Custom page size for created document
PDFPage string `json:"pdf_page,omitempty"`
// CSS style margin sizes.
PDFMargins string `json:"pdf_margins,omitempty"`
// Page Orientation
PDFOrientation string `json:"pdf_orientation,omitempty"`
// Additional CSS string to be injected into the page before render.
CSS string `json:"css,omitempty"`
// Additional JS string to be injected into the page before render.
JS string `json:"js,omitempty"`
// Time in milliseconds to delay capture after page load
Delay int `json:"delay,omitempty"`
// Time in milliseconds for the resulting image to be cached for further requests.
CacheTTL int `json:"cache_ttl,omitempty"`
// Custom user-agent header string for the web request.
UserAgent string `json:"user_agent,omitempty"`
// Custom accept-language header string for the web request.
AcceptLanguage string `json:"accept_language,omitempty"`
// Additional headers seperated with newline
Headers string `json:"headers,omitempty"`
// Force CSS media emulation for print or screen.
EmulateMedia string `json:"emulate_media,omitempty"`
// By default, any response from remote server outside http 200-299 status codes generates an error. If you wish to capture error pages, pass true.
AllowFailed bool `json:"allow_failed,omitempty"`
// Wait until window load event fires or network becomes idle before capturing the page.
Wait string `json:"wait,omitempty"`
// Wait until a DOM element matching the provided css selector becomes present on the page.
Shutter string `json:"shutter,omitempty"`
// Ensure that the captured document does not get cached / stored for further use
Privacy bool `json:"privacy,omitempty"`
// If specified, ensures that the resulting file is saved with the given name.
Filename string `json:"filename,omitempty"`
//Custom pdf page width. Must be used together with PdfHeight
PdfWidth string `json:"pdf_width,omitempty"`
//Custom pdf page height. Check PdfWidth for details.
PdfHeight string `json:"pdf_height,omitempty"`
//HTML template for page header. It should have a valid markup and can contain elements with classes 'pageNumber', 'totalPages', 'url', 'title' or 'date'. Header is automatically added to all pages. Note that you need to have top margins on your documents in order to have the header show up.
PdfHeader string `json:"pdf_header,omitempty"`
//HTML template for page footer. Please check pdf_header information for details.
PdfFooter string `json:"pdf_footer,omitempty"`
//Removes the ads on the page
BlockAds bool `json:"block_ads,omitempty"`
//Block / hide European Union cookie warnings before capture.
BlockCookieWarnings bool `json:"block_cookie_warnings,omitempty"`
}
type htmlToPDFCallOptions struct {
HTMLToPDFCaptureOptions
JSON bool `json:"json,omitempty"`
URL string `json:"url,omitempty"`
HTML string `json:"html,omitempty"`
}
// Capture result from screenshot API
type HTMLToPDFCaptureResult struct {
Image string `json:"image,omitempty"`
Width string `json:"width,omitempty"`
Height string `json:"height,omitempty"`
RemoteStatus string `json:"remote_status,omitempty"`
Cached bool `json:"cached,string,omitempty"`
URL string `json:"url,omitempty"`
}
// Restpack Screenshot API Client
type HTMLToPDFClient interface {
// Capture a URL and return the information & cdn url
Capture(url string, options ...HTMLToPDFCaptureOptions) (HTMLToPDFCaptureResult, error)
// Capture a HTML snippet and return the information & cdn url
CaptureHTML(url string, options ...HTMLToPDFCaptureOptions) (HTMLToPDFCaptureResult, error)
// Capture a URL and return a reader for resulting pdf
CaptureToReader(url string, options ...HTMLToPDFCaptureOptions) (io.Reader, error)
// Capture a HTML snippet and returna a reader for resulting pdf
CaptureHTMLToReader(url string, options ...HTMLToPDFCaptureOptions) (io.Reader, error)
}
type htmlToPDFClient struct {
*client
}
func (me *htmlToPDFClient) Capture(url string, options ...HTMLToPDFCaptureOptions) (HTMLToPDFCaptureResult, error) {
opt := htmlToPDFCallOptions{
URL: url,
JSON: true,
}
if len(options) > 0 {
opt.HTMLToPDFCaptureOptions = options[0]
}
var res struct {
HTMLToPDFCaptureResult
Error string `json:"error"`
}
httpres, _, err := me.do("POST", "/convert").JSON(opt).EndStruct(&res)
if err != nil {
return HTMLToPDFCaptureResult{}, err
}
if httpres.StatusCode > 300 {
return res.HTMLToPDFCaptureResult, errors.New(res.Error)
}
return res.HTMLToPDFCaptureResult, err
}
func (me *htmlToPDFClient) CaptureHTML(html string, options ...HTMLToPDFCaptureOptions) (HTMLToPDFCaptureResult, error) {
opt := htmlToPDFCallOptions{
HTML: html,
JSON: true,
}
if len(options) > 0 {
opt.HTMLToPDFCaptureOptions = options[0]
}
var res struct {
HTMLToPDFCaptureResult
Error string `json:"error"`
}
httpres, _, err := me.do("POST", "/convert").JSON(opt).EndStruct(&res)
if err != nil {
return HTMLToPDFCaptureResult{}, err
}
if httpres.StatusCode > 300 {
return res.HTMLToPDFCaptureResult, errors.New(res.Error)
}
return res.HTMLToPDFCaptureResult, err
}
func (me *htmlToPDFClient) CaptureToReader(url string, options ...HTMLToPDFCaptureOptions) (io.Reader, error) {
opt := htmlToPDFCallOptions{
URL: url,
JSON: false,
}
if len(options) > 0 {
opt.HTMLToPDFCaptureOptions = options[0]
}
resp, body, err := me.do("POST", "/convert").JSON(opt).End()
if err != nil {
return nil, err
}
if resp.StatusCode > 300 {
return nil, errors.New(resp.Status)
}
return bytes.NewReader(body), err
}
func (me *htmlToPDFClient) CaptureHTMLToReader(html string, options ...HTMLToPDFCaptureOptions) (io.Reader, error) {
opt := htmlToPDFCallOptions{
HTML: html,
JSON: false,
}
if len(options) > 0 {
opt.HTMLToPDFCaptureOptions = options[0]
}
resp, body, err := me.do("POST", "/convert").JSON(opt).End()
if err != nil {
return nil, err
}
if resp.StatusCode > 300 {
return nil, errors.New(resp.Status)
}
return bytes.NewReader(body), err
}