palapala_pdf 0.1.12 → 0.1.13

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: 3a5231c5a9994723db5fafc42682644a6320a69813444bc0939e179225e7181b
4
+ data.tar.gz: 5f3600f8fb867ee059e1a67fcda2eeeff0d7f290c8621d8bc055e6fb4cbda39c
5
5
  SHA512:
6
- metadata.gz: 3458400fa50aeb4d2e672f156e8bf81f0a8d6a2384cb4b3d8624ca0cc18adc2c8a38eb905850fcb8d73ac3a72cd61525f77f02c5c1dd26d4a1f32ec87831556c
7
- data.tar.gz: f536a148a022ebed055f349362d1f90593de300ac7972e767dc26c20db7be1ee7e1437a1195fa15e4ce5f5b40dc6e623196a64e6c0cc34c3a3fdf61dba080e4e
6
+ metadata.gz: a50a38a6482f04f7af087b0469d0fcf0d5bc02e79f3ad2fe26a91c5b06aa07345fe15b99472355eab1daef95c3069d54e1d9990820b401872681172b278f43d2
7
+ data.tar.gz: 2697106dfcad72207bd20fcad857c9a474ffacf49a67fa10e1d2801a8b5c41226e9d9b7145066b78f6dc002dc951f706c1f5fd83c346b196674f59294d124bc0
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: "DRAFT").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
data/lib/palapala/pdf.rb CHANGED
@@ -43,7 +43,8 @@ module Palapala
43
43
  paper_width: nil,
44
44
  prefer_css_page_size: nil,
45
45
  print_background: nil,
46
- scale: nil)
46
+ scale: nil,
47
+ watermark: nil)
47
48
  @content = content || raise(ArgumentError, "Content is required and can't be nil")
48
49
  @opts = {}
49
50
  raise(ArgumentError, "Either footer or footer_template is expected") if !footer_template.nil? && !footer.nil?
@@ -62,11 +63,29 @@ module Palapala
62
63
  @opts[:preferCSSPageSize] = prefer_css_page_size || Palapala.defaults[:prefer_css_page_size]
63
64
  @opts[:printBackground] = print_background || Palapala.defaults[:print_background]
64
65
  @opts[:scale] = scale || Palapala.defaults[:scale]
66
+ @opts[:headerTemplate] = (@opts[:headerTemplate].to_s + watermark(watermark)) if watermark
65
67
  @opts[:displayHeaderFooter] = (@opts[:headerTemplate] || @opts[:footerTemplate]) ? true : false
66
68
  @opts[:encoding] = :binary
67
69
  @opts.compact!
68
70
  end
69
71
 
72
+ def watermark(watermark, angle: "-15deg", color: "rgba(25,25,25,0.25)")
73
+ <<~HTML
74
+ <style>
75
+ .palapala_pdf_watermark {
76
+ position: fixed;
77
+ top: 50%;
78
+ left: 50%;
79
+ transform: translate(-50%, -50%) rotate(#{angle});
80
+ font-size: 8em;
81
+ color: #{color};
82
+ z-index: 9999;
83
+ }
84
+ </style>
85
+ <span class="palapala_pdf_watermark">#{watermark}</span>
86
+ HTML
87
+ end
88
+
70
89
  def hf_template(from:)
71
90
  return if from.nil?
72
91
  style = <<~HTML.freeze
@@ -1,3 +1,3 @@
1
1
  module Palapala
2
- VERSION = "0.1.12"
2
+ VERSION = "0.1.13"
3
3
  end
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.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koen Handekyn