gotenberg-ruby 1.0.0 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +443 -1
- data/lib/gotenberg/analyzers/base.rb +2 -1
- data/lib/gotenberg/analyzers/image.rb +0 -1
- data/lib/gotenberg/backtrace.rb +33 -0
- data/lib/gotenberg/chromium/files.rb +4 -10
- data/lib/gotenberg/chromium/properties.rb +1 -1
- data/lib/gotenberg/chromium.rb +16 -5
- data/lib/gotenberg/client.rb +5 -1
- data/lib/gotenberg/configuration.rb +15 -0
- data/lib/gotenberg/exceptions.rb +5 -0
- data/lib/gotenberg/helpers/action_view.rb +46 -0
- data/lib/gotenberg/railtie.rb +11 -0
- data/lib/gotenberg/version.rb +1 -1
- data/lib/gotenberg-ruby.rb +10 -0
- metadata +27 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1731138f88801603e7ac087f4ca9c12946d0d78b73655f47b61af96002b43ff4
|
4
|
+
data.tar.gz: f11fc54c6125da4e46eb35b8e7110f94f74a8f705f8331cc67aefbbc6aa4c138
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50b23ee43c4a9b87d704af9e044c14192f0dd2d15ea67048fca0eba328c1f627208ac383cc236e6fbeb1afee95f85998100524e4fa6433c1f54a4c10ce4abb86
|
7
|
+
data.tar.gz: d7ce697873e1cff4f3d707eb3973c9829e64cff0f3fedd5e9a2fa362679c3bd949a19907ab481cbbde7a2980a2240f856ca22d7611ca048e9f990e9ee3409ca6
|
data/README.md
CHANGED
@@ -1 +1,443 @@
|
|
1
|
-
|
1
|
+
This package is a Ruby client for [Gotenberg](https://gotenberg.dev), a developer-friendly API to interact with powerful
|
2
|
+
tools like Chromium and LibreOffice for converting numerous document formats (HTML, Markdown, Word, Excel, etc.) into
|
3
|
+
PDF files, and more!
|
4
|
+
|
5
|
+
## Requirement
|
6
|
+
|
7
|
+
This packages requires [Gotenberg](https://gotenberg.dev), a Docker-powered stateless API for PDF files:
|
8
|
+
|
9
|
+
* 🔥 [Live Demo](https://gotenberg.dev/docs/get-started/live-demo)
|
10
|
+
* [Docker](https://gotenberg.dev/docs/get-started/docker)
|
11
|
+
* [Docker Compose](https://gotenberg.dev/docs/get-started/docker-compose)
|
12
|
+
* [Kubernetes](https://gotenberg.dev/docs/get-started/kubernetes)
|
13
|
+
* [Cloud Run](https://gotenberg.dev/docs/get-started/cloud-run)
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem "gotenberg-ruby"
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
* [Send a request to the API](#send-a-request-to-the-api)
|
24
|
+
* [Chromium](#chromium)
|
25
|
+
|
26
|
+
### Send a request to the API
|
27
|
+
|
28
|
+
After having created the HTTP request (see below), you have two options:
|
29
|
+
|
30
|
+
1. Get the response from the API and handle it according to your need.
|
31
|
+
2. Save the resulting file to a given directory.
|
32
|
+
|
33
|
+
> In the following examples, we assume the Gotenberg API is available at http://localhost:3000.
|
34
|
+
|
35
|
+
### Chromium
|
36
|
+
|
37
|
+
The [Chromium module](https://gotenberg.dev/docs/modules/chromium) interacts with the Chromium browser to convert HTML documents to PDF.
|
38
|
+
|
39
|
+
#### Convert a target URL to PDF
|
40
|
+
|
41
|
+
See https://gotenberg.dev/docs/modules/chromium#url.
|
42
|
+
|
43
|
+
Converting a target URL to PDF is as simple as:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
47
|
+
doc.url 'https://my.url'
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
#### Usage:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
# generate pdf output binary data or raise method exception
|
55
|
+
pdf = document.to_binary
|
56
|
+
|
57
|
+
# safe check if pdf generate is success
|
58
|
+
success = document.success?
|
59
|
+
|
60
|
+
# fetch exception data
|
61
|
+
error_message = document.exception.message
|
62
|
+
|
63
|
+
# save PDF file
|
64
|
+
File.open('filename.pdf', 'wb') do |file|
|
65
|
+
file << document.to_binary
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
Available exceptions:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
# raise while PDF transform failed
|
73
|
+
Gotenberg::TransformError
|
74
|
+
|
75
|
+
# raise while change PDF metadata failed
|
76
|
+
Gotenberg::ModifyMetadataError
|
77
|
+
|
78
|
+
# raise while loading asset source failed
|
79
|
+
Gotenberg::RemoteSourceError
|
80
|
+
```
|
81
|
+
|
82
|
+
You may inject `<link>` and `<script>` HTML elements thanks to the `extra_link_tags` and `extra_script_tags` arguments:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
86
|
+
doc.url 'https://my.url', ['https://my.css'], ['https://my.js']
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
Please note that Gotenberg will add the `<link>` and `<script>` elements based on the order of the arguments.
|
91
|
+
|
92
|
+
#### Rails integrations
|
93
|
+
|
94
|
+
For rails apps gem provide few helpful helpers for easier access to assets inside your rails app:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
# read from assets pipeline or webpacker
|
98
|
+
gotenberg_image_tag 'logo.svg'
|
99
|
+
|
100
|
+
# read from absolute file path (use with carefully for security reasons)
|
101
|
+
gotenberg_image_tag 'app/assets/images/logo.svg', absolute_path: true
|
102
|
+
|
103
|
+
# also you can encode you source as base64 data resource (useful for header and footer)
|
104
|
+
gotenberg_image_tag 'app/assets/images/logo.svg', absolute_path: true, inline: true
|
105
|
+
|
106
|
+
# same methods available for js
|
107
|
+
gotenberg_javascript_tag 'application.js', inline: true
|
108
|
+
|
109
|
+
# ... and css.
|
110
|
+
gotenberg_stylesheet_tag 'application.css', inline: true
|
111
|
+
```
|
112
|
+
|
113
|
+
⚠️ Warning! Nested resources for CSS is not supported yet.
|
114
|
+
|
115
|
+
#### Convert an HTML document to PDF
|
116
|
+
|
117
|
+
See https://gotenberg.dev/docs/modules/chromium#html.
|
118
|
+
|
119
|
+
Prepare HTML content with build-in Rails methods:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
# declare HTML renderer
|
123
|
+
renderer = ApplicationController.renderer.new(https: true, http_host: 'localhost:3000')
|
124
|
+
|
125
|
+
# render HTML string for passing into service
|
126
|
+
index_html = renderer.render 'pdf/document', layout: 'pdf', locals: {}
|
127
|
+
```
|
128
|
+
|
129
|
+
You may convert an HTML document string with:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
133
|
+
doc.html index_html
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
137
|
+
You may also send additional files, like images, fonts, stylesheets, and so on. The only requirement is that their paths
|
138
|
+
in the HTML DOM are on the root level.
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
142
|
+
doc.html index_html
|
143
|
+
doc.assets ['/path/to/my.css', '/path/to/my.js']
|
144
|
+
end
|
145
|
+
```
|
146
|
+
|
147
|
+
#### Change PDF meta with exiftools
|
148
|
+
|
149
|
+
If you want to use this feature, you need to install additional package to host system:
|
150
|
+
|
151
|
+
```
|
152
|
+
sudo apt install exiftool
|
153
|
+
```
|
154
|
+
|
155
|
+
and now you can change PDF metatags using exiftools:
|
156
|
+
|
157
|
+
```ruby
|
158
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
159
|
+
doc.html index_html
|
160
|
+
doc.meta title: 'Custom PDF title'
|
161
|
+
end
|
162
|
+
```
|
163
|
+
|
164
|
+
Note: for Rails based apps, you can setup <title>Custom PDF title</title> header in index.html and
|
165
|
+
it will be automatically added to output PDF.
|
166
|
+
|
167
|
+
#### Configuration file (optionally)
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
Gotenberg.configure do |config|
|
171
|
+
# activate HTML debug mode
|
172
|
+
config.html_debug = false
|
173
|
+
|
174
|
+
# default temporary directory for output
|
175
|
+
config.backtrace_dir = Rails.root.join('tmp', 'gotenberg')
|
176
|
+
end
|
177
|
+
```
|
178
|
+
|
179
|
+
#### Convert one or more markdown files to PDF
|
180
|
+
|
181
|
+
See https://gotenberg.dev/docs/modules/chromium#markdown.
|
182
|
+
|
183
|
+
You may convert markdown files with:
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
187
|
+
doc.markdown wrapper_html, ['/path/to/file.md']
|
188
|
+
end
|
189
|
+
```
|
190
|
+
|
191
|
+
The first argument is a `Stream` with HTML content, for instance:
|
192
|
+
|
193
|
+
```html
|
194
|
+
<!doctype html>
|
195
|
+
<html lang="en">
|
196
|
+
<head>
|
197
|
+
<meta charset="utf-8">
|
198
|
+
<title>My PDF</title>
|
199
|
+
</head>
|
200
|
+
<body>
|
201
|
+
{{ toHTML "file.md" }}
|
202
|
+
</body>
|
203
|
+
</html>
|
204
|
+
```
|
205
|
+
|
206
|
+
Here, there is a Go template function `toHTML`. Gotenberg will use it to convert a markdown file's content to HTML.
|
207
|
+
|
208
|
+
Like the HTML conversion, you may also send additional files, like images, fonts, stylesheets, and so on. The only
|
209
|
+
requirement is that their paths in the HTML DOM are on the root level.
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
213
|
+
doc.markdown wrapper_html, ['/path/to/file.md', '/path/to/my2.md']
|
214
|
+
doc.assets ['/path/to/my.css', '/path/to/my.js']
|
215
|
+
end
|
216
|
+
```
|
217
|
+
|
218
|
+
#### Paper size
|
219
|
+
|
220
|
+
You may override the default paper size with:
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
224
|
+
doc.html index_html
|
225
|
+
doc.paper_size width, height
|
226
|
+
end
|
227
|
+
```
|
228
|
+
|
229
|
+
Examples of paper size (width x height, in inches):
|
230
|
+
|
231
|
+
* `Letter` - 8.5 x 11 (default)
|
232
|
+
* `Legal` - 8.5 x 14
|
233
|
+
* `Tabloid` - 11 x 17
|
234
|
+
* `Ledger` - 17 x 11
|
235
|
+
* `A0` - 33.1 x 46.8
|
236
|
+
* `A1` - 23.4 x 33.1
|
237
|
+
* `A2` - 16.54 x 23.4
|
238
|
+
* `A3` - 11.7 x 16.54
|
239
|
+
* `A4` - 8.27 x 11.7
|
240
|
+
* `A5` - 5.83 x 8.27
|
241
|
+
* `A6` - 4.13 x 5.83
|
242
|
+
|
243
|
+
#### Margins
|
244
|
+
|
245
|
+
You may override the default margins (i.e., `0.39`, in inches):
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
249
|
+
doc.html index_html
|
250
|
+
doc.margins top: 1, bottom: 1, left: 0.39, right: 0.39
|
251
|
+
end
|
252
|
+
```
|
253
|
+
|
254
|
+
#### Prefer CSS page size
|
255
|
+
|
256
|
+
You may force page size as defined by CSS:
|
257
|
+
|
258
|
+
```ruby
|
259
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
260
|
+
doc.html index_html
|
261
|
+
doc.prefer_css_page_size
|
262
|
+
end
|
263
|
+
```
|
264
|
+
|
265
|
+
#### Print the background graphics
|
266
|
+
|
267
|
+
```ruby
|
268
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
269
|
+
doc.html index_html
|
270
|
+
doc.print_background
|
271
|
+
end
|
272
|
+
```
|
273
|
+
|
274
|
+
#### Landscape orientation
|
275
|
+
|
276
|
+
You may override the default portrait orientation with:
|
277
|
+
|
278
|
+
```ruby
|
279
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
280
|
+
doc.html index_html
|
281
|
+
doc.landscape
|
282
|
+
end
|
283
|
+
```
|
284
|
+
|
285
|
+
#### Scale
|
286
|
+
|
287
|
+
You may override the default scale of the page rendering (i.e., `1.0`) with:
|
288
|
+
|
289
|
+
```ruby
|
290
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
291
|
+
doc.html index_html
|
292
|
+
doc.scale 2
|
293
|
+
end
|
294
|
+
```
|
295
|
+
|
296
|
+
#### Page ranges
|
297
|
+
|
298
|
+
You may set the page ranges to print, e.g., `1-5, 8, 11-13`. Empty means all pages.
|
299
|
+
|
300
|
+
```ruby
|
301
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
302
|
+
doc.html index_html
|
303
|
+
doc.native_page_ranges '1-2'
|
304
|
+
end
|
305
|
+
```
|
306
|
+
|
307
|
+
#### Header and footer
|
308
|
+
|
309
|
+
You may add a header and/or a footer to each page of the PDF:
|
310
|
+
|
311
|
+
```ruby
|
312
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
313
|
+
doc.header header_html
|
314
|
+
doc.html index_html
|
315
|
+
doc.footer footer_html
|
316
|
+
doc.margins top: 1, bottom: 1
|
317
|
+
end
|
318
|
+
```
|
319
|
+
|
320
|
+
Each of them has to be a complete HTML document:
|
321
|
+
|
322
|
+
```html
|
323
|
+
<html>
|
324
|
+
<head>
|
325
|
+
<style>
|
326
|
+
body {
|
327
|
+
font-size: 8rem;
|
328
|
+
margin: 4rem auto;
|
329
|
+
}
|
330
|
+
</style>
|
331
|
+
</head>
|
332
|
+
<body>
|
333
|
+
<p><span class="pageNumber"></span> of <span class="totalPages"></span></p>
|
334
|
+
</body>
|
335
|
+
</html>
|
336
|
+
```
|
337
|
+
|
338
|
+
The following classes allow you to inject printing values:
|
339
|
+
|
340
|
+
* `date` - formatted print date.
|
341
|
+
* `title` - document title.
|
342
|
+
* `url` - document location.
|
343
|
+
* `pageNumber` - current page number.
|
344
|
+
* `totalPages` - total pages in the document.
|
345
|
+
|
346
|
+
⚠️ Make sure that:
|
347
|
+
|
348
|
+
1. Margins top and bottom are large enough (i.e., `margins(top: 1, bottom: 1, left: 0.39, right: 0.39)`)
|
349
|
+
2. The font size is big enough.
|
350
|
+
|
351
|
+
⚠️ There are some limitations:
|
352
|
+
|
353
|
+
* No JavaScript.
|
354
|
+
* The CSS properties are independent of the ones from the HTML document.
|
355
|
+
* The footer CSS properties override the ones from the header;
|
356
|
+
* Only fonts installed in the Docker image are loaded - see the [Fonts chapter](https://gotenberg.dev/docs/customize/fonts).
|
357
|
+
* Images only work using a base64 encoded source - i.e., `data:image/png;base64, iVBORw0K....`
|
358
|
+
* `background-color` and color `CSS` properties require an additional `-webkit-print-color-adjust: exact` CSS property in order to work.
|
359
|
+
* Assets are not loaded (i.e., CSS files, scripts, fonts, etc.).
|
360
|
+
|
361
|
+
#### Wait delay
|
362
|
+
|
363
|
+
When the page relies on JavaScript for rendering, and you don't have access to the page's code, you may want to wait a
|
364
|
+
certain amount of time (i.e., `1s`, `2ms`, etc.) to make sure Chromium has fully rendered the page you're trying to generate.
|
365
|
+
|
366
|
+
```ruby
|
367
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
368
|
+
doc.html index_html
|
369
|
+
doc.wait_delay '3s'
|
370
|
+
end
|
371
|
+
```
|
372
|
+
|
373
|
+
#### Wait for expression
|
374
|
+
|
375
|
+
You may also wait until a given JavaScript expression returns true:
|
376
|
+
|
377
|
+
```ruby
|
378
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
379
|
+
doc.html index_html
|
380
|
+
doc.wait_for_expression("window.status === 'ready'")
|
381
|
+
end
|
382
|
+
```
|
383
|
+
|
384
|
+
#### User agent
|
385
|
+
|
386
|
+
You may override the default `User-Agent` header used by Gotenberg:
|
387
|
+
|
388
|
+
```ruby
|
389
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
390
|
+
doc.html index_html
|
391
|
+
doc.user_agent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
|
392
|
+
end
|
393
|
+
```
|
394
|
+
|
395
|
+
#### Extra HTTP headers
|
396
|
+
|
397
|
+
You may add HTTP headers that Chromium will send when loading the HTML document:
|
398
|
+
|
399
|
+
```ruby
|
400
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
401
|
+
doc.url 'https://my.url'
|
402
|
+
doc.extra_http_headers [
|
403
|
+
'My-Header-1' => 'My value',
|
404
|
+
'My-Header-2' => 'My value'
|
405
|
+
]
|
406
|
+
end
|
407
|
+
```
|
408
|
+
|
409
|
+
#### Fail on console exceptions
|
410
|
+
|
411
|
+
You may force Gotenberg to return a `409 Conflict` response if there are exceptions in the Chromium console:
|
412
|
+
|
413
|
+
```ruby
|
414
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
415
|
+
doc.url 'https://my.url'
|
416
|
+
doc.fail_on_console_exceptions
|
417
|
+
end
|
418
|
+
```
|
419
|
+
|
420
|
+
#### Emulate media type
|
421
|
+
|
422
|
+
Some websites have dedicated CSS rules for print. Using `screen` allows you to force the "standard" CSS rules.
|
423
|
+
You may also force the `print` media type:
|
424
|
+
|
425
|
+
```ruby
|
426
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
427
|
+
doc.url 'https://my.url'
|
428
|
+
doc.emulate_media_type 'screen'
|
429
|
+
end
|
430
|
+
```
|
431
|
+
|
432
|
+
#### PDF Format
|
433
|
+
|
434
|
+
See https://gotenberg.dev/docs/modules/pdf-engines#engines.
|
435
|
+
|
436
|
+
You may set the PDF format of the resulting PDF with:
|
437
|
+
|
438
|
+
```ruby
|
439
|
+
document = Gotenberg::Chromium.call(ENV['GOTENBERG_URL']) do |doc|
|
440
|
+
doc.url 'https://my.url'
|
441
|
+
doc.pdf_format 'PDF/A-1a'
|
442
|
+
end
|
443
|
+
```
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'faraday'
|
2
2
|
require 'base64'
|
3
|
+
require 'gotenberg/exceptions'
|
3
4
|
|
4
5
|
module Gotenberg
|
5
6
|
module Analyzers
|
@@ -25,7 +26,7 @@ module Gotenberg
|
|
25
26
|
def remote_source
|
26
27
|
Faraday.get(src).body
|
27
28
|
rescue StandardError => e
|
28
|
-
raise 'Unable to load remote source. %s' % e.message
|
29
|
+
raise RemoteSourceError.new('Unable to load remote source. %s' % e.message)
|
29
30
|
end
|
30
31
|
|
31
32
|
def local_source
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'launchy'
|
2
|
+
|
3
|
+
module Gotenberg
|
4
|
+
class Backtrace
|
5
|
+
attr_accessor :files
|
6
|
+
|
7
|
+
def initialize files
|
8
|
+
@files = files
|
9
|
+
@backtrace_location = build_backtrace_location
|
10
|
+
end
|
11
|
+
|
12
|
+
def call
|
13
|
+
FileUtils.mkdir_p(@backtrace_location)
|
14
|
+
|
15
|
+
files.each do |file|
|
16
|
+
File.open(File.join(@backtrace_location, file.original_filename), 'wb') do |f|
|
17
|
+
f << file.io.read
|
18
|
+
end
|
19
|
+
|
20
|
+
file.io.rewind
|
21
|
+
end
|
22
|
+
|
23
|
+
::Launchy.open(File.join(@backtrace_location, 'index.html'))
|
24
|
+
end
|
25
|
+
|
26
|
+
def build_backtrace_location
|
27
|
+
File.join(
|
28
|
+
Gotenberg.configuration.backtrace_dir,
|
29
|
+
"#{Time.now.to_f.to_s.tr('.', '_')}_#{rand(0x100000000).to_s(36)}"
|
30
|
+
)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -10,8 +10,6 @@ module Gotenberg
|
|
10
10
|
def header header
|
11
11
|
compiler = Compiler.new(header)
|
12
12
|
|
13
|
-
#File.open('header.html', 'w') { |f| f.write(compiler.body) }
|
14
|
-
|
15
13
|
files << multipart_file(compiler.body, 'header.html', 'text/html')
|
16
14
|
end
|
17
15
|
|
@@ -20,8 +18,6 @@ module Gotenberg
|
|
20
18
|
def footer footer
|
21
19
|
compiler = Compiler.new(footer)
|
22
20
|
|
23
|
-
#File.open('footer.html', 'w') { |f| f.write(compiler.body) }
|
24
|
-
|
25
21
|
files << multipart_file(compiler.body, 'footer.html', 'text/html')
|
26
22
|
end
|
27
23
|
|
@@ -37,8 +33,6 @@ module Gotenberg
|
|
37
33
|
|
38
34
|
binary_assets(compiler.assets)
|
39
35
|
|
40
|
-
#File.open('index.html', 'w') { |f| f.write(compiler.body) }
|
41
|
-
|
42
36
|
@endpoint = '/forms/chromium/convert/html'
|
43
37
|
|
44
38
|
self
|
@@ -60,16 +54,16 @@ module Gotenberg
|
|
60
54
|
end
|
61
55
|
|
62
56
|
# Sets the additional files, like images, fonts, stylesheets, and so on.
|
63
|
-
def binary_assets
|
64
|
-
|
57
|
+
def binary_assets sources
|
58
|
+
sources.each do |(io, filename)|
|
65
59
|
files << multipart_file(io, filename)
|
66
60
|
end
|
67
61
|
|
68
62
|
self
|
69
63
|
end
|
70
64
|
|
71
|
-
def assets
|
72
|
-
|
65
|
+
def assets sources
|
66
|
+
sources.each do |f|
|
73
67
|
files << multipart_file(IO.binread(f), File.basename(f))
|
74
68
|
end
|
75
69
|
|
data/lib/gotenberg/chromium.rb
CHANGED
@@ -5,6 +5,8 @@ require 'gotenberg/chromium/metadata'
|
|
5
5
|
require 'gotenberg/client'
|
6
6
|
require 'gotenberg/exiftools'
|
7
7
|
require 'gotenberg/extractors'
|
8
|
+
require 'gotenberg/exceptions'
|
9
|
+
require 'gotenberg/backtrace'
|
8
10
|
|
9
11
|
module Gotenberg
|
10
12
|
class Chromium
|
@@ -26,6 +28,7 @@ module Gotenberg
|
|
26
28
|
end
|
27
29
|
|
28
30
|
def call
|
31
|
+
backtrace if html_debug?
|
29
32
|
transform
|
30
33
|
|
31
34
|
if success? && metadata_available?
|
@@ -36,29 +39,37 @@ module Gotenberg
|
|
36
39
|
end
|
37
40
|
|
38
41
|
def success?
|
39
|
-
|
42
|
+
exception == nil
|
40
43
|
end
|
41
44
|
|
42
45
|
def to_binary
|
43
|
-
response
|
46
|
+
response || raise(exception)
|
47
|
+
end
|
48
|
+
|
49
|
+
def html_debug?
|
50
|
+
Gotenberg.configuration.html_debug == true
|
44
51
|
end
|
45
52
|
|
46
53
|
private
|
47
54
|
|
55
|
+
def backtrace
|
56
|
+
Backtrace.new(files).call
|
57
|
+
end
|
58
|
+
|
48
59
|
def transform
|
49
60
|
@response = client.adapter.post(endpoint, properties.merge(files: files), headers).body
|
50
61
|
rescue StandardError => e
|
51
|
-
@exception = e
|
62
|
+
@exception = Gotenberg::TransformError.new(e)
|
52
63
|
end
|
53
64
|
|
54
65
|
def modify_metadata
|
55
66
|
@response = Exiftools.modify(response, metadata)
|
56
67
|
rescue StandardError => e
|
57
|
-
@exception = e
|
68
|
+
@exception = Gotenberg::ModifyMetadataError.new(e)
|
58
69
|
end
|
59
70
|
|
60
71
|
def client
|
61
|
-
|
72
|
+
Client.new(base_path)
|
62
73
|
end
|
63
74
|
end
|
64
75
|
end
|
data/lib/gotenberg/client.rb
CHANGED
@@ -10,12 +10,16 @@ module Gotenberg
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def adapter
|
13
|
-
@adapter ||= Faraday.new(base_path) do |c|
|
13
|
+
@adapter ||= Faraday.new(base_path, headers: default_headers) do |c|
|
14
14
|
c.request :multipart
|
15
15
|
c.request :url_encoded
|
16
16
|
c.adapter :net_http
|
17
17
|
c.response :raise_error
|
18
18
|
end
|
19
19
|
end
|
20
|
+
|
21
|
+
def default_headers
|
22
|
+
{'Content-Type' => 'multipart/form-data'}
|
23
|
+
end
|
20
24
|
end
|
21
25
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Gotenberg
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :backtrace_dir, :html_debug
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@backtrace_dir = if defined?(Rails)
|
7
|
+
Rails.root.join('tmp', 'gotenberg')
|
8
|
+
else
|
9
|
+
Dir.mktmpdir
|
10
|
+
end
|
11
|
+
|
12
|
+
@html_debug = false
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Gotenberg
|
2
|
+
module Helpers
|
3
|
+
module ActionView
|
4
|
+
def gotenberg_image_tag source, options = {}
|
5
|
+
gootenberg_source_tag(source, options.merge(tag: 'image'))
|
6
|
+
end
|
7
|
+
|
8
|
+
def gotenberg_javascript_tag source, options = {}
|
9
|
+
gootenberg_source_tag(source, options.merge(tag: 'js'))
|
10
|
+
end
|
11
|
+
|
12
|
+
def gotenberg_stylesheet_tag source, options = {}
|
13
|
+
gootenberg_source_tag(source, options.merge(tag: 'css'))
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def gootenberg_source_tag source, options = {}
|
19
|
+
src = if options[:absolute_path]
|
20
|
+
File.join(Rails.root, source)
|
21
|
+
elsif Rails.env.development?
|
22
|
+
gootenberg_asset_location(source, :url)
|
23
|
+
else
|
24
|
+
File.join(Rails.public_path, gootenberg_asset_location(source, :path))
|
25
|
+
end
|
26
|
+
|
27
|
+
gootenberg_context_tag(src: src, **options)
|
28
|
+
end
|
29
|
+
|
30
|
+
def gootenberg_context_tag attributes
|
31
|
+
('<!-- GOTENBERG-CONTEXT-TAG %s -->' % attributes.to_json).html_safe
|
32
|
+
end
|
33
|
+
|
34
|
+
def gootenberg_asset_location source, type
|
35
|
+
webpacker = Module.const_defined?(:Webpacker)
|
36
|
+
|
37
|
+
case type
|
38
|
+
when :path
|
39
|
+
public_send((webpacker ? :asset_pack_path : :asset_path), source)
|
40
|
+
when :url
|
41
|
+
public_send((webpacker ? :asset_pack_url : :asset_url), source)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/gotenberg/version.rb
CHANGED
data/lib/gotenberg-ruby.rb
CHANGED
@@ -1,5 +1,15 @@
|
|
1
1
|
require 'gotenberg/version'
|
2
|
+
require 'gotenberg/railtie' if defined?(Rails::Railtie)
|
2
3
|
|
3
4
|
module Gotenberg
|
4
5
|
autoload :Chromium, 'gotenberg/chromium'
|
6
|
+
autoload :Configuration, 'gotenberg/configuration'
|
7
|
+
|
8
|
+
def self.configuration
|
9
|
+
@configuration ||= Configuration.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.configure
|
13
|
+
yield(configuration)
|
14
|
+
end
|
5
15
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gotenberg-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sanzstez
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-09-
|
11
|
+
date: 2022-09-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -52,6 +52,26 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 1.0.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: launchy
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.2'
|
62
|
+
- - "<"
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '3'
|
65
|
+
type: :runtime
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '2.2'
|
72
|
+
- - "<"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '3'
|
55
75
|
description: A gem that provides a client interface for the Gotenberg PDF generate
|
56
76
|
service
|
57
77
|
email:
|
@@ -67,6 +87,7 @@ files:
|
|
67
87
|
- lib/gotenberg/analyzers/css.rb
|
68
88
|
- lib/gotenberg/analyzers/image.rb
|
69
89
|
- lib/gotenberg/analyzers/js.rb
|
90
|
+
- lib/gotenberg/backtrace.rb
|
70
91
|
- lib/gotenberg/chromium.rb
|
71
92
|
- lib/gotenberg/chromium/files.rb
|
72
93
|
- lib/gotenberg/chromium/headers.rb
|
@@ -74,8 +95,12 @@ files:
|
|
74
95
|
- lib/gotenberg/chromium/properties.rb
|
75
96
|
- lib/gotenberg/client.rb
|
76
97
|
- lib/gotenberg/compiler.rb
|
98
|
+
- lib/gotenberg/configuration.rb
|
99
|
+
- lib/gotenberg/exceptions.rb
|
77
100
|
- lib/gotenberg/exiftools.rb
|
78
101
|
- lib/gotenberg/extractors.rb
|
102
|
+
- lib/gotenberg/helpers/action_view.rb
|
103
|
+
- lib/gotenberg/railtie.rb
|
79
104
|
- lib/gotenberg/version.rb
|
80
105
|
homepage: https://github.com/sanzstez/gotenberg-ruby
|
81
106
|
licenses:
|