pdf_gem 1.0.1 → 1.0.9

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: 10af893a52c091022ff0544911751cc04cd09dbd0ce2dd45c3aca4cee493affd
4
- data.tar.gz: ec3e961a45fa96c8fbbe45e1d671f84367600b00f1a4dfd0cfc74c50067b3e7e
3
+ metadata.gz: 3ae377666e9721fcf26fda08d4791500e0d8e28e6eaddcbac702e1238c5815d9
4
+ data.tar.gz: e749a87289fa44a1ce4e47c7549855932400512b0877bd72e9e32eb606652ce1
5
5
  SHA512:
6
- metadata.gz: fd9be56c9327a4894059cb4a174b485d33c1c1c3c8ab744d5b788abd663311769fab3f87f37cc1c60d2c7f6749640b39308330b3840abb1fbb9d3e64ae3892ea
7
- data.tar.gz: caaf28b437e0ba053b8bed3271619d1561ce5cbbc07ec2f93a034b9a8be614498099bfe7613d5bc84730580048a2bae526bd0fe645d94292ddb8e52b12fdb5df
6
+ metadata.gz: c3dac5f68aa57d4f32781b7937b27b5dadb7cda557e6d802c9c1015bf23a78a4db952a797ca9e15a5f4db142a34de6b3a6fae48cae229a6358c7140a3df0811b
7
+ data.tar.gz: 78bb021dcac458206a562b8dc6a423823765f591d69a86f76dd1eb7cca10e5141e733a0fd8e40bb4f43c38b1e98a0c70c2d9fdfa64edcbf399f0e2df04cb5b2f
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # PdfGem
2
- This is a gem for generating PDF from HTML, and is rendered from Chromium Browser
2
+ This is a gem for converting HTML to PDF, the rendering engine is Chromium Browser
3
3
 
4
4
  ## Prerequisites
5
5
  This package depends on node.js and puppeteer run this command for installing them:
@@ -29,7 +29,7 @@ You may need to add
29
29
  ```ruby
30
30
  Mime::Type.register "application/pdf", :pdf
31
31
  ```
32
- to config/initializers/mime_types.rb
32
+ to `config/initializers/mime_types.rb`
33
33
 
34
34
 
35
35
  ## Usage
@@ -49,6 +49,8 @@ class TestController < ApplicationController
49
49
  end
