palapala_pdf 0.1.11 → 0.1.12

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: 230cc525cd5e4bbc4d2ce9ddb0a418486bc27ad6163cef9834a67d7e23442b41
4
- data.tar.gz: b283d90551ef07efe3384148b061ec0a6a18cb3bdbc9485c1e8717e6bc4979f3
3
+ metadata.gz: 9371e3f5558b6cac6126a9f8004e48dbbf7059e3bfbd230885d66502e1e0f931
4
+ data.tar.gz: 769a77ca03fb96e858991d98c0c49f2aa3de58ba0dc2e0f33207c264969ce9c4
5
5
  SHA512:
6
- metadata.gz: cfcf738f7171f679d419349cce4ce0441bb3be54fc355fa9592222b04c63654d6646c6103310f8ddf56e7112a3ccc95b30ddb92db86e0ff48a12b221f8be039c
7
- data.tar.gz: efa8277743d960b0d3e869ab970c8248a2f901b572d4451bf0c479c311eabbd2a810e678efad489913ced8ab2ada14e8ce7d4b4fbb7d3ccad61ce98f419ee1b2
6
+ metadata.gz: 3458400fa50aeb4d2e672f156e8bf81f0a8d6a2384cb4b3d8624ca0cc18adc2c8a38eb905850fcb8d73ac3a72cd61525f77f02c5c1dd26d4a1f32ec87831556c
7
+ data.tar.gz: f536a148a022ebed055f349362d1f90593de300ac7972e767dc26c20db7be1ee7e1437a1195fa15e4ce5f5b40dc6e623196a64e6c0cc34c3a3fdf61dba080e4e
data/README.md CHANGED
@@ -18,6 +18,8 @@ And this while having the most modern HTML/CSS/JS availlable to you: flex, grid,
18
18
 
19
19
  A core goal of this project is performance, and it is designed to be exceptionally fast. By leveraging **direct communication** with a headless Chrome or Chromium browser via a **raw web socket**, the gem minimizes overhead and dependencies, enabling PDF generation at speeds that significantly outperform other solutions. Whether generating simple or complex documents, this gem ensures that your Ruby applications can handle PDF tasks efficiently and at scale.
20
20
 
21
+ [Example: paged_css.pdf](https://raw.githubusercontent.com/palapala-app/palapala_pdf/main/examples/paged_css.pdf)
22
+
21
23
  ## Sponsor This Project
22
24
 
23
25
  If you find this project useful and would like to support its development, consider sponsoring or buying a coffee to help keep it going:
Binary file
@@ -3,18 +3,27 @@
3
3
  $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
4
4
  require 'palapala'
5
5
 
6
- header_template =
7
- '<div style="text-align: center; font-size: 12pt; padding: 1rem; width: 100%;">Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>'
6
+ header =
7
+ '<div class="center">Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>'
8
8
 
9
- footer_template =
10
- '<div style="text-align: center; font-size: 12pt; padding: 1rem; width: 100%;">Generated with Palapala PDF</div>'
9
+ left_center_right = <<~HTML
10
+ <div style="display: flex; justify-content: space-between; width: 100%; margin-left: 1cm; margin-right: 1cm;">
11
+ <div style="text-align: left; flex: 1;">Left Text</div>
12
+ <div style="text-align: center; flex: 1;">Center Text</div>
13
+ <div style="text-align: right; flex: 1;">Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>
14
+ </div>
15
+ HTML
16
+
17
+ footer =
18
+ '<span>Generated at&nbsp;<span class="date"></span></span>'
11
19
 
12
20
  Palapala::Pdf.new(
13
21
  "<h1>Title</h1><p>Hello world #{Time.now}</>",
14
- header_template:,
15
- footer_template:,
16
- margin_top: 3,
17
- margin_bottom: 3).save('headers_and_footers.pdf')
22
+ header: left_center_right,
23
+ footer:,
24
+ margin_top: 1,
25
+ margin_left: 1,
26
+ margin_bottom: 1).save('headers_and_footers.pdf')
18
27
 
19
28
  puts "Generated headers_and_footers.pdf"
20
29
  # `open headers_and_footers.pdf`
Binary file
Binary file
@@ -43,6 +43,7 @@ document = <<~HTML
43
43
  margin: 0;
44
44
  padding: 0;
45
45
  font-family: Arial, sans-serif;
46
+ /* background-color: yellow; */
46
47
  }
