pdf_gem 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +139 -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 +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6c6ee1c453af223c06cda4f2785f8a69f02da7c3e9f4c8116be1b40b08165e60
|
4
|
+
data.tar.gz: 43be06726e1ca3e06b0442a810e231f9abd28c12fb2e1dfd3ad29abe3e251d2f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 28635eb2f795ce7cd35cda838db9546a612b2d0fae5186cb277105e8d3bfd3100078ba8734948e9395d1797d6a06b9e8d25375a2a8a142d070c6973ebefd5b9c
|
7
|
+
data.tar.gz: cbb3d3bc77429cf93b09b89518c1733b304539dee62199a7cd005e0b87a5d1b5d5bddadd747b15e744a7a6de03a71a7aca2cf234f1e57af582fbba2a0bdf4ceb
|
data/README.md
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
# PdfGem
|
2
|
+
This is a gem for converting HTML in PDF, the rendering engine is 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]> (Used only for PdfGem.pdf_from_url) This is the url to render.
|
71
|
+
- `html` <[string]> (Used 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]> (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
|
+
- `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** 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.
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
> Example of working footer template:
|
102
|
+
```html
|
103
|
+
<div id="footer-template" style="font-size:10px !important; color:#808080; padding-left:10px">
|
104
|
+
<span class="date"></span>
|
105
|
+
<span class="title"></span>
|
106
|
+
<span class="url"></span>
|
107
|
+
<span class="pageNumber"></span>
|
108
|
+
<span class="totalPages"></span>
|
109
|
+
</div>
|
110
|
+
```
|
111
|
+
|
112
|
+
The `width`, `height`, and `margin` options accept values labeled with units. Unlabeled values are treated as pixels.
|
113
|
+
|
114
|
+
All possible units are:
|
115
|
+
- `px` - pixel
|
116
|
+
- `in` - inch
|
117
|
+
- `cm` - centimeter
|
118
|
+
- `mm` - millimeter
|
119
|
+
|
120
|
+
The `format` options are:
|
121
|
+
- `Letter`: 8.5in x 11in
|
122
|
+
- `Legal`: 8.5in x 14in
|
123
|
+
- `Tabloid`: 11in x 17in
|
124
|
+
- `Ledger`: 17in x 11in
|
125
|
+
- `A0`: 33.1in x 46.8in
|
126
|
+
- `A1`: 23.4in x 33.1in
|
127
|
+
- `A2`: 16.54in x 23.4in
|
128
|
+
- `A3`: 11.7in x 16.54in
|
129
|
+
- `A4`: 8.27in x 11.7in
|
130
|
+
- `A5`: 5.83in x 8.27in
|
131
|
+
- `A6`: 4.13in x 5.83in
|
132
|
+
|
133
|
+
> **NOTE** `headerTemplate` and `footerTemplate` markup have the following limitations:
|
134
|
+
> 1. Script tags inside templates are not evaluated.
|
135
|
+
> 2. Page styles are not visible inside templates.
|
136
|
+
|
137
|
+
|
138
|
+
## License
|
139
|
+
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,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pdf_gem
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.2
|
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: This is a gem for converting HTML in PDF, the rendering engine is Chromium
|
42
|
+
Browser
|
43
|
+
email:
|
44
|
+
- blzk100@gmailcom
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/pdf_gem.rb
|
52
|
+
- lib/pdf_gem/railtie.rb
|
53
|
+
- lib/pdf_gem/version.rb
|
54
|
+
- lib/pdf_generator.js
|
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
|
+
homepage: https://github.com/blasko03/pdf_gem
|
63
|
+
licenses:
|
64
|
+
- Apache-2.0
|
65
|
+
metadata:
|
66
|
+
allowed_push_host: https://rubygems.org
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubygems_version: 3.1.2
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Gem for creating pdf from html
|
86
|
+
test_files: []
|