pdf_gem 1.0.2 → 1.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -2
- data/lib/pdf_gem.rb +7 -11
- data/lib/pdf_gem/version.rb +1 -1
- data/lib/pdf_generator.js +15 -6
- data/lib/renderer.rb +24 -8
- metadata +2 -8
- data/lib/tmp/1zlgz1o22datkhezl9dp5j803ocavkyc6wtpmbiz.html +0 -13
- data/lib/tmp/512m1i0cteoydwdoju0e6qdjbm0qttswr711igtr.html +0 -13
- data/lib/tmp/evziomgjof3tg36lz6t6x429rbni8ipjefmxjilq.html +0 -13
- data/lib/tmp/j56oa22hog5nnt1wwebf1xv5nbieeitn5l1nshws.html +0 -13
- data/lib/tmp/kjn1jl9z22l3kdl1vgjdezivta47rlgos30wxx1q.html +0 -13
- data/lib/tmp/nllfaomxczkbqfj470n6r478vva66thutkj7kymn.html +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98f332cd6e3854f2d3d5c4df1947f4aa6c0195e39439d22f9d9766f461e16951
|
4
|
+
data.tar.gz: ea268c17bc32203477eb74502c404d6563fa83fe5967184a59a0c0f53418d889
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90043a43775a11bdac9d658c1fa3f57b2528839ee517adc30ac7a79ce152caf02875a284b571656aca0d6e637bcaad8c2948750afeefb0f84186bae83cca8af8
|
7
|
+
data.tar.gz: 552baaa16c13c45ffc648b2291cc9f6f3cb275b5b7bd37b782c6834d67bd5fefec219a19ec80391172aa3a800701f53a3879dddbd64e060820dd5ea246bb8da1
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# PdfGem
|
2
|
-
This is a gem for converting HTML
|
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
|
|
@@ -68,8 +70,15 @@ PdfGem.pdf_from_string(options)
|
|
68
70
|
|
69
71
|
- `options` <[Object]> Options object which might have the following properties:
|
70
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
|
+
- `waitUntil` <[string]|[Array]<[string]>> When to consider navigation succeeded, defaults to `load`. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events can be either:
|
75
|
+
- `load` - consider navigation to be finished when the `load` event is fired.
|
76
|
+
- `domcontentloaded` - consider navigation to be finished when the `DOMContentLoaded` event is fired.
|
77
|
+
- `networkidle0` - consider navigation to be finished when there are no more than 0 network connections for at least `500` ms.
|
78
|
+
- `networkidle2` - consider navigation to be finished when there are no more than 2 network connections for at least `500` ms.
|
71
79
|
- `html` <[string]> (Used only for PdfGem.pdf_from_string) This is the html string to render.
|
72
80
|
- `disposition` <[string]> (Use only for controller render) Disposition string (inline/attachment).
|
81
|
+
- `formats` <[string]> (Use only for controller render) Force to load view of a particular format (pdf, html, xml).
|
73
82
|
- `filename` <[string]> (Use only for controller render) Filename of the file.
|
74
83
|
- `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
84
|
- `scale` <[number]> Scale of the webpage rendering. Defaults to `1`. Scale amount must be between 0.1 and 2.
|
@@ -134,6 +143,11 @@ The `format` options are:
|
|
134
143
|
> 1. Script tags inside templates are not evaluated.
|
135
144
|
> 2. Page styles are not visible inside templates.
|
136
145
|
|
146
|
+
## Troubleshooting
|
147
|
+
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`
|
148
|
+
|
149
|
+
## Contributing
|
150
|
+
You are welcome to contribute.
|
137
151
|
|
138
152
|
## License
|
139
153
|
The gem is available as open source under the terms of the [Apach e2.0 License](https://opensource.org/licenses/Apache-2.0).
|
data/lib/pdf_gem.rb
CHANGED
@@ -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}
|
9
|
-
if(
|
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
|
-
|
11
|
+
FileUtils.mv(stdout, params[:destination])
|
12
12
|
else
|
13
|
-
|
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
|
data/lib/pdf_gem/version.rb
CHANGED
data/lib/pdf_generator.js
CHANGED
@@ -2,22 +2,31 @@ const puppeteer = require('puppeteer');
|
|
2
2
|
const fs = require('fs');
|
3
3
|
const crypto = require('crypto');
|
4
4
|
const path = require('path');
|
5
|
-
|
6
|
-
|
5
|
+
|
6
|
+
(async () => {
|
7
|
+
|
8
|
+
var stdinBuffer = fs.readFileSync(0);
|
9
|
+
|
10
|
+
var tmp = path.join(path.dirname(__filename), 'tmp');
|
11
|
+
|
12
|
+
if (!fs.existsSync(tmp)){
|
13
|
+
fs.mkdirSync(tmp);
|
14
|
+
}
|
15
|
+
|
16
|
+
const filename = path.join(tmp, crypto.randomBytes(40).toString('hex')+ '.pdf');
|
7
17
|
const browser = await puppeteer.launch();
|
8
18
|
try{
|
9
|
-
const params = {...JSON.parse(Buffer.from(
|
19
|
+
const params = {...JSON.parse(Buffer.from(stdinBuffer.toString(), 'base64').toString('utf-8')), ...{path: filename}};
|
10
20
|
const page = await browser.newPage();
|
11
|
-
await page.goto(params.url);
|
21
|
+
await page.goto(params.url, {waitUntil: (params.waitUntil != null ? params.waitUntil : 'load'), timeout: (params.timeout == null ? 30000 : params.timeout)});
|
12
22
|
await page.pdf(params);
|
13
|
-
|
23
|
+
process.stdout.write(filename)
|
14
24
|
}
|
15
25
|
catch(e){
|
16
26
|
console.error(e)
|
17
27
|
process.exit(1);
|
18
28
|
}
|
19
29
|
finally{
|
20
|
-
fs.unlinkSync(filename)
|
21
30
|
await browser.close();
|
22
31
|
process.exit(0);
|
23
32
|
}
|
data/lib/renderer.rb
CHANGED
@@ -1,11 +1,27 @@
|
|
1
|
-
module PdfGem
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
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.
|
4
|
+
version: 1.0.10
|
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-
|
11
|
+
date: 2020-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -53,12 +53,6 @@ files:
|
|
53
53
|
- lib/pdf_gem/version.rb
|
54
54
|
- lib/pdf_generator.js
|
55
55
|
- lib/renderer.rb
|
56
|
-
- lib/tmp/1zlgz1o22datkhezl9dp5j803ocavkyc6wtpmbiz.html
|
57
|
-
- lib/tmp/512m1i0cteoydwdoju0e6qdjbm0qttswr711igtr.html
|
58
|
-
- lib/tmp/evziomgjof3tg36lz6t6x429rbni8ipjefmxjilq.html
|
59
|
-
- lib/tmp/j56oa22hog5nnt1wwebf1xv5nbieeitn5l1nshws.html
|
60
|
-
- lib/tmp/kjn1jl9z22l3kdl1vgjdezivta47rlgos30wxx1q.html
|
61
|
-
- lib/tmp/nllfaomxczkbqfj470n6r478vva66thutkj7kymn.html
|
62
56
|
homepage: https://github.com/blasko03/pdf_gem
|
63
57
|
licenses:
|
64
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>
|