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