palapala_pdf 0.1.12 → 0.1.15

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9371e3f5558b6cac6126a9f8004e48dbbf7059e3bfbd230885d66502e1e0f931
4
- data.tar.gz: 769a77ca03fb96e858991d98c0c49f2aa3de58ba0dc2e0f33207c264969ce9c4
3
+ metadata.gz: cb2b592eddfeb0f59e676024799d3042f7244a009ef1df2404ce70b47b917a8e
4
+ data.tar.gz: 2759e4fdfabc81dcf85271bcd7caaab869dfeea54f2702f8f334a962cbf82168
5
5
  SHA512:
6
- metadata.gz: 3458400fa50aeb4d2e672f156e8bf81f0a8d6a2384cb4b3d8624ca0cc18adc2c8a38eb905850fcb8d73ac3a72cd61525f77f02c5c1dd26d4a1f32ec87831556c
7
- data.tar.gz: f536a148a022ebed055f349362d1f90593de300ac7972e767dc26c20db7be1ee7e1437a1195fa15e4ce5f5b40dc6e623196a64e6c0cc34c3a3fdf61dba080e4e
6
+ metadata.gz: 39523b9ea30d700456961862236ca0405a9e7311d7c99d81a8c9c0ed73df0b1088b2c9bedf143bc947e95030147cb8da63d9cb4cc102efea34b865919d371e84
7
+ data.tar.gz: 24958e43641f78b024e4848888f19cb7c6d688304486baad281320ab8a3f1bc443ab012f85a51fff174969b047356b89461436529e07f906d8e7c9b3294c02a2
data/README.md CHANGED
@@ -75,7 +75,7 @@ HEADLESS_CHROME_URL=http://192.168.1.1:9222 ruby examples/performance_benchmark.
75
75
  ```
76
76
 
77
77
  ```sh