50
50
  ```
51
51
 
52
+ > **NOTE** If your layout include other refernces like images, css or javascript you must provide the absolute path to the files, you can set config.action_controller.asset_host = "assets.example.com" in that way rails will include the full path for assets, more on https://apidock.com/rails/v6.0.0/ActionView/Helpers/AssetUrlHelper
53
+
52
54
  ### Available lib methods
53
55
 
54
56
 
@@ -67,11 +69,13 @@ PdfGem.pdf_from_string(options)
67
69
  ## Options
68
70
 
69
71
  - `options` <[Object]> Options object which might have the following properties:
70
- - `url` <[string]> (Use only for PdfGem.pdf_from_url) This is the url to render.
71
- - `html` <[string]> (Use only for PdfGem.pdf_from_string) This is the html string to render.
72
+ - `url` <[string]> (Used only for PdfGem.pdf_from_url) This is the url to render.
73
+ - `timeout` <[number]> Maximum navigation time in ms, default is 3000 ms.
74
+ - `html` <[string]> (Used only for PdfGem.pdf_from_string) This is the html string to render.
72
75
  - `disposition` <[string]> (Use only for controller render) Disposition string (inline/attachment).
76
+ - `formats` <[string]> (Use only for controller render) Force to load view of a particular format (pdf, html, xml).
73
77
  - `filename` <[string]> (Use only for controller render) Filename of the file.
74
- - `destination` <[string]> The file path to save the PDF to. If no destination is provided, will be returned a binary string
78
+ - `destination` <[string]> (Use only for PdfGem.pdf_from_url and PdfGem.pdf_from_string) The file path to save the PDF to. If no destination is provided, will be returned a binary string
75
79
  - `scale` <[number]> Scale of the webpage rendering. Defaults to `1`. Scale amount must be between 0.1 and 2.
76
80
  - `displayHeaderFooter` <[boolean]> Display header and footer. Defaults to `false`.
77
81
  - `headerTemplate` <[string]> HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them:
@@ -94,8 +98,6 @@ PdfGem.pdf_from_string(options)
94
98
  - `left` <[string]|[number]> Left margin, accepts values labeled with units.
95
99
  - `preferCSSPageSize` <[boolean]> Give any CSS `@page` size declared in the page priority over what is declared in `width` and `height` or `format` options. Defaults to `false`, which will scale the content to fit the paper size.
96
100
 
97
- > **NOTE** Generating a pdf is currently only supported in Chrome headless.
98
-
99
101
  > **NOTE** By default, generates a pdf with modified colors for printing. Use the [`-webkit-print-color-adjust`](https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-print-color-adjust) property to force rendering of exact colors.
100
102
 
101
103
 
@@ -136,6 +138,11 @@ The `format` options are:
136
138
  > 1. Script tags inside templates are not evaluated.
137
139
  > 2. Page styles are not visible inside templates.
138
140
 
141
+ ## Troubleshooting
142
+ In development enviroment if the server runs in single thead mode the app will go in deadlock. Youm must run the server in multithread mode. If you use puma as dev server you can start the server with `puma -w 3`
143
+
144
+ ## Contributing
145
+ You are welcome to contribute.
139
146
 
140
147
  ## License
141
148
  The gem is available as open source under the terms of the [Apach e2.0 License](https://opensource.org/licenses/Apache-2.0).
@@ -5,16 +5,18 @@ require 'renderer'
5
5
 
6
6
  module PdfGem
7
7
  def self.pdf_from_url(params)
8
- stdout, stderr = Open3.capture3("node #{File.join(File.dirname(__FILE__), 'pdf_generator.js').to_s} #{Base64.strict_encode64(params.to_json).to_s}")
9
- if(stdout.present? and stderr.empty?)
8
+ stdout, stderr, s = Open3.capture3("node #{File.join(File.dirname(__FILE__), 'pdf_generator.js').to_s}", stdin_data: Base64.strict_encode64(params.to_json).to_s)
9
+ if(s.success?)
10
10
  if(params[:destination].present?)
11
- self.save_to_file(params[:destination], stdout)
11
+ FileUtils.mv(stdout, params[:destination])
12
12
  else
13
- return Base64.decode64(stdout)
13
+ res = File.open(stdout, 'rb').read
14
+ File.delete(stdout) if File.exist?(stdout)
15
+ return res
14
16
  end
15
17
  else
16
18
  raise stderr.present? ? stderr : "error"
17
- end
19
+ end
18
20
  end
19
21
 
20
22
  def self.pdf_from_string(params)
@@ -29,10 +31,4 @@ module PdfGem
29
31
  File.delete(html_file) if File.exist?(html_file)
30
32
  return result
31
33
  end
32
-
33
- def self.save_to_file(destination, data)
34
- File.open(destination, "wb") do |f|
35
- f.write(Base64.decode64(data))
36
- end
37
- end
38
34
  end
@@ -1,3 +1,3 @@
1
1
  module PdfGem
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.9'
3
3
  end
@@ -2,22 +2,24 @@ const puppeteer = require('puppeteer');
2
2
  const fs = require('fs');
3
3
  const crypto = require('crypto');
4
4
  const path = require('path');
5
- (async () => {
5
+
6
+ (async () => {
7
+
8
+ var stdinBuffer = fs.readFileSync(0);
6
9
  const filename = path.join(path.dirname(__filename), 'tmp', crypto.randomBytes(40).toString('hex')+ '.pdf');
7
10
  const browser = await puppeteer.launch();
8
11
  try{
9
- const params = {...JSON.parse(Buffer.from(process.argv.slice(2)[0], 'base64').toString('utf-8')), ...{path: filename}};
12
+ const params = {...JSON.parse(Buffer.from(stdinBuffer.toString(), 'base64').toString('utf-8')), ...{path: filename}};
10
13
  const page = await browser.newPage();
11
- await page.goto(params.url);
14
+ await page.goto(params.url, {waitUntil: 'networkidle2', timeout: (params.timeout == null ? 30000 : params.timeout)});
12
15
  await page.pdf(params);
13
- console.log(Buffer.from(fs.readFileSync(filename, 'binary'), 'binary').toString('base64'))
16
+ process.stdout.write(filename)
14
17
  }
15
18
  catch(e){
16
19
  console.error(e)
17
20
  process.exit(1);
18
21
  }
19
22
  finally{
20
- fs.unlinkSync(filename)
21
23
  await browser.close();
22
24
  process.exit(0);
23
25
  }
@@ -1,11 +1,27 @@
1
- module PdfGem
2
- ActionController::Renderers.add :pdf do |template, options|
3
- params = options.except(:prefixes, :template, :disposition, :url, :html, :filename)
4
- params[:html] = render_to_string(template.present? ? template : options.template)
5
- send_data PdfGem::pdf_from_string(params), type: Mime[:pdf], disposition: (params[:disposition].present? ? params[:disposition] : 'inline'), :filename => options[:filename]
6
- end
1
+ module PdfGem
2
+
3
+ class PdfGemRailtie < Rails::Railtie
4
+ initializer "my_railtie.configure_rails_initialization" do
5
+
6
+ ActionController::Renderers.add :pdf do |template, options|
7
+ params = options.except(:prefixes, :template, :disposition, :url, :html, :filename, :formats)
8
+ params[:html] = render_to_string(:action => (template.present? ? template : options.template), formats: options[:formats].present? ? options[:formats] : [:pdf] )
9
+ send_data PdfGem::pdf_from_string(params), type: Mime[:pdf], disposition: (params[:disposition].present? ? params[:disposition] : 'inline'), :filename => options[:filename]
10
+ end
7
11
 
8
- if Mime::Type.lookup_by_extension(:pdf).nil?
9
- Mime::Type.register('application/pdf', :pdf)
12
+ ActionController::Renderers.add :pdf2 do |template, options|
13
+ params = options.except(:prefixes, :template, :disposition, :url, :html, :filename, :formats)
14
+ params[:html] = render_to_string(:action => (template.present? ? template : options.template), formats: options[:formats].present? ? options[:formats] : [:pdf] )
15
+ send_data PdfGem::pdf_from_string(params), type: Mime[:pdf], disposition: (params[:disposition].present? ? params[:disposition] : 'inline'), :filename => options[:filename]
16
+ end
17
+ if Mime::Type.lookup_by_extension(:pdf).nil?
18
+ Mime::Type.register('application/pdf', :pdf)
19
+ end
20
+ end
10
21
  end
22
+
23
+
24
+
25
+
26
+
11
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdf_gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Blasina
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-25 00:00:00.000000000 Z
11
+ date: 2020-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,7 +38,8 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- description: Gem based on chrome browser
41
+ description: This is a gem for converting HTML in PDF, the rendering engine is Chromium
42
+ Browser
42
43
  email:
43
44
  - blzk100@gmailcom
44
45
  executables: []
@@ -52,12 +53,6 @@ files:
52
53
  - lib/pdf_gem/version.rb
53
54
  - lib/pdf_generator.js
54
55
  - lib/renderer.rb
55
- - lib/tmp/1zlgz1o22datkhezl9dp5j803ocavkyc6wtpmbiz.html
56
- - lib/tmp/512m1i0cteoydwdoju0e6qdjbm0qttswr711igtr.html
57
- - lib/tmp/evziomgjof3tg36lz6t6x429rbni8ipjefmxjilq.html
58
- - lib/tmp/j56oa22hog5nnt1wwebf1xv5nbieeitn5l1nshws.html
59
- - lib/tmp/kjn1jl9z22l3kdl1vgjdezivta47rlgos30wxx1q.html
60
- - lib/tmp/nllfaomxczkbqfj470n6r478vva66thutkj7kymn.html
61
56
  homepage: https://github.com/blasko03/pdf_gem
62
57
  licenses:
63
58
  - Apache-2.0
@@ -1,13 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
-
3
- <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4
- <title>Test HTML File</title>
5
-
6
- </head>
7
- <body>
8
-
9
- <p>This is a simple HTML file.</p>
10
-
11
-
12
-
13
- </body></html>
@@ -1,13 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
-
3
- <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4
- <title>Test HTML File</title>
5
-
6
- </head>
7
- <body>
8
-
9
- <p>This is a simple HTML file.</p>
10
-
11
-
12
-
13
- </body></html>
@@ -1,13 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
-
3
- <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4
- <title>Test HTML File</title>
5
-
6
- </head>
7
- <body>
8
-
9
- <p>This is a simple HTML file.</p>
10
-
11
-
12
-
13
- </body></html>
@@ -1,13 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
-
3
- <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4
- <title>Test HTML File</title>
5
-
6
- </head>
7
- <body>
8
-
9
- <p>This is a simple HTML file.</p>
10
-
11
-
12
-
13
- </body></html>
@@ -1,13 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
-
3
- <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4
- <title>Test HTML File</title>
5
-
6
- </head>
7
- <body>
8
-
9
- <p>This is a simple HTML file.</p>
10
-
11
-
12
-
13
- </body></html>
@@ -1,13 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
-
3
- <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4
- <title>Test HTML File</title>
5
-
6
- </head>
7
- <body>
8
-
9
- <p>This is a simple HTML file.</p>
10
-
11
-
12
-
13
- </body></html>