Dhalang 0.3.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -0
- data/Dhalang.gemspec +0 -2
- data/Gemfile.lock +2 -2
- data/LICENSE +1 -1
- data/README.md +49 -4
- data/lib/Dhalang.rb +4 -1
- data/lib/Dhalang/error.rb +1 -0
- data/lib/Dhalang/puppeteer.rb +84 -5
- data/lib/Dhalang/version.rb +1 -1
- data/lib/PDF.rb +12 -9
- data/lib/Screenshot.rb +20 -6
- data/lib/js/dhalang.js +118 -26
- data/lib/js/pdf-generator.js +10 -13
- data/lib/js/screenshot-generator.js +15 -10
- data/package-lock.json +721 -200
- data/package.json +4 -4
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c68cc819480b82b9d2693cc4689e54237650e2cc2f4d1e6e40297b711e4e9c76
|
4
|
+
data.tar.gz: c5af3754067a98ccc3af4c93266827e498888d26c16557bae82b6defa3c818b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b483a6aa15f278dd3d49529e539160b999ed30cfc26a8e32ec77b9a862a7b4ae67c274f9fc7a6d46c207f204ed0882d3badf2a1491aca2b46ba6ae7504074190
|
7
|
+
data.tar.gz: e432a18e55b081e3eafbb4085483b406f879be835c0d00647ca38a38f9d7ec2ed3caefbc6b2f7a78362422eb6290b9473202aa5a3c27a6976baeef9784e58728
|
data/.travis.yml
CHANGED
data/Dhalang.gemspec
CHANGED
@@ -13,8 +13,6 @@ Gem::Specification.new do |spec|
|
|
13
13
|
spec.summary = "Ruby wrapper for Puppeteer. Generate screenshots and PDF's from HTML!"
|
14
14
|
spec.homepage = "https://github.com/NielsSteensma/Dhalang"
|
15
15
|
|
16
|
-
|
17
|
-
|
18
16
|
# Specify which files should be added to the gem when it is released.
|
19
17
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
18
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
data/Gemfile.lock
CHANGED
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -3,11 +3,14 @@
|
|
3
3
|
> Dhalang is a Ruby wrapper for Google's Puppeteer.
|
4
4
|
|
5
5
|
|
6
|
+
|
6
7
|
## Features
|
7
8
|
* Generate PDFs from pages
|
8
9
|
* Generate PDFs from html ( external images/stylesheets supported )
|
9
10
|
* Capture a screenshot of a webpage
|
10
11
|
|
12
|
+
|
13
|
+
|
11
14
|
## Installation
|
12
15
|
Add this line to your application's Gemfile:
|
13
16
|
|
@@ -21,7 +24,7 @@ Install puppeteer in your application's root directory:
|
|
21
24
|
|
22
25
|
$ npm install puppeteer
|
23
26
|
|
24
|
-
<sub>NodeJS
|
27
|
+
<sub>NodeJS v10.18.1 or greater is required</sub>
|
25
28
|
## Usage
|
26
29
|
__Get a PDF of a website url__
|
27
30
|
`Dhalang::PDF.get_from_url("https://www.google.com")`
|
@@ -38,7 +41,49 @@ __Get a JPEG screenshot of a website__
|
|
38
41
|
|
39
42
|
All methods return a string containing the PDF or JPEG/PNG in binary.
|
40
43
|
|
41
|
-
|
44
|
+
|
45
|
+
|
46
|
+
## Custom PDF/screenshot options
|
47
|
+
To override the default options that are set by Dhalang you can pass as last argument a hash with the custom options you want to set.
|
48
|
+
|
49
|
+
For example to set custom margins for PDFs:
|
50
|
+
|
51
|
+
`Dhalang::PDF.get_from_url("https://www.google.com", {margin: { top: 100, right: 100, bottom: 100, left: 100}})
|
52
|
+
`
|
53
|
+
|
54
|
+
For example to only take a screenshot of the visible part of the page:
|
55
|
+
`Dhalang::Screenshot.get_from_url_as_png("https://www.google.com", {fullPage: false})
|
56
|
+
`
|
57
|
+
|
58
|
+
A list of all possible PDF options that can be set, can be found at: https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagepdfoptions
|
59
|
+
|
60
|
+
A list of all possible screenshot options that can be set, can be found at: https://github.com/puppeteer/puppeteer/blob/main/docs/api.md#pagescreenshotoptions
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
## Custom user options
|
66
|
+
You may want to change the way Dhalang interacts with Puppeteer in general. User options can be set by providing them in a hash as last argument to any calls you make to the library. Are you setting both custom PDF and user options? Then they should be passed as a single hash.
|
67
|
+
|
68
|
+
For example to set a custom navigation timeout:
|
69
|
+
`Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com", {navigationTimeout: 20000})`
|
70
|
+
|
71
|
+
Below table lists all possible configuration parameters that can be set:
|
72
|
+
| Key | Description | Default |
|
73
|
+
|--------------------|-----------------------------------------------------------------------------------------|---------------------------------|
|
74
|
+
| navigationTimeout | Amount of milliseconds until Puppeteer while timeout when navigating to the given page | 10000 |
|
75
|
+
| navigationWaitForSelector | If set, Dhalang will wait for the specified selector to appear before creating the screenshot or PDF | None |
|
76
|
+
| navigationWaitForXPath | If set, Dhalang will wait for the specified XPath to appear before creating the screenshot or PDF | None |
|
77
|
+
| userAgent | User agent to send with the request | Default Puppeteer one |
|
78
|
+
| isHeadless | Indicates if Chromium should be launched headless | true |
|
79
|
+
| isAutoHeight | When set to true the height of generated PDFs will be based on the scrollHeight property of the document body | false |
|
80
|
+
| viewPort | Custom viewport to use for the request | Default Puppeteer one |
|
81
|
+
| httpAuthenticationCredentials | Custom HTTP authentication credentials to use for the request | None |
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
## Examples of using Dhalang
|
86
|
+
To return a PDF from a Rails controller you can do the following:
|
42
87
|
```
|
43
88
|
def example_controller_method
|
44
89
|
binary_pdf = Dhalang::PDF.get_from_url("https://www.google.com")
|
@@ -46,7 +91,7 @@ def example_controller_method
|
|
46
91
|
end
|
47
92
|
```
|
48
93
|
|
49
|
-
To return
|
94
|
+
To return a PNG from a Rails controller you can do the following:
|
50
95
|
```
|
51
96
|
def example_controller_method
|
52
97
|
binary_png = Dhalang::Screenshot.get_from_url_as_png("https://www.google.com")
|
@@ -54,7 +99,7 @@ def example_controller_method
|
|
54
99
|
end
|
55
100
|
```
|
56
101
|
|
57
|
-
To return
|
102
|
+
To return a JPEG from a Rails controller you can do the following:
|
58
103
|
```
|
59
104
|
def example_controller_method
|
60
105
|
binary_jpeg = Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com")
|
data/lib/Dhalang.rb
CHANGED
@@ -4,8 +4,11 @@ module Dhalang
|
|
4
4
|
require_relative 'Dhalang/version'
|
5
5
|
require_relative 'Dhalang/url_utils'
|
6
6
|
require_relative 'Dhalang/file_utils'
|
7
|
+
require_relative 'Dhalang/error'
|
7
8
|
require_relative 'Dhalang/puppeteer'
|
8
9
|
require 'uri'
|
9
10
|
require 'tempfile'
|
10
11
|
require 'shellwords'
|
11
|
-
|
12
|
+
require 'json'
|
13
|
+
require 'open3'
|
14
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
class DhalangError < StandardError; end
|
data/lib/Dhalang/puppeteer.rb
CHANGED
@@ -3,15 +3,94 @@ module Dhalang
|
|
3
3
|
class Puppeteer
|
4
4
|
NODE_MODULES_PATH = Dir.pwd + '/node_modules/'.freeze
|
5
5
|
private_constant :NODE_MODULES_PATH
|
6
|
-
|
6
|
+
|
7
|
+
USER_OPTIONS = {
|
8
|
+
navigationTimeout: 10000,
|
9
|
+
navigationWaitUntil: 'load',
|
10
|
+
navigationWaitForSelector: '',
|
11
|
+
navigationWaitForXPath: '',
|
12
|
+
userAgent: '',
|
13
|
+
isHeadless: true,
|
14
|
+
viewPort: '',
|
15
|
+
httpAuthenticationCredentials: '',
|
16
|
+
isAutoHeight: false
|
17
|
+
}
|
18
|
+
private_constant :USER_OPTIONS
|
19
|
+
|
20
|
+
DEFAULT_PDF_OPTIONS = {
|
21
|
+
scale: 1,
|
22
|
+
displayHeaderFooter: false,
|
23
|
+
headerTemplate: '',
|
24
|
+
footerTemplate: '',
|
25
|
+
printBackground: true,
|
26
|
+
landscape: false,
|
27
|
+
pageRanges: '',
|
28
|
+
format: 'A4',
|
29
|
+
width: '',
|
30
|
+
height: '',
|
31
|
+
margin: { top: 36, right: 36, bottom: 20, left: 36 },
|
32
|
+
preferCSSPageSize: true,
|
33
|
+
omitBackground: false
|
34
|
+
}
|
35
|
+
private_constant :DEFAULT_PDF_OPTIONS
|
36
|
+
|
37
|
+
DEFAULT_PNG_OPTIONS = {
|
38
|
+
fullPage: true,
|
39
|
+
clip: nil,
|
40
|
+
omitBackground: false
|
41
|
+
}
|
42
|
+
private_constant :DEFAULT_PNG_OPTIONS
|
43
|
+
|
44
|
+
DEFAULT_JPEG_OPTIONS = {
|
45
|
+
quality: 100,
|
46
|
+
fullPage: true,
|
47
|
+
clip: nil,
|
48
|
+
omitBackground: false
|
49
|
+
}
|
50
|
+
private_constant :DEFAULT_JPEG_OPTIONS
|
51
|
+
|
52
|
+
|
7
53
|
# Launches a new Node process, executing the (Puppeteer) script under the given script_path.
|
8
54
|
#
|
9
55
|
# @param [String] page_url The url to pass to the goTo method of Puppeteer.
|
10
56
|
# @param [String] script_path The absolute path of the JS script to execute.
|
11
|
-
# @param [String] temp_file_path The absolute path of the temp file to use to write any actions
|
57
|
+
# @param [String] temp_file_path The absolute path of the temp file to use to write any actions from Puppeteer.
|
12
58
|
# @param [String] temp_file_extension The extension of the temp file.
|
13
|
-
|
14
|
-
|
59
|
+
# @param [Object] options Set of options to use, configurable by the user.
|
60
|
+
def self.visit(page_url, script_path, temp_file_path, temp_file_extension, options)
|
61
|
+
configuration = create_configuration(page_url, script_path, temp_file_path, temp_file_extension, options)
|
62
|
+
|
63
|
+
command = "node #{script_path} #{Shellwords.escape(configuration)}"
|
64
|
+
|
65
|
+
Open3.popen2e(command) do |_stdin, stdouterr, wait|
|
66
|
+
return nil if wait.value.success?
|
67
|
+
|
68
|
+
output = stdouterr.read.strip
|
69
|
+
output = nil if output == ''
|
70
|
+
message = output || "Exited with status #{wait.value.exitstatus}"
|
71
|
+
raise DhalangError, message
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
# Returns a JSON string with the configuration to use within the Puppeteer script.
|
77
|
+
#
|
78
|
+
# @param [String] page_url The url to pass to the goTo method of Puppeteer.
|
79
|
+
# @param [String] script_path The absolute path of the JS script to execute.
|
80
|
+
# @param [String] temp_file_path The absolute path of the temp file to use to write any actions from Puppeteer.
|
81
|
+
# @param [String] temp_file_extension The extension of the temp file.
|
82
|
+
# @param [Hash] options Set of options to use, configurable by the user.
|
83
|
+
private_class_method def self.create_configuration(page_url, script_path, temp_file_path, temp_file_extension, options)
|
84
|
+
{
|
85
|
+
webPageUrl: page_url,
|
86
|
+
tempFilePath: temp_file_path,
|
87
|
+
puppeteerPath: NODE_MODULES_PATH,
|
88
|
+
imageType: temp_file_extension,
|
89
|
+
userOptions: USER_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value]}.to_h,
|
90
|
+
pdfOptions: DEFAULT_PDF_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value] }.to_h,
|
91
|
+
pngOptions: DEFAULT_PNG_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value] }.to_h,
|
92
|
+
jpegOptions: DEFAULT_JPEG_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value] }.to_h
|
93
|
+
}.to_json
|
15
94
|
end
|
16
95
|
end
|
17
|
-
end
|
96
|
+
end
|
data/lib/Dhalang/version.rb
CHANGED
data/lib/PDF.rb
CHANGED
@@ -6,25 +6,27 @@ module Dhalang
|
|
6
6
|
|
7
7
|
# Captures the full webpage under the given url as PDF.
|
8
8
|
#
|
9
|
-
# @param [String] url
|
9
|
+
# @param [String] url The url to get as PDF.
|
10
|
+
# @param [Hash] options User configurable options.
|
10
11
|
#
|
11
12
|
# @return [String] The PDF that was created as binary.
|
12
|
-
def self.get_from_url(url)
|
13
|
+
def self.get_from_url(url, options = {})
|
13
14
|
UrlUtils.validate(url)
|
14
|
-
get(url)
|
15
|
+
get(url, options)
|
15
16
|
end
|
16
17
|
|
17
18
|
# Captures the full HTML as PDF.
|
18
19
|
# Useful when creating dynamic content, for example invoices.
|
19
20
|
#
|
20
|
-
# @param [String]
|
21
|
+
# @param [String] html The html to get as PDF.
|
22
|
+
# @param [Hash] options User configurable options.
|
21
23
|
#
|
22
24
|
# @return [String] The PDF that was created as binary.
|
23
|
-
def self.get_from_html(html)
|
25
|
+
def self.get_from_html(html, options = {})
|
24
26
|
html_file = FileUtils.create_temp_file("html", html)
|
25
27
|
url = "file://" + html_file.path
|
26
28
|
begin
|
27
|
-
binary_pdf_content = get(url)
|
29
|
+
binary_pdf_content = get(url, options)
|
28
30
|
ensure
|
29
31
|
FileUtils.delete(html_file)
|
30
32
|
end
|
@@ -34,13 +36,14 @@ module Dhalang
|
|
34
36
|
|
35
37
|
# Groups and executes the logic for creating a PDF of a webpage.
|
36
38
|
#
|
37
|
-
# @param [String]
|
39
|
+
# @param [String] url The url to create a PDF for.
|
40
|
+
# @param [Hash] options Set of options to use, passed by the user of this library.
|
38
41
|
#
|
39
42
|
# @return [String] The PDF that was created as binary.
|
40
|
-
private_class_method def self.get(url)
|
43
|
+
private_class_method def self.get(url, options)
|
41
44
|
temp_file = FileUtils.create_temp_file("pdf")
|
42
45
|
begin
|
43
|
-
Puppeteer.visit(url, PUPPETEER_SCRIPT_PATH, temp_file.path, "pdf")
|
46
|
+
Puppeteer.visit(url, PUPPETEER_SCRIPT_PATH, temp_file.path, "pdf", options)
|
44
47
|
binary_pdf_content = FileUtils.read_binary(temp_file.path)
|
45
48
|
ensure
|
46
49
|
FileUtils.delete(temp_file)
|
data/lib/Screenshot.rb
CHANGED
@@ -7,37 +7,51 @@ module Dhalang
|
|
7
7
|
# Captures a full JPEG screenshot of the webpage under the given url.
|
8
8
|
#
|
9
9
|
# @param [String] url The url to take a screenshot of.
|
10
|
+
# @param [Hash] options User configurable options.
|
10
11
|
#
|
11
12
|
# @return [String] the screenshot that was taken as binary.
|
12
|
-
def self.get_from_url_as_jpeg(url)
|
13
|
-
get(url, "jpeg")
|
13
|
+
def self.get_from_url_as_jpeg(url, options = {})
|
14
|
+
get(url, "jpeg", options)
|
14
15
|
end
|
15
16
|
|
16
17
|
# Captures a full PNG screenshot of the webpage under the given url.
|
17
18
|
#
|
18
19
|
# @param [String] url The url to take a screenshot of.
|
20
|
+
# @param [Hash] options User configurable options.
|
19
21
|
#
|
20
22
|
# @return [String] The screenshot that was taken as binary.
|
21
|
-
def self.get_from_url_as_png(url)
|
22
|
-
get(url, "png")
|
23
|
+
def self.get_from_url_as_png(url, options = {})
|
24
|
+
get(url, "png", options)
|
23
25
|
end
|
24
26
|
|
25
27
|
# Groups and executes the logic for taking a screenhot of a webpage.
|
26
28
|
#
|
27
29
|
# @param [String] url The url to take a screenshot of.
|
28
30
|
# @param [String] image_type The image type to use for storing the screenshot.
|
31
|
+
# @param [Hash] options Set of options to use, passed by the user of this library.
|
29
32
|
#
|
30
33
|
# @return [String] The screenshot that was taken as binary.
|
31
|
-
private_class_method def self.get(url, image_type)
|
34
|
+
private_class_method def self.get(url, image_type, options)
|
32
35
|
UrlUtils.validate(url)
|
36
|
+
validate_options(options)
|
33
37
|
temp_file = FileUtils.create_temp_file(image_type)
|
34
38
|
begin
|
35
|
-
Puppeteer.visit(url, PUPPETEER_SCRIPT_PATH, temp_file.path, image_type)
|
39
|
+
Puppeteer.visit(url, PUPPETEER_SCRIPT_PATH, temp_file.path, image_type, options)
|
36
40
|
binary_image_content = FileUtils.read_binary(temp_file.path)
|
37
41
|
ensure
|
38
42
|
FileUtils.delete(temp_file)
|
39
43
|
end
|
40
44
|
return binary_image_content
|
41
45
|
end
|
46
|
+
|
47
|
+
# Raises an error if the given options might conflict with the Puppeteer configuration.
|
48
|
+
#
|
49
|
+
# @param [Hash] options The options to validate
|
50
|
+
private_class_method def self.validate_options(options)
|
51
|
+
symbolized_options = options.transform_keys(&:to_sym)
|
52
|
+
if symbolized_options.has_key?(:type)
|
53
|
+
raise DhalangError, 'Invalid option set: "type"'
|
54
|
+
end
|
55
|
+
end
|
42
56
|
end
|
43
57
|
end
|
data/lib/js/dhalang.js
CHANGED
@@ -1,54 +1,146 @@
|
|
1
1
|
/**
|
2
2
|
* @typedef {Object} Configuration
|
3
|
-
* @property {string} webPageUrl
|
4
|
-
* @property {string} tempFilePath
|
5
|
-
* @property {string} puppeteerModulePath
|
6
|
-
* @property {string} imageType
|
3
|
+
* @property {string} webPageUrl - The url of the webpage to visit.
|
4
|
+
* @property {string} tempFilePath - The path of the tempfile to write the screenshot/pdf to.
|
5
|
+
* @property {string} puppeteerModulePath - The path of the Puppeteer module.
|
6
|
+
* @property {string} imageType - The type of image to save ( undefined for pdfgenerator ).
|
7
|
+
* @property {UserOptions} userOptions - User defined and default parameters to use when navigating to pages.
|
8
|
+
* @property {Object} pdfOptions - User defined and default parameters to use when creating PDFs. Note: Do not use directly, rather use {@link getConfiguredPdfOptions}.
|
9
|
+
* @property {Object} pngOptions - User defined and default parameters to use when creating PNGs.
|
10
|
+
* @property {Object} jpegOptions - User defined and default parameters to use when creating JPEGs.
|
11
|
+
*/
|
12
|
+
|
13
|
+
/**
|
14
|
+
* @typedef {Object} UserOptions
|
15
|
+
* @property {number} navigationTimeout - Maximum in milliseconds until navigation times out, we use a default of 10 seconds as timeout.
|
16
|
+
* @property {string} navigationWaitUntil - Determines when the navigation was finished, we wait here until the Window.load event is fired ( meaning all images, stylesheet, etc was loaded ).
|
17
|
+
* @property {string} navigationWaitForSelector - If set, specifies the selector Puppeteer should wait for to appear before continuing.
|
18
|
+
* @property {string} navigationWaitForXPath - If set, specifies the XPath Puppeteer should wait for to appear before continuing.
|
19
|
+
* @property {string} userAgent - The user agent to send with requests.
|
20
|
+
* @property {boolean} isHeadless - Indicates if Puppeteer should launch Chromium in headless mode.
|
21
|
+
* @property {Object} viewPort - The view port to use.
|
22
|
+
* @property {Object} httpAuthenticationCredentials - The credentials to use for HTTP authentication.
|
23
|
+
* @property {boolean} isAutoHeight - The height is automatically set
|
7
24
|
*/
|
8
25
|
|
9
26
|
/**
|
10
27
|
* @typedef {Object} NavigationParameters
|
11
|
-
* @property {number} timeout
|
12
|
-
* @property {string} waituntil
|
28
|
+
* @property {number} timeout - Maximum in milliseconds until navigation times out, we use a default of 10 seconds as timeout.
|
29
|
+
* @property {string} waituntil - Determines when the navigation was finished, we wait here until the Window.load event is fired ( meaning all images, stylesheet, etc was loaded ).
|
30
|
+
*/
|
31
|
+
|
32
|
+
/**
|
33
|
+
* @typedef {Object} WaitingParameters
|
34
|
+
* @property {number} timeout - Maximum in milliseconds until navigation times out, we use a default of 10 seconds as timeout.
|
13
35
|
*/
|
14
36
|
|
15
37
|
/**
|
16
|
-
*
|
17
|
-
* @param {Boolean} isForScreenshotGenerator - Indicates if this configuration is for a screenshot generator.
|
38
|
+
* Parses the given configuration process argument from Ruby to a JS object.
|
18
39
|
* @returns {Configuration}
|
19
|
-
* The
|
40
|
+
* The configuration object.
|
20
41
|
*/
|
21
|
-
exports.getConfiguration = function (
|
22
|
-
return
|
23
|
-
puppeteerPath: process.argv[2],
|
24
|
-
webPageUrl: process.argv[3],
|
25
|
-
tempFilePath: process.argv[4],
|
26
|
-
imageType: isForScreenshotGenerator ? process.argv[5] : undefined
|
27
|
-
}
|
42
|
+
exports.getConfiguration = function () {
|
43
|
+
return JSON.parse(process.argv[2])
|
28
44
|
}
|
29
45
|
|
30
46
|
/**
|
31
47
|
* Launches Puppeteer and returns its instance.
|
32
|
-
* @param {
|
33
|
-
* @returns {Promise<Object>}
|
48
|
+
* @param {UserOptions} configuration - The configuration to use.
|
49
|
+
* @returns {Promise<Object>}
|
34
50
|
* The launched instance of Puppeteer.
|
35
51
|
*/
|
36
|
-
exports.launchPuppeteer = async function (
|
37
|
-
module.paths.push(
|
52
|
+
exports.launchPuppeteer = async function (configuration) {
|
53
|
+
module.paths.push(configuration.puppeteerPath);
|
38
54
|
const puppeteer = require('puppeteer');
|
55
|
+
const launchArgs = ['--no-sandbox', '--disable-setuid-sandbox'];
|
39
56
|
return await puppeteer.launch({
|
40
|
-
args:
|
57
|
+
args: launchArgs,
|
58
|
+
headless: configuration.userOptions.isHeadless
|
41
59
|
});
|
42
60
|
}
|
43
61
|
|
44
62
|
/**
|
45
|
-
*
|
63
|
+
* Configures the given Puppeteer page object.
|
64
|
+
* @param {Object} page - The Puppeteer page object to configure.
|
65
|
+
* @param {UserOptions} userOptions - The user options to use.
|
66
|
+
*/
|
67
|
+
exports.configure = async function (page, userOptions) {
|
68
|
+
if (userOptions.userAgent !== "") {
|
69
|
+
await page.setUserAgent(userOptions.userAgent)
|
70
|
+
}
|
71
|
+
|
72
|
+
if (userOptions.viewPort !== "") {
|
73
|
+
await page.setViewport(userOptions.viewPort)
|
74
|
+
}
|
75
|
+
|
76
|
+
if (userOptions.httpAuthenticationCredentials !== "") {
|
77
|
+
await page.authenticate(userOptions.authenticationCredentials)
|
78
|
+
}
|
79
|
+
}
|
80
|
+
|
81
|
+
/**
|
82
|
+
* Makes the Puppeteer page object open the url with the specified navigation logic as specified in the given configuration.
|
83
|
+
* @param {Object} page - The Puppeteer page object to use for navigation.
|
84
|
+
* @param {Configuration} configuration - The configuration to use.
|
85
|
+
*/
|
86
|
+
exports.navigate = async function (page, configuration) {
|
87
|
+
const navigationWaitForSelector = configuration.userOptions.navigationWaitForSelector;
|
88
|
+
const navigationWaitForXPath = configuration.userOptions.navigationWaitForXPath;
|
89
|
+
|
90
|
+
await page.goto(configuration.webPageUrl, this.getNavigationParameters(configuration));
|
91
|
+
|
92
|
+
if (navigationWaitForSelector !== "") {
|
93
|
+
await page.waitForSelector(navigationWaitForSelector, this.getWaitingParameters(configuration));
|
94
|
+
} else if (navigationWaitForXPath !== "") {
|
95
|
+
await page.waitForXPath(navigationWaitForXPath, this.getWaitingParameters(configuration));
|
96
|
+
} else {
|
97
|
+
await page.waitForTimeout(250);
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
/**
|
102
|
+
* Returns the PDF options to pass to Puppeteer based on the set user options and the documents body.
|
103
|
+
* @param {Object} page - The Puppeteer page to configure.
|
104
|
+
* @param {UserOptions} configuration - The configuration to use.
|
105
|
+
* @returns {Object} - pdfOptions
|
106
|
+
*/
|
107
|
+
exports.getConfiguredPdfOptions = async function (page, configuration) {
|
108
|
+
const pdfOptions = configuration.pdfOptions
|
109
|
+
|
110
|
+
if (configuration.userOptions.isAutoHeight === true) {
|
111
|
+
const pageHeight = await page.evaluate(() => {
|
112
|
+
return Math.max(document.body.scrollHeight, document.body.offsetHeight);
|
113
|
+
})
|
114
|
+
if (pageHeight) {
|
115
|
+
pdfOptions['height'] = pageHeight + 1 + 'px'
|
116
|
+
}
|
117
|
+
}
|
118
|
+
|
119
|
+
return pdfOptions
|
120
|
+
}
|
121
|
+
|
122
|
+
/**
|
123
|
+
* Extracts the navigation parameters from the configuration in a format that is usable by Puppeteer.
|
124
|
+
* @param {Configuration} configuration - The configuration to extract the navigation parameters from.
|
46
125
|
* @returns {NavigationParameters}
|
47
|
-
* The navigation parameters
|
126
|
+
* The extracted navigation parameters.
|
127
|
+
*/
|
128
|
+
exports.getNavigationParameters = function (configuration) {
|
129
|
+
return {
|
130
|
+
timeout: configuration.userOptions.navigationTimeout,
|
131
|
+
waituntil: configuration.userOptions.navigationWaitUntil
|
132
|
+
}
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
/**
|
137
|
+
* Extracts the waiting parameters from the configuration in a format that is usable by Puppeteer.
|
138
|
+
* @param {Configuration} configuration - The configuration to extract the waiting parameters from.
|
139
|
+
* @returns {WaitingParameters}
|
140
|
+
* The extracted waiting parameters.
|
48
141
|
*/
|
49
|
-
exports.
|
142
|
+
exports.getWaitingParameters = function (configuration) {
|
50
143
|
return {
|
51
|
-
timeout:
|
52
|
-
waitUntil: 'load'
|
144
|
+
timeout: configuration.userOptions.navigationTimeout
|
53
145
|
}
|
54
146
|
}
|