pdf_gem 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c618dfc11c4ba373b60e66c30a723f1f45b17f9b4437e5ea2b1458ac6dd3a20a
4
+ data.tar.gz: e7b54132e60927e11ecbcafe632286f86cd7b370a960b68a8a3dcbdfb392d143
5
+ SHA512:
6
+ metadata.gz: 2e0e27de69f25c6e63c7e672deecdcdfc13b98c5ea16008131852a9655d8fbc6a10ac933826dd3dea705bb125000abab9c3e2592e772bba10cc325e26e3617ba
7
+ data.tar.gz: 513d9320d4ac3f1410cac31ed22bd84fd70bbb7464e4eb723edbd7d843fad378f58c7f0bc7d7f47004175a7e3f4349553d413126ca51bae6e634300d78bd4bfe
@@ -0,0 +1,147 @@
1
+ # PdfGem
2
+ This is a gem for converting HTML to 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
+ > **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
+
54
+ ### Available lib methods
55
+
56
+
57
+ This method generates pdf from url
58
+
59
+ ```ruby
60
+ PdfGem.pdf_from_url(options)
61
+ ```
62
+
63
+ This method generates pdf from html string
64
+ ```ruby
65
+ PdfGem.pdf_from_string(options)
66
+ ```
67
+
68
+
69
+ ## Options
70
+
71
+ - `options` <[Object]> Options object which might have the following properties:
72
+ - `url` <[string]> (Used only for PdfGem.pdf_from_url) This is the url to render.
73
+ - `html` <[string]> (Used only for PdfGem.pdf_from_string) This is the html string to render.
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).
76
+ - `filename` <[string]> (Use only for controller render) Filename of the file.
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
78
+ - `scale` <[number]> Scale of the webpage rendering. Defaults to `1`. Scale amount must be between 0.1 and 2.
79
+ - `displayHeaderFooter` <[boolean]> Display header and footer. Defaults to `false`.
80
+ - `headerTemplate` <[string]> HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them:
81
+ - `date` formatted print date
82
+ - `title` document title
83
+ - `url` document location
84
+ - `pageNumber` current page number
85
+ - `totalPages` total pages in the document
86
+ - `footerTemplate` <[string]> HTML template for the print footer. Should use the same format as the `headerTemplate`.
87
+ - `printBackground` <[boolean]> Print background graphics. Defaults to `false`.
88
+ - `landscape` <[boolean]> Paper orientation. Defaults to `false`.
89
+ - `pageRanges` <[string]> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
90
+ - `format` <[string]> Paper format. If set, takes priority over `width` or `height` options. Defaults to 'Letter'.
91
+ - `width` <[string]|[number]> Paper width, accepts values labeled with units.
92
+ - `height` <[string]|[number]> Paper height, accepts values labeled with units.
93
+ - `margin` <[Object]> Paper margins, defaults to none.
94
+ - `top` <[string]|[number]> Top margin, accepts values labeled with units.
95
+ - `right` <[string]|[number]> Right margin, accepts values labeled with units.
96
+ - `bottom` <[string]|[number]> Bottom margin, accepts values labeled with units.
97
+ - `left` <[string]|[number]> Left margin, accepts values labeled with units.
98
+ - `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.
99
+
100
+ > **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.
101
+
102
+
103
+
104
+ > Example of working footer template:
105
+ ```html
106
+ <div id="footer-template" style="font-size:10px !important; color:#808080; padding-left:10px">
107
+ <span class="date"></span>
108
+ <span class="title"></span>
109
+ <span class="url"></span>
110
+ <span class="pageNumber"></span>
111
+ <span class="totalPages"></span>
112
+ </div>
113
+ ```
114
+
115
+ The `width`, `height`, and `margin` options accept values labeled with units. Unlabeled values are treated as pixels.
116
+
117
+ All possible units are:
118
+ - `px` - pixel
119
+ - `in` - inch
120
+ - `cm` - centimeter
121
+ - `mm` - millimeter
122
+
123
+ The `format` options are:
124
+ - `Letter`: 8.5in x 11in
125
+ - `Legal`: 8.5in x 14in
126
+ - `Tabloid`: 11in x 17in
127
+ - `Ledger`: 17in x 11in
128
+ - `A0`: 33.1in x 46.8in
129
+ - `A1`: 23.4in x 33.1in
130
+ - `A2`: 16.54in x 23.4in
131
+ - `A3`: 11.7in x 16.54in
132
+ - `A4`: 8.27in x 11.7in
133
+ - `A5`: 5.83in x 8.27in
134
+ - `A6`: 4.13in x 5.83in
135
+
136
+ > **NOTE** `headerTemplate` and `footerTemplate` markup have the following limitations:
137
+ > 1. Script tags inside templates are not evaluated.
138
+ > 2. Page styles are not visible inside templates.
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.
145
+
146
+ ## License
147
+ The gem is available as open source under the terms of the [Apach e2.0 License](https://opensource.org/licenses/Apache-2.0).
@@ -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
@@ -0,0 +1,34 @@
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, 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
+ if(params[:destination].present?)
11
+ FileUtils.mv(stdout, params[:destination])
12
+ else
13
+ res = File.open(stdout, 'rb').read
14
+ File.delete(stdout) if File.exist?(stdout)
15
+ return res
16
+ end
17
+ else
18
+ raise stderr.present? ? stderr : "error"
19
+ end
20
+ end
21
+
22
+ def self.pdf_from_string(params)
23
+ tmp = File.join(File.dirname(__FILE__), 'tmp')
24
+ Dir.mkdir(tmp) unless File.exist?(tmp)
25
+ html_file = File.join(tmp, "#{rand(36**40).to_s(36)}.html")
26
+ File.open(html_file, "w+") do |f|
27
+ f.write(params[:html])
28
+ end
29
+ params[:url] = "file:#{html_file}"
30
+ result = self.pdf_from_url(params)
31
+ File.delete(html_file) if File.exist?(html_file)
32
+ return result
33
+ end
34
+ end
@@ -0,0 +1,4 @@
1
+ module PdfGem
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module PdfGem
2
+ VERSION = '1.0.3'
3
+ end
@@ -0,0 +1,26 @@
1
+ const puppeteer = require('puppeteer');
2
+ const fs = require('fs');
3
+ const crypto = require('crypto');
4
+ const path = require('path');
5
+
6
+ (async () => {
7
+
8
+ var stdinBuffer = fs.readFileSync(0);
9
+ const filename = path.join(path.dirname(__filename), 'tmp', crypto.randomBytes(40).toString('hex')+ '.pdf');
10
+ const browser = await puppeteer.launch();
11
+ try{
12
+ const params = {...JSON.parse(Buffer.from(stdinBuffer.toString(), 'base64').toString('utf-8')), ...{path: filename}};
13
+ const page = await browser.newPage();
14
+ await page.goto(params.url, {waitUntil: 'networkidle2'});
15
+ await page.pdf(params);
16
+ process.stdout.write(filename)
17
+ }
18
+ catch(e){
19
+ console.error(e)
20
+ process.exit(1);
21
+ }
22
+ finally{
23
+ await browser.close();
24
+ process.exit(0);
25
+ }
26
+ })();
@@ -0,0 +1,17 @@
1
+ module PdfGem
2
+ ActionController::Renderers.add :pdf do |template, options|
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] )
11
+ send_data PdfGem::pdf_from_string(params), type: Mime[:pdf], disposition: (params[:disposition].present? ? params[:disposition] : 'inline'), :filename => options[:filename]
12
+ end
13
+
14
+ if Mime::Type.lookup_by_extension(:pdf).nil?
15
+ Mime::Type.register('application/pdf', :pdf)
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pdf_gem
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Blasina
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-27 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
+ homepage: https://github.com/blasko03/pdf_gem
57
+ licenses:
58
+ - Apache-2.0
59
+ metadata:
60
+ allowed_push_host: https://rubygems.org
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubygems_version: 3.1.2
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Gem for creating pdf from html
80
+ test_files: []