78
- CHROME_HEADLESS_PATH=/var/to/chrome ruby examples/performance_benchmark.rb
78
+ HEADLESS_CHROME_PATH=/var/to/chrome ruby examples/performance_benchmark.rb
79
79
  ```
80
80
 
81
81
  **Create a PDF from HTML**
@@ -97,6 +97,7 @@ binary_data = Palapala::Pdf.new("<h1>Hello, world! #{Time.now}</h1>").binary_dat
97
97
  ## Advanced Examples
98
98
 
99
99
  - headers and footers
100
+ - watermark
100
101
  - paged css for paper sizes, paper margins, pages breaks, etc
101
102
  - js based rendering
102
103
 
@@ -189,7 +190,29 @@ In this example, `pdf_data` is the binary data of the PDF file. The `filename` o
189
190
 
190
191
  TODO
191
192
 
192
- *It has also been reported that the Chrome process repeatedly crashes when running inside a Docker container on an M1 Mac. Chrome should work as expected when deployed to a Docker container on a non-M1 Mac.*
193
+ ```Dockerfile
194
+ # Install Nodejs and Chromium, to import the chrome headless shell dependencies easily (chrome itself is not used)
195
+ RUN curl -fsSL https://deb.nodesource.com/setup_22.x | bash - && apt-get install --no-install-recommends -y nodejs chromium && \
196
+ rm -rf /var/lib/apthet/lists /var/cache/apt/archives
197
+
198
+ # Install Chrome Headless Shell
199
+ RUN npx --yes @puppeteer/browsers install chrome-headless-shell@stable
200
+ ```
201
+
202
+ Use a script like the below, to launch chrome headless shell from e.g. docker entrypoint script.
203
+
204
+ *launch_chrome_headless_shell.rb*
205
+
206
+ ```sh
207
+ #!/bin/bash
208
+ # find the installation path of chrome headless shell (latest version)
209
+ export CHROME_PATH=$(npx --yes @puppeteer/browsers install chrome-headless-shell@stable | awk '{print $2}')
210
+ # start chrome headless with remote debugging on
211
+ $CHROME_PATH --disable-gpu --remote-debugging-port=9222 --disable-software-rasterizer --disable-bluetooth --no-sandbox
212
+ ```
213
+
214
+ *It has also been reported that the Chrome process repeatedly crashes when running inside a Docker container on an M1 Mac. Chrome should work asexpected when deployed to a Docker container on a non-M1 Mac.*
215
+
193
216
 
194
217
  ## Thread-safety
195
218
 
@@ -203,12 +226,40 @@ thread safe in the sense that every web socket get's a new tab in the underlying
203
226
 
204
227
  TODO
205
228
 
206
- possible buildpacks
229
+ This buildpack installs chrome and chromedriver (chromedriver is actually not needed, but at least the buildpack is maintained)
207
230
 
208
- https://github.com/heroku/heroku-buildpack-chrome-for-testing
231
+ ```sh
232
+ https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-google-chrome
233
+ ```
209
234
 
210
- this buildpack install chrome and chromedriver, which is actually not needed, but it's maintained
235
+ ### launch as child process
211
236
 
212
- https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-google-chrome
237
+ set `HEADLESS_CHROME_PATH=chrome` as an ENV variable as this buildpacks adds `chrome` to the path.
213
238
 
214
- this buildpack installs chrome, which is all we need, but it's deprecated
239
+ ### run seperately
240
+
241
+ In your `Procfile` adjust the web worker command
242
+
243
+ ```yaml
244
+ web: bin/start
245
+ ```
246
+
247
+ Create a bin/start script
248
+
249
+ ```sh
250
+ #!/bin/bash
251
+ # Start Rails app
252
+ bundle exec rails server -p $PORT -e $RAILS_ENV &
253
+
254
+ # Start the background app
255
+ command_to_start_your_background_app &
256
+
257
+ # Wait for all processes to finish
258
+ wait -n
259
+ ```
260
+
261
+ ensure the script is executable
262
+
263
+ ```sh
264
+ chmod +x bin/start
265
+ ```
Binary file
@@ -23,7 +23,8 @@ Palapala::Pdf.new(
23
23
  footer:,
24
24
  margin_top: 1,
25
25
  margin_left: 1,
26
- margin_bottom: 1).save('headers_and_footers.pdf')
26
+ margin_bottom: 1,
27
+ watermark: "CLASSIFIED").save('headers_and_footers.pdf')
27
28
 
28
29
  puts "Generated headers_and_footers.pdf"
29
30
  # `open headers_and_footers.pdf`
Binary file
Binary file
@@ -0,0 +1,54 @@
1
+ module Palapala
2
+ module Helper
3
+ def self.header(left: "", center: "", right: "", margin: "1cm")
4
+ <<~HTML
5
+ <div style="display: flex; justify-content: space-between; width: 100%; margin-left: #{margin}; margin-right: #{margin};">
6
+ <div style="text-align: left; flex: 1;">#{left}</div>
7
+ <div style="text-align: center; flex: 1;">#{center}</div>
8
+ <div style="text-align: right; flex: 1;">#{right}</div>
9
+ </div>
10
+ HTML
11
+ end
12
+
13
+ def self.footer(left: "", center: "", right: "", margin: "1cm")
14
+ self.header(left:, center:, right:, margin:)
15
+ end
16
+ ``
17
+ def self.page_number
18
+ <<~HTML
19
+ <span class="pageNumber"></span>/<span class="totalPages"></span>
20
+ HTML
21
+ end
22
+
23
+ def self.watermark(watermark, angle: "-15deg", color: "rgba(25,25,25,0.25)", font_size: "72pt")
24
+ <<~HTML
25
+ <style>
26
+ .palapala_pdf_watermark {
27
+ position: fixed;
28
+ top: 50%;
29
+ left: 50%;
30
+ transform: translate(-50%, -50%) rotate(#{angle});
31
+ font-size: #{font_size};
32
+ color: #{color};
33
+ z-index: 9999;
34
+ }
35
+ </style>
36
+ <span class="palapala_pdf_watermark">#{watermark}</span>
37
+ HTML
38
+ end
39
+
40
+ def self.hf_template(from:)
41
+ return if from.nil?
42
+ style = <<~HTML.freeze
43
+ <style>
44
+ #header, #footer {
45
+ font-size: 10pt;
46
+ display: flex;
47
+ justify-content: center;
48
+ }
49
+ </style>
50
+ HTML
51
+ style + from
52
+ end
53
+ end
54
+ end
data/lib/palapala/pdf.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require_relative "./renderer"
2
+ require_relative "./helper"
2
3
 
3
4
  module Palapala
4
5
  # Page class to generate PDF from HTML content using Chrome in headless mode in a thread-safe way
@@ -43,13 +44,14 @@ module Palapala
43
44
  paper_width: nil,
44
45
  prefer_css_page_size: nil,
45
46
  print_background: nil,
46
- scale: nil)
47
+ scale: nil,
48
+ watermark: nil)
47
49
  @content = content || raise(ArgumentError, "Content is required and can't be nil")
48
50
  @opts = {}
49
51
  raise(ArgumentError, "Either footer or footer_template is expected") if !footer_template.nil? && !footer.nil?
50
52
  raise(ArgumentError, "Either header or header_template is expected") if !header_template.nil? && !header.nil?
51
- @opts[:headerTemplate] = header_template || hf_template(from: header) || Palapala.defaults[:header_template]
52
- @opts[:footerTemplate] = footer_template || hf_template(from: footer) || Palapala.defaults[:footer_template]
53
+ @opts[:headerTemplate] = header_template || Helper.hf_template(from: header) || Palapala.defaults[:header_template]
54
+ @opts[:footerTemplate] = footer_template || Helper.hf_template(from: footer) || Palapala.defaults[:footer_template]
53
55
  @opts[:pageRanges] = page_ranges || Palapala.defaults[:page_ranges]
54
56
  @opts[:generateTaggedPDF] = generate_tagged_pdf || Palapala.defaults[:generate_tagged_pdf]
55
57
  @opts[:paperWidth] = paper_width || Palapala.defaults[:paper_width]
@@ -62,25 +64,14 @@ module Palapala
62
64
  @opts[:preferCSSPageSize] = prefer_css_page_size || Palapala.defaults[:prefer_css_page_size]
63
65
  @opts[:printBackground] = print_background || Palapala.defaults[:print_background]
64
66
  @opts[:scale] = scale || Palapala.defaults[:scale]
67
+ @opts[:headerTemplate] = (@opts[:headerTemplate].to_s + Helper.watermark(watermark)) if watermark
65
68
  @opts[:displayHeaderFooter] = (@opts[:headerTemplate] || @opts[:footerTemplate]) ? true : false
69
+ @opts[:headerTemplate] ||= "&nbsp;" if @opts[:displayHeaderFooter]
70
+ @opts[:footerTemplate] ||= "&nbsp;" if @opts[:displayHeaderFooter]
66
71
  @opts[:encoding] = :binary
67
72
  @opts.compact!
68
73
  end
69
74
 
70
- def hf_template(from:)
71
- return if from.nil?
72
- style = <<~HTML.freeze
73
- <style>
74
- #header, #footer {
75
- font-size: 10pt;
76
- display: flex;
77
- justify-content: center;
78
- }
79
- </style>
80
- HTML
81
- style + from
82
- end
83
-
84
75
  # Render the PDF content to a binary string.
85
76
  #
86
77
  # The params from the initializer are converted to the expected casing and merged with the options passed to this method.
@@ -1,3 +1,3 @@
1
1
  module Palapala
2
- VERSION = "0.1.12"
2
+ VERSION = "0.1.15"
3
3
  end
data/lib/palapala.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require_relative "palapala/pdf"
2
+ require_relative "palapala/helper"
2
3
  require_relative "palapala/version"
3
4
 
4
5
  module Palapala
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: palapala_pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koen Handekyn
@@ -69,6 +69,7 @@ files:
69
69
  - examples/performance_benchmark.rb
70
70
  - lib/palapala.rb
71
71
  - lib/palapala/chrome_process.rb
72
+ - lib/palapala/helper.rb
72
73
  - lib/palapala/pdf.rb
73
74
  - lib/palapala/renderer.rb
74
75
  - lib/palapala/version.rb