pdf_gem 1.0.8

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: cef9f7203d05d0d116b4e5294f4b7e2cd8682d2926fd1fd84373eb096a9b5ea9
4
+ data.tar.gz: 00f5792d27ce1726a3c182a9426bcaf51b6aba78e3c4d8255031abd693b0c642
5
+ SHA512:
6
+ metadata.gz: 6a758ed6a2a8130eefd19c76a11fa56ae25789a5aff73d0ea7278644dc96041081a411f9a8f63c5cfbf6ab40ab28bd6cfb1845ccabf266a6a016778922218447
7
+ data.tar.gz: bb4033abcfb6ddb00357ec2e6e7e6104edabd2555d459be36fd785dabec197c95601f7a835c34ab646316e180dcec6f4ca0979eb90026bced6a1777aaa2f5bcb
@@ -0,0 +1,156 @@
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
+ Add this lines to `application.rb`
29
+ ```ruby
30
+ ActionController::Renderers.add :pdf do |template, options|
31
+ PdfGem.renderer(template, options)
32
+ end
33
+ ```
34
+ > **NOTE** You can change the :pdf in some other name if you have conflicts with other similar libraries
35
+
36
+
37
+ You may need to add
38
+ ```ruby
39
+ Mime::Type.register "application/pdf", :pdf
40
+ ```
41
+ to `config/initializers/mime_types.rb`
42
+
43
+
44
+ ## Usage
45
+
46
+ ### Usage from controller
47
+
48
+ ```ruby
49
+ class TestController < ApplicationController
50
+ def show
51
+ respond_to do |format|
52
+ format.html
53
+ format.pdf do
54
+ render pdf: "template", options #look options section
55
+ end
56
+ end
57
+ end
58
+ end
59
+ ```
60
+
61
+ > **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
62
+
63
+ ### Available lib methods
64
+
65
+
66
+ This method generates pdf from url
67
+
68
+ ```ruby
69
+ PdfGem.pdf_from_url(options)
70
+ ```
71
+
72
+ This method generates pdf from html string
73
+ ```ruby
74
+ PdfGem.pdf_from_string(options)
75
+ ```
76
+
77
+
78
+ ## Options
79
+
80
+ - `options` <[Object]> Options object which might have the following properties:
81
+ - `url` <[string]> (Used only for PdfGem.pdf_from_url) This is the url to render.
82
+ - `html` <[string]> (Used only for PdfGem.pdf_from_string) This is the html string to render.
83
+ - `disposition` <[string]> (Use only for controller render) Disposition string (inline/attachment).
84
+ - `formats` <[string]> (Use only for controller render) Force to load view of a particular format (pdf, html, xml).
85
+ - `filename` <[string]> (Use only for controller render) Filename of the file.
86
+ - `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
87
+ - `scale` <[number]> Scale of the webpage rendering. Defaults to `1`. Scale amount must be between 0.1 and 2.
88
+ - `displayHeaderFooter` <[boolean]> Display header and footer. Defaults to `false`.
89
+ - `headerTemplate` <[string]> HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them:
90
+ - `date` formatted print date
91
+ - `title` document title
92
+ - `url` document location
93
+ - `pageNumber` current page number
94
+ - `totalPages` total pages in the document
95
+ - `footerTemplate` <[string]> HTML template for the print footer. Should use the same format as the `headerTemplate`.
96
+ - `printBackground` <[boolean]> Print background graphics. Defaults to `false`.
97
+ - `landscape` <[boolean]> Paper orientation. Defaults to `false`.
98
+ - `pageRanges` <[string]> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
99
+ - `format` <[string]> Paper format. If set, takes priority over `width` or `height` options. Defaults to 'Letter'.
100
+ - `width` <[string]|[number]> Paper width, accepts values labeled with units.
101
+ - `height` <[string]|[number]> Paper height, accepts values labeled with units.
102
+ - `margin` <[Object]> Paper margins, defaults to none.
103
+ - `top` <[string]|[number]> Top margin, accepts values labeled with units.
104
+ - `right` <[string]|[number]> Right margin, accepts values labeled with units.
105
+ - `bottom` <[string]|[number]> Bottom margin, accepts values labeled with units.
106
+ - `left` <[string]|[number]> Left margin, accepts values labeled with units.
107
+ - `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.
108
+
109
+ > **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.
110
+
111
+
112
+
113
+ > Example of working footer template:
114
+ ```html
115
+ <div id="footer-template" style="font-size:10px !important; color:#808080; padding-left:10px">
116
+ <span class="date"></span>
117
+ <span class="title"></span>
118
+ <span class="url"></span>
119
+ <span class="pageNumber"></span>
120
+ <span class="totalPages"></span>
121
+ </div>
122
+ ```
123
+
124
+ The `width`, `height`, and `margin` options accept values labeled with units. Unlabeled values are treated as pixels.
125
+
126
+ All possible units are:
127
+ - `px` - pixel
128
+ - `in` - inch
129
+ - `cm` - centimeter
130
+ - `mm` - millimeter
131
+
132
+ The `format` options are:
133
+ - `Letter`: 8.5in x 11in
134
+ - `Legal`: 8.5in x 14in
135
+ - `Tabloid`: 11in x 17in
136
+ - `Ledger`: 17in x 11in
137
+ - `A0`: 33.1in x 46.8in
138
+ - `A1`: 23.4in x 33.1in
139
+ - `A2`: 16.54in x 23.4in
140
+ - `A3`: 11.7in x 16.54in
141
+ - `A4`: 8.27in x 11.7in
142
+ - `A5`: 5.83in x 8.27in
143
+ - `A6`: 4.13in x 5.83in
144
+
145
+ > **NOTE** `headerTemplate` and `footerTemplate` markup have the following limitations:
146
+ > 1. Script tags inside templates are not evaluated.
147
+ > 2. Page styles are not visible inside templates.
148
+
149
+ ## Troubleshooting
150
+ 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`
151
+
152
+ ## Contributing
153
+ You are welcome to contribute.
154
+
155
+ ## License
156
+ 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.8'
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,27 @@
1
+ module PdfGem
2
+
3
+ class PdfGemRailtie < Rails::Railtie
4
+ initializer "my_railtie.configure_rails_initialization" do
5
+
6
+ ActionController::Renderers.add :pdf do |template, options|
7
+ params = options.except(:prefixes, :template, :disposition, :url, :html, :filename, :formats)
8
+ params[:html] = render_to_string(:action => (template.present? ? template : options.template), formats: options[:formats].present? ? options[:formats] : [:pdf] )
9
+ send_data PdfGem::pdf_from_string(params), type: Mime[:pdf], disposition: (params[:disposition].present? ? params[:disposition] : 'inline'), :filename => options[:filename]
10
+ end
11
+
12
+ ActionController::Renderers.add :pdf2 do |template, options|
13
+ params = options.except(:prefixes, :template, :disposition, :url, :html, :filename, :formats)
14
+ params[:html] = render_to_string(:action => (template.present? ? template : options.template), formats: options[:formats].present? ? options[:formats] : [:pdf] )
15
+ send_data PdfGem::pdf_from_string(params), type: Mime[:pdf], disposition: (params[:disposition].present? ? params[:disposition] : 'inline'), :filename => options[:filename]
16
+ end
17
+ if Mime::Type.lookup_by_extension(:pdf).nil?
18
+ Mime::Type.register('application/pdf', :pdf)
19
+ end
20
+ end
21
+ end
22
+
23
+
24
+
25
+
26
+
27
+ 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.8
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: []