47
48
  h1 {
48
49
  page-break-before: always;
@@ -165,21 +166,21 @@ end
165
166
  def header_footer_template(debug_color: nil)
166
167
  <<~HTML
167
168
  #{ debug(color: debug_color) if debug_color }
168
- <div style="text-align: center; font-size: 12pt; padding: 1rem; width: 100%;">#{yield}</div>
169
+ <div style="font-size: 12pt;">#{yield}</div>
169
170
  HTML
170
171
  end
171
172
 
172
- footer_template = header_footer_template do
173
+ footer = header_footer_template do
173
174
  "Page <span class='pageNumber'></span> of <span class='totalPages'></span>"
174
175
  end
175
176
 
176
- header_template = header_footer_template do
177
+ header = header_footer_template do
177
178
  "Generated with Palapala PDF"
178
179
  end
179
180
 
180
181
  Palapala::Pdf.new(document,
181
- header_template:,
182
- footer_template:).save("paged_css.pdf")
182
+ header:,
183
+ footer:).save("paged_css.pdf")
183
184
 
184
185
  puts "Generated paged_css.pdf"
185
186
 
@@ -82,7 +82,7 @@ module Palapala
82
82
  # Display the version
83
83
  system("#{chrome_path} --version") if Palapala.debug
84
84
  # Launch chrome-headless-shell with the --remote-debugging-port parameter
85
- params = [ "--disable-gpu", "--remote-debugging-port=9222" ]
85
+ params = [ "--disable-gpu", "--remote-debugging-port=9222", "--remote-debugging-address=0.0.0.0" ]
86
86
  params.merge!(Palapala.chrome_params) if Palapala.chrome_params
87
87
  pid = if Palapala.debug
88
88
  spawn(chrome_path, *params)
data/lib/palapala/pdf.rb CHANGED
@@ -12,8 +12,10 @@ module Palapala
12
12
  #
13
13
  # @param content [String] the HTML content to convert to PDF
14
14
  # @param footer_html [String] the HTML content for the footer
15
+ # @param footer [String] the footer content that is centered
15
16
  # @param generate_tagged_pdf [Boolean] whether to generate a tagged PDF
16
17
  # @param header_html [String] the HTML content for the header
18
+ # @param header [String] the header content that is centered
17
19
  # @param landscape [Boolean] whether to use landscape orientation
18
20
  # @param margin_bottom [Integer] the bottom margin in inches
19
21
  # @param margin_left [Integer] the left margin in inches
@@ -27,8 +29,10 @@ module Palapala
27
29
  # @param scale [Float] the scale of the PDF rendering
28
30
  def initialize(content,
29
31
  footer_template: nil,
32
+ footer: nil,
30
33
  generate_tagged_pdf: nil,
31
34
  header_template: nil,
35
+ header: nil,
32
36
  landscape: nil,
33
37
  margin_bottom: nil,
34
38
  margin_left: nil,
@@ -42,8 +46,10 @@ module Palapala
42
46
  scale: nil)
43
47
  @content = content || raise(ArgumentError, "Content is required and can't be nil")
44
48
  @opts = {}
45
- @opts[:headerTemplate] = header_template || Palapala.defaults[:header_template]
46
- @opts[:footerTemplate] = footer_template || Palapala.defaults[:footer_template]
49
+ raise(ArgumentError, "Either footer or footer_template is expected") if !footer_template.nil? && !footer.nil?
50
+ 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]
47
53
  @opts[:pageRanges] = page_ranges || Palapala.defaults[:page_ranges]
48
54
  @opts[:generateTaggedPDF] = generate_tagged_pdf || Palapala.defaults[:generate_tagged_pdf]
49
55
  @opts[:paperWidth] = paper_width || Palapala.defaults[:paper_width]
@@ -61,6 +67,20 @@ module Palapala
61
67
  @opts.compact!
62
68
  end
63
69
 
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
+
64
84
  # Render the PDF content to a binary string.
65
85
  #
66
86
  # The params from the initializer are converted to the expected casing and merged with the options passed to this method.
@@ -103,8 +103,20 @@ module Palapala
103
103
  Base64.decode64(result["data"])
104
104
  end
105
105
 
106
+ def ping
107
+ result = send_command_and_wait_for_result("Runtime.evaluate", params: { expression: "1 + 1" })
108
+ raise "Ping failed" unless result["result"]["value"] == 2
109
+ end
110
+
106
111
  def self.html_to_pdf(html, params: {})
107
112
  thread_local_instance.html_to_pdf(html, params: params)
113
+ rescue StandardError
114
+ reset # Reset the renderer on error, the websocket connection might be broken
115
+ thread_local_instance.html_to_pdf(html, params: params) # Retry (once)
116
+ end
117
+
118
+ def self.ping
119
+ thread_local_instance.ping
108
120
  end
109
121
 
110
122
  def close
@@ -1,3 +1,3 @@
1
1
  module Palapala
2
- VERSION = "0.1.11"
2
+ VERSION = "0.1.12"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: palapala_pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koen Handekyn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-30 00:00:00.000000000 Z
11
+ date: 2024-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: base64