Dhalang 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ccf622bf03fb6c32a8ee8a4ab020017d3fc058ac
4
+ data.tar.gz: 91f7d4409f8500bc5fa8a8e6012bddfbbbf0ea6f
5
+ SHA512:
6
+ metadata.gz: e65ca807af24f423c9dac5c853a13204f0bab8dcfd93aaea269499d6837167b675f24a6a5c5429086747407ff8b9ddee59299dfcf11d1a1274d267c4d43cc30f
7
+ data.tar.gz: a6c1b92715bf262445f9a8031897a817af37371b3dd5399571c53e3e0f5cedad1e327ae0a242608b66fa83dd8842114f2a588dc1cb73837495d6dc54cce444c5
@@ -0,0 +1,51 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+ .idea/*
13
+
14
+ # Used by dotenv library to load environment variables.
15
+ # .env
16
+
17
+ ## Specific to RubyMotion:
18
+ .dat*
19
+ .repl_history
20
+ build/
21
+ *.bridgesupport
22
+ build-iPhoneOS/
23
+ build-iPhoneSimulator/
24
+
25
+ ## Specific to RubyMotion (use of CocoaPods):
26
+ #
27
+ # We recommend against adding the Pods directory to your .gitignore. However
28
+ # you should judge for yourself, the pros and cons are mentioned at:
29
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
30
+ #
31
+ # vendor/Pods/
32
+
33
+ ## Documentation cache and generated files:
34
+ /.yardoc/
35
+ /_yardoc/
36
+ /doc/
37
+ /rdoc/
38
+
39
+ ## Environment normalization:
40
+ /.bundle/
41
+ /vendor/bundle
42
+ /lib/bundler/man/
43
+
44
+ # for a library or gem, you might want to ignore these files since the code is
45
+ # intended to run in multiple environments; otherwise, check them in:
46
+ # Gemfile.lock
47
+ # .ruby-version
48
+ # .ruby-gemset
49
+
50
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
51
+ .rvmrc
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2
4
+ addons:
5
+ chrome: stable
6
+ install:
7
+ - bundle install
8
+ - npm install
9
+ script: bundle exec rake spec
@@ -0,0 +1,37 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "Dhalang/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "Dhalang"
8
+ spec.version = Dhalang::VERSION
9
+ spec.authors = ["Niels Steensma"]
10
+ spec.email = ["nielssteensma@yahoo.nl"]
11
+
12
+ spec.summary = "Ruby wrapper for Puppeteer. Generate PDF's from HTML and URLs!"
13
+ spec.homepage = "https://github.com/NielsSteensma/Dhalang"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
19
+ else
20
+ raise "RubyGems 2.0 or newer is required to protect against " \
21
+ "public gem pushes."
22
+ end
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ spec.add_development_dependency "pdf-reader", "~> 2.2"
37
+ end
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in Dhalang.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 NielsS
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,42 @@
1
+ # Dhalang [![Build Status](https://travis-ci.com/NielsSteensma/Dhalang.svg?token=XZgKAByw2KZjcrsCh8gW&branch=master)](https://travis-ci.com/NielsSteensma/Dhalang)
2
+
3
+ > Dhalang is a Ruby wrapper for Google's Puppeteer.
4
+
5
+
6
+ ## Features
7
+ * Generate PDFs from pages
8
+ * Generate PDFs from html ( external images/stylesheets supported )
9
+
10
+ More will come.
11
+
12
+ ## Installation
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'dhalang'
16
+
17
+ And then execute:
18
+
19
+ $ bundle update
20
+
21
+ Install puppeteer in your application's root directory:
22
+
23
+ $ npm install puppeteer
24
+
25
+ <sub>NodeJS v7.6.0 or greater is required</sub>
26
+ ## Usage
27
+ __Get a PDF of a website url__
28
+ `Dhalang::PDF.get_from_url("https://www.google.com")`
29
+ It is important to pass the complete url, leaving out https://, http:// or www. will result in an error.
30
+
31
+ __Get a PDF of a HTML string__
32
+ `Dhalang::PDF.get_from_html("<html><head></head><body><h1>examplestring</h1></body></html>")`
33
+
34
+ Both methods return a string containing the PDF in binary.
35
+
36
+ To return the PDF from a Rails controller you can do the following:
37
+ ```
38
+ def example_controller_method
39
+ binary_pdf = Dhalang::PDF.get_from_url("https://www.google.com")
40
+ send_data(binary_pdf, filename: 'pdfofgoogle.pdf', type: 'application/pdf')
41
+ end
42
+ ```
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,3 @@
1
+ module Dhalang
2
+ require 'PDF'
3
+ end
@@ -0,0 +1,3 @@
1
+ module Dhalang
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,65 @@
1
+ require "Dhalang/version"
2
+ require 'uri'
3
+ require 'tempfile'
4
+
5
+ module Dhalang
6
+ class PDF
7
+ PDF_GENERATOR_JS_PATH = File.expand_path('../js/pdfgenerator.js', __FILE__)
8
+ PROJECT_PATH = Dir.pwd + '/node_modules/'
9
+
10
+ def self.get_from_url(url)
11
+ validate_url(url)
12
+ temporary_pdf_save_file = create_temporary_pdf_file
13
+ begin
14
+ visit_page_with_puppeteer(url, temporary_pdf_save_file.path)
15
+ binary_pdf_content = get_file_content_as_binary_string(temporary_pdf_save_file)
16
+ ensure
17
+ temporary_pdf_save_file.close unless temporary_pdf_save_file.closed?
18
+ temporary_pdf_save_file.unlink
19
+ end
20
+ return binary_pdf_content
21
+ end
22
+
23
+ def self.get_from_html(html)
24
+ html_file = create_temporary_html_file(html)
25
+ temporary_pdf_save_file = create_temporary_pdf_file
26
+ begin
27
+ visit_page_with_puppeteer("file://" + html_file.path, temporary_pdf_save_file.path)
28
+ binary_pdf_content = get_file_content_as_binary_string(temporary_pdf_save_file)
29
+ ensure
30
+ temporary_pdf_save_file.close unless temporary_pdf_save_file.closed?
31
+ html_file.close unless html_file.closed?
32
+ temporary_pdf_save_file.unlink
33
+ html_file.unlink
34
+ end
35
+ return binary_pdf_content
36
+ end
37
+
38
+ private
39
+ def self.validate_url(url)
40
+ if (url !~ URI::DEFAULT_PARSER.regexp[:ABS_URI])
41
+ raise URI::InvalidURIError, 'The given url was invalid, use format http://www.example.com'
42
+ end
43
+ end
44
+
45
+ def self.create_temporary_pdf_file
46
+ Tempfile.new("pdf")
47
+ end
48
+
49
+ ## Creates a temp .html file which can be browsed to by puppeteer for creating a pdf
50
+ def self.create_temporary_html_file(content)
51
+ html_file = Tempfile.new(['page', '.html'])
52
+ html_file.write(content)
53
+ html_file.rewind
54
+ return html_file
55
+ end
56
+
57
+ def self.visit_page_with_puppeteer(page_to_visit, path_to_save_pdf_to)
58
+ system("node #{PDF_GENERATOR_JS_PATH} #{page_to_visit} #{Shellwords.escape(path_to_save_pdf_to)} #{Shellwords.escape(PROJECT_PATH)}")
59
+ end
60
+
61
+ def self.get_file_content_as_binary_string(file)
62
+ IO.binread(file.path)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,27 @@
1
+ 'use strict';
2
+
3
+ const createPdf = async() => {
4
+ module.paths.push(process.argv[4]);
5
+ const puppeteer = require('puppeteer');
6
+ let browser;
7
+ try {
8
+ browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
9
+ const page = await browser.newPage();
10
+ await page.goto(process.argv[2], {timeout: 10000, waitUntil: 'networkidle2'});
11
+ await page.waitFor(250);
12
+ await page.pdf({
13
+ path: process.argv[3],
14
+ format: 'A4',
15
+ margin: { top: 36, right: 36, bottom: 20, left: 36 },
16
+ printBackground: true
17
+ });
18
+ } catch (err) {
19
+ console.log(err.message);
20
+ } finally {
21
+ if (browser) {
22
+ browser.close();
23
+ }
24
+ process.exit();
25
+ }
26
+ };
27
+ createPdf();
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "dhalang",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "directories": {
7
+ "lib": "lib"
8
+ },
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/NielsSteensma/Dhalang.git"
15
+ },
16
+ "author": "",
17
+ "license": "ISC",
18
+ "bugs": {
19
+ "url": "https://github.com/NielsSteensma/Dhalang/issues"
20
+ },
21
+ "homepage": "https://github.com/NielsSteensma/Dhalang#readme",
22
+ "devDependencies": {
23
+ "puppeteer": "^1.11.0"
24
+ }
25
+ }
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Dhalang
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Niels Steensma
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pdf-reader
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.2'
69
+ description:
70
+ email:
71
+ - nielssteensma@yahoo.nl
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Dhalang.gemspec
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - lib/Dhalang.rb
84
+ - lib/Dhalang/version.rb
85
+ - lib/PDF.rb
86
+ - lib/js/pdfgenerator.js
87
+ - package.json
88
+ homepage: https://github.com/NielsSteensma/Dhalang
89
+ licenses: []
90
+ metadata:
91
+ allowed_push_host: https://rubygems.org
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.5.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Ruby wrapper for Puppeteer. Generate PDF's from HTML and URLs!
112
+ test_files: []