pdf_gem 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +141 -0
- data/Rakefile +28 -0
- data/lib/pdf_gem.rb +38 -0
- data/lib/pdf_gem/railtie.rb +4 -0
- data/lib/pdf_gem/version.rb +3 -0
- data/lib/pdf_generator.js +24 -0
- data/lib/renderer.rb +11 -0
- data/lib/tmp/1zlgz1o22datkhezl9dp5j803ocavkyc6wtpmbiz.html +13 -0
- data/lib/tmp/512m1i0cteoydwdoju0e6qdjbm0qttswr711igtr.html +13 -0
- data/lib/tmp/evziomgjof3tg36lz6t6x429rbni8ipjefmxjilq.html +13 -0
- data/lib/tmp/j56oa22hog5nnt1wwebf1xv5nbieeitn5l1nshws.html +13 -0
- data/lib/tmp/kjn1jl9z22l3kdl1vgjdezivta47rlgos30wxx1q.html +13 -0
- data/lib/tmp/nllfaomxczkbqfj470n6r478vva66thutkj7kymn.html +13 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 10af893a52c091022ff0544911751cc04cd09dbd0ce2dd45c3aca4cee493affd
|
4
|
+
data.tar.gz: ec3e961a45fa96c8fbbe45e1d671f84367600b00f1a4dfd0cfc74c50067b3e7e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fd9be56c9327a4894059cb4a174b485d33c1c1c3c8ab744d5b788abd663311769fab3f87f37cc1c60d2c7f6749640b39308330b3840abb1fbb9d3e64ae3892ea
|
7
|
+
data.tar.gz: caaf28b437e0ba053b8bed3271619d1561ce5cbbc07ec2f93a034b9a8be614498099bfe7613d5bc84730580048a2bae526bd0fe645d94292ddb8e52b12fdb5df
|
data/README.md
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
# PdfGem
|
2
|
+
This is a gem for generating PDF from HTML, and is rendered from Chromium Browser
|
3
|
+
|
4
|
+
## Prerequisites
|
5
|
+
This package depends on node.js and puppeteer run this command for installing them:
|
6
|
+
|
7
|
+
```bash
|
8
|
+
$ npm install -g node puppeteer
|
9
|
+
```
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'pdf_gem'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
```bash
|
20
|
+
$ bundle install
|
21
|
+
```
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
```bash
|
25
|
+
$ gem install pdf_gem
|
26
|
+
```
|
27
|
+
|
28
|
+
You may need to add
|
29
|
+
```ruby
|
30
|
+
Mime::Type.register "application/pdf", :pdf
|
31
|
+
```
|
32
|
+
to config/initializers/mime_types.rb
|
33
|
+
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
### Usage from controller
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class TestController < ApplicationController
|
41
|
+
def show
|
42
|
+
respond_to do |format|
|
43
|
+
format.html
|
44
|
+
format.pdf do
|
45
|
+
render pdf: "template", options #look options section
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
### Available lib methods
|
53
|
+
|
54
|
+
|
55
|
+
This method generates pdf from url
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
PdfGem.pdf_from_url(options)
|
59
|
+
```
|
60
|
+
|
61
|
+
This method generates pdf from html string
|
62
|
+
```ruby
|
63
|
+
PdfGem.pdf_from_string(options)
|
64
|
+
```
|
65
|
+
|
66
|
+
|
67
|
+
## Options
|
68
|
+
|
69
|
+
- `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
|
+
- `disposition` <[string]> (Use only for controller render) Disposition string (inline/attachment).
|
73
|
+
- `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
|
75
|
+
- `scale` <[number]> Scale of the webpage rendering. Defaults to `1`. Scale amount must be between 0.1 and 2.
|
76
|
+
- `displayHeaderFooter` <[boolean]> Display header and footer. Defaults to `false`.
|
77
|
+
- `headerTemplate` <[string]> HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them:
|
78
|
+
- `date` formatted print date
|
79
|
+
- `title` document title
|
80
|
+
- `url` document location
|
81
|
+
- `pageNumber` current page number
|
82
|
+
- `totalPages` total pages in the document
|
83
|
+
- `footerTemplate` <[string]> HTML template for the print footer. Should use the same format as the `headerTemplate`.
|
84
|
+
- `printBackground` <[boolean]> Print background graphics. Defaults to `false`.
|
85
|
+
- `landscape` <[boolean]> Paper orientation. Defaults to `false`.
|
86
|
+
- `pageRanges` <[string]> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
|
87
|
+
- `format` <[string]> Paper format. If set, takes priority over `width` or `height` options. Defaults to 'Letter'.
|
88
|
+
- `width` <[string]|[number]> Paper width, accepts values labeled with units.
|
89
|
+
- `height` <[string]|[number]> Paper height, accepts values labeled with units.
|
90
|
+
- `margin` <[Object]> Paper margins, defaults to none.
|
91
|
+
- `top` <[string]|[number]> Top margin, accepts values labeled with units.
|
92
|
+
- `right` <[string]|[number]> Right margin, accepts values labeled with units.
|
93
|
+
- `bottom` <[string]|[number]> Bottom margin, accepts values labeled with units.
|
94
|
+
- `left` <[string]|[number]> Left margin, accepts values labeled with units.
|
95
|
+
- `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
|
+
|
97
|
+
> **NOTE** Generating a pdf is currently only supported in Chrome headless.
|
98
|
+
|
99
|
+
> **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
|
+
|
101
|
+
|
102
|
+
|
103
|
+
> Example of working footer template:
|
104
|
+
```html
|
105
|
+
<div id="footer-template" style="font-size:10px !important; color:#808080; padding-left:10px">
|
106
|
+
<span class="date"></span>
|
107
|
+
<span class="title"></span>
|
108
|
+
<span class="url"></span>
|
109
|
+
<span class="pageNumber"></span>
|
110
|
+
<span class="totalPages"></span>
|
111
|
+
</div>
|
112
|
+
```
|
113
|
+
|
114
|
+
The `width`, `height`, and `margin` options accept values labeled with units. Unlabeled values are treated as pixels.
|
115
|
+
|
116
|
+
All possible units are:
|
117
|
+
- `px` - pixel
|
118
|
+
- `in` - inch
|
119
|
+
- `cm` - centimeter
|
120
|
+
- `mm` - millimeter
|
121
|
+
|
122
|
+
The `format` options are:
|
123
|
+
- `Letter`: 8.5in x 11in
|
124
|
+
- `Legal`: 8.5in x 14in
|
125
|
+
- `Tabloid`: 11in x 17in
|
126
|
+
- `Ledger`: 17in x 11in
|
127
|
+
- `A0`: 33.1in x 46.8in
|
128
|
+
- `A1`: 23.4in x 33.1in
|
129
|
+
- `A2`: 16.54in x 23.4in
|
130
|
+
- `A3`: 11.7in x 16.54in
|
131
|
+
- `A4`: 8.27in x 11.7in
|
132
|
+
- `A5`: 5.83in x 8.27in
|
133
|
+
- `A6`: 4.13in x 5.83in
|
134
|
+
|
135
|
+
> **NOTE** `headerTemplate` and `footerTemplate` markup have the following limitations:
|
136
|
+
> 1. Script tags inside templates are not evaluated.
|
137
|
+
> 2. Page styles are not visible inside templates.
|
138
|
+
|
139
|
+
|
140
|
+
## License
|
141
|
+
The gem is available as open source under the terms of the [Apach e2.0 License](https://opensource.org/licenses/Apache-2.0).
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = 'rdoc'
|
13
|
+
rdoc.title = 'PdfGem'
|
14
|
+
rdoc.options << '--line-numbers'
|
15
|
+
rdoc.rdoc_files.include('README.md')
|
16
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'bundler/gem_tasks'
|
20
|
+
require 'rake/testtask'
|
21
|
+
|
22
|
+
Rake::TestTask.new(:test) do |t|
|
23
|
+
t.libs << 'test'
|
24
|
+
t.pattern = 'test/**/*_test.rb'
|
25
|
+
t.verbose = false
|
26
|
+
end
|
27
|
+
|
28
|
+
task default: :test
|
data/lib/pdf_gem.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "pdf_gem/railtie"
|
2
|
+
require 'open3'
|
3
|
+
require 'base64'
|
4
|
+
require 'renderer'
|
5
|
+
|
6
|
+
module PdfGem
|
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?)
|
10
|
+
if(params[:destination].present?)
|
11
|
+
self.save_to_file(params[:destination], stdout)
|
12
|
+
else
|
13
|
+
return Base64.decode64(stdout)
|
14
|
+
end
|
15
|
+
else
|
16
|
+
raise stderr.present? ? stderr : "error"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.pdf_from_string(params)
|
21
|
+
tmp = File.join(File.dirname(__FILE__), 'tmp')
|
22
|
+
Dir.mkdir(tmp) unless File.exist?(tmp)
|
23
|
+
html_file = File.join(tmp, "#{rand(36**40).to_s(36)}.html")
|
24
|
+
File.open(html_file, "w+") do |f|
|
25
|
+
f.write(params[:html])
|
26
|
+
end
|
27
|
+
params[:url] = "file:#{html_file}"
|
28
|
+
result = self.pdf_from_url(params)
|
29
|
+
File.delete(html_file) if File.exist?(html_file)
|
30
|
+
return result
|
31
|
+
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
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
const puppeteer = require('puppeteer');
|
2
|
+
const fs = require('fs');
|
3
|
+
const crypto = require('crypto');
|
4
|
+
const path = require('path');
|
5
|
+
(async () => {
|
6
|
+
const filename = path.join(path.dirname(__filename), 'tmp', crypto.randomBytes(40).toString('hex')+ '.pdf');
|
7
|
+
const browser = await puppeteer.launch();
|
8
|
+
try{
|
9
|
+
const params = {...JSON.parse(Buffer.from(process.argv.slice(2)[0], 'base64').toString('utf-8')), ...{path: filename}};
|
10
|
+
const page = await browser.newPage();
|
11
|
+
await page.goto(params.url);
|
12
|
+
await page.pdf(params);
|
13
|
+
console.log(Buffer.from(fs.readFileSync(filename, 'binary'), 'binary').toString('base64'))
|
14
|
+
}
|
15
|
+
catch(e){
|
16
|
+
console.error(e)
|
17
|
+
process.exit(1);
|
18
|
+
}
|
19
|
+
finally{
|
20
|
+
fs.unlinkSync(filename)
|
21
|
+
await browser.close();
|
22
|
+
process.exit(0);
|
23
|
+
}
|
24
|
+
})();
|
data/lib/renderer.rb
ADDED
@@ -0,0 +1,11 @@
|
|
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
|
7
|
+
|
8
|
+
if Mime::Type.lookup_by_extension(:pdf).nil?
|
9
|
+
Mime::Type.register('application/pdf', :pdf)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,13 @@
|
|
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>
|
@@ -0,0 +1,13 @@
|
|
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>
|
@@ -0,0 +1,13 @@
|
|
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>
|
@@ -0,0 +1,13 @@
|
|
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>
|
@@ -0,0 +1,13 @@
|
|
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>
|
@@ -0,0 +1,13 @@
|
|
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>
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pdf_gem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Blasina
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-01-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Gem based on chrome browser
|
42
|
+
email:
|
43
|
+
- blzk100@gmailcom
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- lib/pdf_gem.rb
|
51
|
+
- lib/pdf_gem/railtie.rb
|
52
|
+
- lib/pdf_gem/version.rb
|
53
|
+
- lib/pdf_generator.js
|
54
|
+
- 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
|
+
homepage: https://github.com/blasko03/pdf_gem
|
62
|
+
licenses:
|
63
|
+
- Apache-2.0
|
64
|
+
metadata:
|
65
|
+
allowed_push_host: https://rubygems.org
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubygems_version: 3.1.2
|
82
|
+
signing_key:
|
83
|
+
specification_version: 4
|
84
|
+
summary: Gem for creating pdf from html
|
85
|
+
test_files: []
|