pdf_gem 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -2
- data/lib/pdf_gem.rb +7 -11
- data/lib/pdf_gem/version.rb +1 -1
- data/lib/pdf_generator.js +7 -5
- data/lib/renderer.rb +8 -2
- 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: c618dfc11c4ba373b60e66c30a723f1f45b17f9b4437e5ea2b1458ac6dd3a20a
|
4
|
+
data.tar.gz: e7b54132e60927e11ecbcafe632286f86cd7b370a960b68a8a3dcbdfb392d143
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2e0e27de69f25c6e63c7e672deecdcdfc13b98c5ea16008131852a9655d8fbc6a10ac933826dd3dea705bb125000abab9c3e2592e772bba10cc325e26e3617ba
|
7
|
+
data.tar.gz: 513d9320d4ac3f1410cac31ed22bd84fd70bbb7464e4eb723edbd7d843fad378f58c7f0bc7d7f47004175a7e3f4349553d413126ca51bae6e634300d78bd4bfe
|
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
|
|
@@ -70,6 +72,7 @@ PdfGem.pdf_from_string(options)
|
|
70
72
|
- `url` <[string]> (Used only for PdfGem.pdf_from_url) This is the url to render.
|
71
73
|
- `html` <[string]> (Used only for PdfGem.pdf_from_string) This is the html string to render.
|
72
74
|
- `disposition` <[string]> (Use only for controller render) Disposition string (inline/attachment).
|
75
|
+
- `formats` <[string]> (Use only for controller render) Force to load view of a particular format (pdf, html, xml).
|
73
76
|
- `filename` <[string]> (Use only for controller render) Filename of the file.
|
74
77
|
- `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
78
|
- `scale` <[number]> Scale of the webpage rendering. Defaults to `1`. Scale amount must be between 0.1 and 2.
|
@@ -134,6 +137,11 @@ The `format` options are:
|
|
134
137
|
> 1. Script tags inside templates are not evaluated.
|
135
138
|
> 2. Page styles are not visible inside templates.
|
136
139
|
|
140
|
+
## Troubleshooting
|
141
|
+
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`
|
142
|
+
|
143
|
+
## Contributing
|
144
|
+
You are welcome to contribute.
|
137
145
|
|
138
146
|
## License
|
139
147
|
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,24 @@ const puppeteer = require('puppeteer');
|
|
2
2
|
const fs = require('fs');
|
3
3
|
const crypto = require('crypto');
|
4
4
|
const path = require('path');
|
5
|
-
|
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(
|
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'});
|
12
15
|
await page.pdf(params);
|
13
|
-
|
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
|
}
|
data/lib/renderer.rb
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
module PdfGem
|
2
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)
|
3
|
+
params = options.except(:prefixes, :template, :disposition, :url, :html, :filename, :formats)
|
4
|
+
params[:html] = render_to_string(:action => (template.present? ? template : options.template), formats: options[:formats].present? ? options[:formats] : [:pdf] )
|
5
|
+
send_data PdfGem::pdf_from_string(params), type: Mime[:pdf], disposition: (params[:disposition].present? ? params[:disposition] : 'inline'), :filename => options[:filename]
|
6
|
+
end
|
7
|
+
|
8
|
+
ActionController::Renderers.add :pdf2 do |template, options|
|
9
|
+
params = options.except(:prefixes, :template, :disposition, :url, :html, :filename, :formats)
|
10
|
+
params[:html] = render_to_string(:action => (template.present? ? template : options.template), formats: options[:formats].present? ? options[:formats] : [:pdf] )
|
5
11
|
send_data PdfGem::pdf_from_string(params), type: Mime[:pdf], disposition: (params[:disposition].present? ? params[:disposition] : 'inline'), :filename => options[:filename]
|
6
12
|
end
|
7
13
|
|
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.3
|
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-27 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>
|