puppet_pdf 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +20 -3
- data/javascript/pdf_from_html.js +50 -0
- data/javascript/{pdf.js → pdf_from_url.js} +3 -7
- data/lib/puppet_pdf/pdf_creator.rb +5 -4
- data/lib/puppet_pdf/version.rb +1 -1
- data/lib/puppet_pdf.rb +9 -2
- data/package.json +2 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d554307f30fb1566b92ab4b53d7a498db6ed05b4a4518cb3c22ad052edd92c2c
|
4
|
+
data.tar.gz: bdab5ae4f0937b991daf22a0d9833e816eeff762b2309109a7f370f02a38fdf0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e0fa0d135d947c2ab9956976f98d1fb0571f85cab2388d75c7b61b9c1e3b8a957aa4a440a7bc0717bc4c2576c0d4ac666bdaf7c884bfc674f53d1d7c20088f6
|
7
|
+
data.tar.gz: 0573cfc7f2004a5f2a61dcb1d8483494f738c475851b7daca4cf1f2481ecd3a3ebc71cf3f25e3ab4efd5fd79aafb76150e0ce72608ebcd364419c7453711d0ee
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
[![Build Status](https://travis-ci.org/andersonfernandes/puppet_pdf.svg?branch=master)](https://travis-ci.org/andersonfernandes/puppet_pdf)
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/puppet_pdf.svg)](https://badge.fury.io/rb/puppet_pdf) [![Build Status](https://travis-ci.org/andersonfernandes/puppet_pdf.svg?branch=master)](https://travis-ci.org/andersonfernandes/puppet_pdf) [![Maintainability](https://api.codeclimate.com/v1/badges/d7347b2b2ab3ffdc4668/maintainability)](https://codeclimate.com/github/andersonfernandes/puppet_pdf/maintainability)
|
2
2
|
|
3
3
|
# PuppetPdf
|
4
4
|
|
5
|
-
PuppetPdf is a lib that wraps [Google Puppeteer](https://pptr.dev/) pdf generation to be used with Rails.
|
5
|
+
PuppetPdf is a lib that wraps [Google Puppeteer](https://pptr.dev/) pdf generation method to be used with Rails.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -20,13 +20,30 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
$ gem install puppet_pdf
|
22
22
|
|
23
|
+
Run this task to install some dependencies:
|
24
|
+
|
25
|
+
$ rails puppet_pdf:install_dependencies
|
26
|
+
|
23
27
|
## Usage
|
24
28
|
|
29
|
+
In any part of the application you can call:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
PuppetPdf.pdf_from_url(url, options)
|
33
|
+
```
|
34
|
+
|
35
|
+
to generate a pdf from a url. And:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
PuppetPdf.pdf_from_html(html, options)
|
39
|
+
```
|
25
40
|
|
41
|
+
to generate a pdf from an html.
|
26
42
|
|
27
|
-
## Development
|
28
43
|
|
44
|
+
***For now only the output_path option is available to use.***
|
29
45
|
|
46
|
+
And a pdf of the given url will be generated, and the path to this file is going to be returned.
|
30
47
|
|
31
48
|
## Contributing
|
32
49
|
|
@@ -0,0 +1,50 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
const puppeteer = require('puppeteer');
|
4
|
+
const fs = require('fs')
|
5
|
+
const path = require('path')
|
6
|
+
|
7
|
+
function getCurrentTimeFormated() {
|
8
|
+
const date = new Date();
|
9
|
+
const dateFormatted = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`
|
10
|
+
|
11
|
+
return `${dateFormatted}, ${date.toLocaleTimeString('en')}`
|
12
|
+
}
|
13
|
+
|
14
|
+
var access = fs.createWriteStream(path.resolve(__dirname, '/tmp/puppeteer.log'), { 'flags': 'a' });
|
15
|
+
// Redirect stdout/stderr to puppeteer.log file
|
16
|
+
process.stdout.write = process.stderr.write = access.write.bind(access);
|
17
|
+
|
18
|
+
const pdf_from_html = async () => {
|
19
|
+
let browser;
|
20
|
+
try {
|
21
|
+
const browser = await puppeteer.launch({
|
22
|
+
headless: true,
|
23
|
+
pipe: true,
|
24
|
+
args: ['--disable-gpu', '--full-memory-crash-report', '--unlimited-storage',
|
25
|
+
'--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
|
26
|
+
});
|
27
|
+
const page = await browser.newPage();
|
28
|
+
await page.setContent(process.argv[2])
|
29
|
+
await page.waitFor(1000);
|
30
|
+
await page.pdf({
|
31
|
+
path: process.argv[3],
|
32
|
+
format: 'A4',
|
33
|
+
margin: { top: 36, right: 36, bottom: 45, left: 36 },
|
34
|
+
displayHeaderFooter: true,
|
35
|
+
headerTemplate: "<div></div>",
|
36
|
+
footerTemplate: "<div></div>"
|
37
|
+
});
|
38
|
+
} catch (err) {
|
39
|
+
const formatedMessage = `\n ${err.message} at: ${getCurrentTimeFormated()}`;
|
40
|
+
access.write(formatedMessage)
|
41
|
+
} finally {
|
42
|
+
if (browser) {
|
43
|
+
browser.close();
|
44
|
+
}
|
45
|
+
process.exit();
|
46
|
+
}
|
47
|
+
};
|
48
|
+
|
49
|
+
pdf_from_html();
|
50
|
+
|
@@ -15,7 +15,7 @@ var access = fs.createWriteStream(path.resolve(__dirname, '/tmp/puppeteer.log'),
|
|
15
15
|
// Redirect stdout/stderr to puppeteer.log file
|
16
16
|
process.stdout.write = process.stderr.write = access.write.bind(access);
|
17
17
|
|
18
|
-
const
|
18
|
+
const pdf_from_url = async () => {
|
19
19
|
let browser;
|
20
20
|
try {
|
21
21
|
const browser = await puppeteer.launch({
|
@@ -33,11 +33,7 @@ const createPdf = async () => {
|
|
33
33
|
margin: { top: 36, right: 36, bottom: 45, left: 36 },
|
34
34
|
displayHeaderFooter: true,
|
35
35
|
headerTemplate: "<div></div>",
|
36
|
-
footerTemplate:
|
37
|
-
<footer style="width: 100%; font-size: 8px; font-family: Helvetica; color: #DCDCDC">
|
38
|
-
<div style="margin-left: 36px; float: left">Generated at: ${getCurrentTimeFormated()}</div>
|
39
|
-
</footer>
|
40
|
-
`
|
36
|
+
footerTemplate: "<div></div>"
|
41
37
|
});
|
42
38
|
} catch (err) {
|
43
39
|
const formatedMessage = `\n ${err.message} at: ${getCurrentTimeFormated()}`;
|
@@ -50,5 +46,5 @@ const createPdf = async () => {
|
|
50
46
|
}
|
51
47
|
};
|
52
48
|
|
53
|
-
|
49
|
+
pdf_from_url();
|
54
50
|
|
@@ -5,21 +5,22 @@ module PuppetPdf
|
|
5
5
|
class PdfCreator
|
6
6
|
include ::Utils::YarnWrapper
|
7
7
|
|
8
|
-
def initialize(
|
8
|
+
def initialize(task, source, options = {})
|
9
9
|
validate_yarn_installation
|
10
10
|
|
11
|
-
@
|
11
|
+
@task = task
|
12
|
+
@source = source
|
12
13
|
@output_path = options.fetch(:output_path, default_output_path)
|
13
14
|
end
|
14
15
|
|
15
16
|
def call
|
16
|
-
run_yarn(
|
17
|
+
run_yarn(task, source, output_path)
|
17
18
|
output_path
|
18
19
|
end
|
19
20
|
|
20
21
|
private
|
21
22
|
|
22
|
-
attr_reader :
|
23
|
+
attr_reader :task, :source, :output_path
|
23
24
|
|
24
25
|
def default_output_path
|
25
26
|
file = Tempfile.new(["puppet_pdf_#{Time.now.to_i}", '.pdf'])
|
data/lib/puppet_pdf/version.rb
CHANGED
data/lib/puppet_pdf.rb
CHANGED
@@ -4,7 +4,14 @@ require 'puppet_pdf/railtie' if defined?(Rails)
|
|
4
4
|
|
5
5
|
module PuppetPdf
|
6
6
|
def self.pdf_from_url(url, options = {})
|
7
|
-
pdf_creator
|
8
|
-
|
7
|
+
pdf_creator(:pdf_from_url, url, options).call
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.pdf_from_html(html, options = {})
|
11
|
+
pdf_creator(:pdf_from_html, html, options).call
|
12
|
+
end
|
13
|
+
|
14
|
+
private_class_method def self.pdf_creator(task, source, options)
|
15
|
+
::PuppetPdf::PdfCreator.new(task, source, options)
|
9
16
|
end
|
10
17
|
end
|
data/package.json
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: puppet_pdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anderson Fernandes
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-07-
|
11
|
+
date: 2019-07-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -127,7 +127,8 @@ files:
|
|
127
127
|
- Rakefile
|
128
128
|
- bin/console
|
129
129
|
- bin/setup
|
130
|
-
- javascript/
|
130
|
+
- javascript/pdf_from_html.js
|
131
|
+
- javascript/pdf_from_url.js
|
131
132
|
- lib/puppet_pdf.rb
|
132
133
|
- lib/puppet_pdf/pdf_creator.rb
|
133
134
|
- lib/puppet_pdf/railtie.rb
|