pdfgen 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +38 -0
- data/Rakefile +12 -0
- data/VERSION +1 -0
- data/lib/javascript_bin/make_pdf.js +44 -0
- data/lib/pdfgen.rb +48 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 60fe8cc6f4d47c8fd5af37261f7fe11079bbbf6d
|
4
|
+
data.tar.gz: e099bdd44342ddcfd71a767bf2fc5a31d3667a6f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dc69f4ea62cfd5e666a9102bee591621e1dd69c3cb51700bbf215abe9707945b105836cec3f4d07601cf9e42baf387bd647e16ed780059dc35851ec34c401f19
|
7
|
+
data.tar.gz: 5ac4dd0db6eabbf2f162c5eba4ea99de47b59a107d78c92a08884a248fd28ff7d527d1b449faeb0043f788597da4c5946b2b2f88c86a86b91722d173ae6ace98
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Dan Fox
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Pdfgen
|
2
|
+
A tool for creating PDFs using [Puppeteer](https://github.com/GoogleChrome/puppeteer) and headless Chrome.
|
3
|
+
|
4
|
+
# Dependencies
|
5
|
+
This gem requires that node be installed along with puppeteer.
|
6
|
+
|
7
|
+
### OSX (Homebrew)
|
8
|
+
To install node on OSX you can use Homebrew:
|
9
|
+
```bash
|
10
|
+
brew install nodejs
|
11
|
+
```
|
12
|
+
|
13
|
+
And then install puppeteer with npm:
|
14
|
+
```bash
|
15
|
+
npm install --save puppeteer
|
16
|
+
```
|
17
|
+
|
18
|
+
# Usage
|
19
|
+
Currently the only use case that is supported is rendering an HTML string. You can do that by using the
|
20
|
+
Pdfgen class:
|
21
|
+
```ruby
|
22
|
+
pdf_as_string = Pdfgen.new(html_to_turn_into_pdf).to_pdf
|
23
|
+
```
|
24
|
+
`to_pdf` returns the pdf as a string. This makes it easy to send to a client from a web server like
|
25
|
+
Rails or to save to a file if that is desired.
|
26
|
+
|
27
|
+
If you need to provide options such as page margins you can pass them as a hash to `to_pdf`. Any
|
28
|
+
options that work for [Puppeteer's `pdf` method](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions)
|
29
|
+
are accepted as they are passed through without modification.
|
30
|
+
```ruby
|
31
|
+
pdf_as_string = Pdfgen.new(html_to_turn_into_pdf).to_pdf(margins: { top: '1in', bottom: '1in' })
|
32
|
+
```
|
33
|
+
|
34
|
+
# Future Development
|
35
|
+
In the future, allowing use of more features of Puppeteer is desired. These include taking a URL and
|
36
|
+
setting cookies. Instead of using a fixed make_pdf.js script, it will probably make sense to convert
|
37
|
+
to generating that javascript file on the fly using templates since additional options like allowing
|
38
|
+
the setting of cookies will require calling more functions than are currently being called.
|
data/Rakefile
ADDED
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,44 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
|
3
|
+
var stdin = process.stdin,
|
4
|
+
stdout = process.stdout,
|
5
|
+
inputChunks = [];
|
6
|
+
|
7
|
+
stdin.resume();
|
8
|
+
stdin.setEncoding('utf8');
|
9
|
+
|
10
|
+
stdin.on('data', function (chunk) {
|
11
|
+
inputChunks.push(chunk);
|
12
|
+
});
|
13
|
+
|
14
|
+
stdin.on('end', function () {
|
15
|
+
var options = JSON.parse(inputChunks.join());
|
16
|
+
var pdf_options = options['pdf_options'];
|
17
|
+
var current_path = options['current_path'];
|
18
|
+
var viewport_options = options['viewport_options'];
|
19
|
+
var media_type = options['emulate_media'];
|
20
|
+
|
21
|
+
module.paths.push(current_path + '/node_modules');
|
22
|
+
|
23
|
+
const puppeteer = require('puppeteer');
|
24
|
+
const fs = require('fs');
|
25
|
+
var html = fs.readFileSync(process.argv[2], 'utf8');
|
26
|
+
|
27
|
+
(async() => {
|
28
|
+
const browser = await puppeteer.launch();
|
29
|
+
const page = await browser.newPage();
|
30
|
+
|
31
|
+
await page.setContent(html);
|
32
|
+
if (typeof media_type !== 'undefined' && media_type) {
|
33
|
+
await page.emulateMedia(media_type);
|
34
|
+
}
|
35
|
+
|
36
|
+
if (typeof viewport_options !== 'undefined' && viewport_options) {
|
37
|
+
await page.setViewport(viewport_options);
|
38
|
+
}
|
39
|
+
|
40
|
+
const pdf_output = await page.pdf(pdf_options);
|
41
|
+
stdout.write(pdf_output);
|
42
|
+
await browser.close();
|
43
|
+
})();
|
44
|
+
});
|
data/lib/pdfgen.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'open3'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
_, status = Open3.capture2('node', '-v')
|
7
|
+
raise 'This gem requires node be installed and available on the PATH' unless status.success?
|
8
|
+
|
9
|
+
MAKE_PDF_COMMAND = File.expand_path('../javascript_bin/make_pdf.js', __FILE__)
|
10
|
+
|
11
|
+
class Pdfgen
|
12
|
+
def initialize(html)
|
13
|
+
@html = html
|
14
|
+
@viewport_options = nil
|
15
|
+
@emulate_media = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
def set_viewport(viewport_options)
|
19
|
+
@viewport_options = viewport_options
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def emulate_media(media_type)
|
24
|
+
@emulate_media = media_type
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_pdf(opts = {})
|
29
|
+
stdin_options = { pdf_options: opts, current_path: Dir.pwd }
|
30
|
+
stdin_options = stdin_options.merge(viewport_options: @viewport_options) if @viewport_options
|
31
|
+
stdin_options = stdin_options.merge(emulate_media: @emulate_media) if @emulate_media
|
32
|
+
file = Tempfile.new('input_html')
|
33
|
+
file.write(@html)
|
34
|
+
file.close
|
35
|
+
pdf_output, status = Open3.capture2(MAKE_PDF_COMMAND, file.path, stdin_data: stdin_options.to_json)
|
36
|
+
file.unlink
|
37
|
+
unless status.success?
|
38
|
+
raise 'There was an unknown error running node to create the pdf. Check your logs for output that might assist in debugging.'
|
39
|
+
end
|
40
|
+
unless pdf_output
|
41
|
+
raise 'There was an error creating the temporary file used to pass the HTML to node.'
|
42
|
+
end
|
43
|
+
pdf_output
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
# Use a new API that uses method chaining to configure the PDF and that queues up strings to put in a js file to run puppeteer
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pdfgen
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Fox
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.11'
|
27
|
+
description: Using Puppeteer and headless Chrome, generate PDFs from HTML without
|
28
|
+
needing to install wkhtmltopdf.
|
29
|
+
email: romaimperator@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- VERSION
|
38
|
+
- lib/javascript_bin/make_pdf.js
|
39
|
+
- lib/pdfgen.rb
|
40
|
+
homepage: https://github.com/romaimperator/pdfgen
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.6.14
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: Generate PDFs using Puppeteer and headless Chrome
|
64
|
+
test_files: []
|