Dhalang 0.4.0 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/LICENSE +1 -1
- data/README.md +50 -22
- data/lib/Dhalang/error.rb +1 -0
- data/lib/Dhalang/puppeteer.rb +46 -28
- data/lib/Dhalang/version.rb +1 -1
- data/lib/Dhalang.rb +3 -1
- data/lib/Screenshot.rb +11 -0
- data/lib/js/dhalang.js +102 -19
- data/lib/js/pdf-generator.js +7 -6
- data/lib/js/screenshot-generator.js +14 -10
- data/package-lock.json +532 -93
- data/package.json +5 -5
- 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: 0b5a8f2427673c1a21cbbfcc3f9b3119c11890b9dbb21a1656ec970c14b4faef
|
4
|
+
data.tar.gz: d98cac7a86e5c12c0fc845e71a8810c3adaa3c583fb53a4139f2e1408231cceb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 142fcef44cc80374790a915356c7eda5b5a5d91e532c44a4a81515f07454b9c2a7a04d34d53b3cb481fb0ed2f58cc6a0ed974a0ad4d244e9622f754e32e47624
|
7
|
+
data.tar.gz: 8379ed46f315e7cf3cfa657c2972fbdc3e34bee4255f2be741e84835c82536d4ace52290fb0197e1638a77667fa0a59309357ce62bca3887ba746c44665d8e9c
|
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
|
|
@@ -40,41 +43,66 @@ All methods return a string containing the PDF or JPEG/PNG in binary.
|
|
40
43
|
|
41
44
|
|
42
45
|
|
43
|
-
## Custom
|
44
|
-
|
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
|
+
|
45
62
|
|
46
|
-
So for example:
|
47
|
-
`Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com", {navigation_timeout: 20000})`
|
48
63
|
|
49
|
-
|
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:
|
50
72
|
| Key | Description | Default |
|
51
73
|
|--------------------|-----------------------------------------------------------------------------------------|---------------------------------|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
+
|
56
83
|
|
57
|
-
|
84
|
+
|
85
|
+
## Examples of using Dhalang
|
58
86
|
To return a PDF from a Rails controller you can do the following:
|
59
|
-
```
|
60
|
-
def example_controller_method
|
61
|
-
|
62
|
-
|
87
|
+
```ruby
|
88
|
+
def example_controller_method
|
89
|
+
binary_pdf = Dhalang::PDF.get_from_url("https://www.google.com")
|
90
|
+
send_data(binary_pdf, filename: 'pdfofgoogle.pdf', type: 'application/pdf')
|
63
91
|
end
|
64
92
|
```
|
65
93
|
|
66
94
|
To return a PNG from a Rails controller you can do the following:
|
67
|
-
```
|
68
|
-
def example_controller_method
|
69
|
-
|
70
|
-
|
95
|
+
```ruby
|
96
|
+
def example_controller_method
|
97
|
+
binary_png = Dhalang::Screenshot.get_from_url_as_png("https://www.google.com")
|
98
|
+
send_data(binary_png, filename: 'screenshotofgoogle.png', type: 'image/png')
|
71
99
|
end
|
72
100
|
```
|
73
101
|
|
74
102
|
To return a JPEG from a Rails controller you can do the following:
|
75
|
-
```
|
76
|
-
def example_controller_method
|
77
|
-
|
78
|
-
|
103
|
+
```ruby
|
104
|
+
def example_controller_method
|
105
|
+
binary_jpeg = Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com")
|
106
|
+
send_data(binary_jpeg, filename: 'screenshotofgoogle.jpeg', type: 'image/jpeg')
|
79
107
|
end
|
80
108
|
```
|
@@ -0,0 +1 @@
|
|
1
|
+
class DhalangError < StandardError; end
|
data/lib/Dhalang/puppeteer.rb
CHANGED
@@ -4,22 +4,20 @@ module Dhalang
|
|
4
4
|
NODE_MODULES_PATH = Dir.pwd + '/node_modules/'.freeze
|
5
5
|
private_constant :NODE_MODULES_PATH
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
HTTP_AUTHENTICATION_CREDENTIALS = ''
|
20
|
-
private_constant :HTTP_AUTHENTICATION_CREDENTIALS
|
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
|
21
19
|
|
22
|
-
|
20
|
+
DEFAULT_PDF_OPTIONS = {
|
23
21
|
scale: 1,
|
24
22
|
displayHeaderFooter: false,
|
25
23
|
headerTemplate: '',
|
@@ -31,9 +29,25 @@ module Dhalang
|
|
31
29
|
width: '',
|
32
30
|
height: '',
|
33
31
|
margin: { top: 36, right: 36, bottom: 20, left: 36 },
|
34
|
-
|
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
|
35
49
|
}
|
36
|
-
private_constant :
|
50
|
+
private_constant :DEFAULT_JPEG_OPTIONS
|
37
51
|
|
38
52
|
|
39
53
|
# Launches a new Node process, executing the (Puppeteer) script under the given script_path.
|
@@ -45,7 +59,17 @@ module Dhalang
|
|
45
59
|
# @param [Object] options Set of options to use, configurable by the user.
|
46
60
|
def self.visit(page_url, script_path, temp_file_path, temp_file_extension, options)
|
47
61
|
configuration = create_configuration(page_url, script_path, temp_file_path, temp_file_extension, options)
|
48
|
-
|
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
|
49
73
|
end
|
50
74
|
|
51
75
|
|
@@ -62,16 +86,10 @@ module Dhalang
|
|
62
86
|
tempFilePath: temp_file_path,
|
63
87
|
puppeteerPath: NODE_MODULES_PATH,
|
64
88
|
imageType: temp_file_extension,
|
65
|
-
userOptions: {
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
},
|
70
|
-
userAgent: options.has_key?(:user_agent) ? options[:user_agent] : USER_AGENT,
|
71
|
-
viewPort: options.has_key?(:view_port) ? options[:view_port] : VIEW_PORT,
|
72
|
-
httpAuthenticationCredentials: options.has_key?(:http_authentication_credentials) ? options[:http_authentication_credentials] : HTTP_AUTHENTICATION_CREDENTIALS
|
73
|
-
},
|
74
|
-
pdfOptions: DEFAULT_OPTIONS.map { |option, value| [option, options.has_key?(option) ? options[option] : value] }.to_h
|
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
|
75
93
|
}.to_json
|
76
94
|
end
|
77
95
|
end
|
data/lib/Dhalang/version.rb
CHANGED
data/lib/Dhalang.rb
CHANGED
@@ -4,9 +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'
|
12
|
-
|
13
|
+
require 'open3'
|
14
|
+
end
|
data/lib/Screenshot.rb
CHANGED
@@ -33,6 +33,7 @@ module Dhalang
|
|
33
33
|
# @return [String] The screenshot that was taken as binary.
|
34
34
|
private_class_method def self.get(url, image_type, options)
|
35
35
|
UrlUtils.validate(url)
|
36
|
+
validate_options(options)
|
36
37
|
temp_file = FileUtils.create_temp_file(image_type)
|
37
38
|
begin
|
38
39
|
Puppeteer.visit(url, PUPPETEER_SCRIPT_PATH, temp_file.path, image_type, options)
|
@@ -42,5 +43,15 @@ module Dhalang
|
|
42
43
|
end
|
43
44
|
return binary_image_content
|
44
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
|
45
56
|
end
|
46
57
|
end
|
data/lib/js/dhalang.js
CHANGED
@@ -4,14 +4,23 @@
|
|
4
4
|
* @property {string} tempFilePath - The path of the tempfile to write the screenshot/pdf to.
|
5
5
|
* @property {string} puppeteerModulePath - The path of the Puppeteer module.
|
6
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
|
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.
|
8
11
|
*/
|
12
|
+
|
9
13
|
/**
|
10
14
|
* @typedef {Object} UserOptions
|
11
|
-
* @property {
|
12
|
-
* @property {string}
|
13
|
-
* @property {
|
14
|
-
* @property {
|
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
|
15
24
|
*/
|
16
25
|
|
17
26
|
/**
|
@@ -20,6 +29,11 @@
|
|
20
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 ).
|
21
30
|
*/
|
22
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.
|
35
|
+
*/
|
36
|
+
|
23
37
|
/**
|
24
38
|
* Parses the given configuration process argument from Ruby to a JS object.
|
25
39
|
* @returns {Configuration}
|
@@ -31,33 +45,102 @@ exports.getConfiguration = function () {
|
|
31
45
|
|
32
46
|
/**
|
33
47
|
* Launches Puppeteer and returns its instance.
|
34
|
-
* @param {
|
35
|
-
* @returns {Promise<Object>}
|
48
|
+
* @param {UserOptions} configuration - The configuration to use.
|
49
|
+
* @returns {Promise<Object>}
|
36
50
|
* The launched instance of Puppeteer.
|
37
51
|
*/
|
38
|
-
exports.launchPuppeteer = async function (
|
39
|
-
module.paths.push(
|
52
|
+
exports.launchPuppeteer = async function (configuration) {
|
53
|
+
module.paths.push(configuration.puppeteerPath);
|
40
54
|
const puppeteer = require('puppeteer');
|
55
|
+
const launchArgs = ['--no-sandbox', '--disable-setuid-sandbox'];
|
41
56
|
return await puppeteer.launch({
|
42
|
-
args:
|
57
|
+
args: launchArgs,
|
58
|
+
headless: configuration.userOptions.isHeadless
|
43
59
|
});
|
44
60
|
}
|
45
61
|
|
46
62
|
/**
|
47
|
-
* Configures the given Puppeteer page.
|
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.httpAuthenticationCredentials)
|
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.
|
48
103
|
* @param {Object} page - The Puppeteer page to configure.
|
49
104
|
* @param {UserOptions} configuration - The configuration to use.
|
105
|
+
* @returns {Object} - pdfOptions
|
50
106
|
*/
|
51
|
-
exports.
|
52
|
-
|
53
|
-
|
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
|
+
}
|
54
117
|
}
|
55
118
|
|
56
|
-
|
57
|
-
|
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.
|
125
|
+
* @returns {NavigationParameters}
|
126
|
+
* The extracted navigation parameters.
|
127
|
+
*/
|
128
|
+
exports.getNavigationParameters = function (configuration) {
|
129
|
+
return {
|
130
|
+
timeout: configuration.userOptions.navigationTimeout,
|
131
|
+
waituntil: configuration.userOptions.navigationWaitUntil
|
58
132
|
}
|
133
|
+
}
|
59
134
|
|
60
|
-
|
61
|
-
|
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.
|
141
|
+
*/
|
142
|
+
exports.getWaitingParameters = function (configuration) {
|
143
|
+
return {
|
144
|
+
timeout: configuration.userOptions.navigationTimeout
|
62
145
|
}
|
63
|
-
}
|
146
|
+
}
|
data/lib/js/pdf-generator.js
CHANGED
@@ -6,19 +6,20 @@ const createPdf = async () => {
|
|
6
6
|
|
7
7
|
let browser;
|
8
8
|
try {
|
9
|
-
browser = await dhalang.launchPuppeteer(configuration
|
9
|
+
browser = await dhalang.launchPuppeteer(configuration);
|
10
10
|
const page = await browser.newPage();
|
11
|
-
await dhalang.
|
12
|
-
await
|
13
|
-
await
|
11
|
+
await dhalang.configure(page, configuration.userOptions);
|
12
|
+
await dhalang.navigate(page, configuration);
|
13
|
+
const pdfOptions = await dhalang.getConfiguredPdfOptions(page, configuration);
|
14
14
|
await page.pdf({
|
15
15
|
...{
|
16
16
|
path: configuration.tempFilePath
|
17
17
|
},
|
18
|
-
...
|
18
|
+
...pdfOptions
|
19
19
|
});
|
20
20
|
} catch (error) {
|
21
|
-
console.
|
21
|
+
console.error(error.message);
|
22
|
+
process.exit(1);
|
22
23
|
} finally {
|
23
24
|
if (browser) {
|
24
25
|
browser.close();
|
@@ -1,23 +1,27 @@
|
|
1
1
|
'use strict';
|
2
2
|
const dhalang = require('./dhalang')
|
3
3
|
|
4
|
-
const
|
4
|
+
const createScreenshot = async () => {
|
5
5
|
const configuration = dhalang.getConfiguration();
|
6
6
|
|
7
7
|
let browser;
|
8
8
|
try {
|
9
|
-
browser = await dhalang.launchPuppeteer(configuration
|
9
|
+
browser = await dhalang.launchPuppeteer(configuration);
|
10
10
|
const page = await browser.newPage();
|
11
|
-
await dhalang.
|
12
|
-
await
|
13
|
-
|
11
|
+
await dhalang.configure(page, configuration.userOptions);
|
12
|
+
await dhalang.navigate(page, configuration);
|
13
|
+
|
14
|
+
const screenshotOptions = configuration.imageType === "png" ? configuration.pngOptions : configuration.jpegOptions
|
14
15
|
await page.screenshot({
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
...{
|
17
|
+
path: configuration.tempFilePath,
|
18
|
+
type: configuration.imageType,
|
19
|
+
},
|
20
|
+
...screenshotOptions
|
18
21
|
});
|
19
22
|
} catch (error) {
|
20
|
-
console.
|
23
|
+
console.error(error.message);
|
24
|
+
process.exit(1);
|
21
25
|
} finally {
|
22
26
|
if (browser) {
|
23
27
|
browser.close();
|
@@ -25,4 +29,4 @@ const createPdf = async () => {
|
|
25
29
|
process.exit();
|
26
30
|
}
|
27
31
|
};
|
28
|
-
|
32
|
+
createScreenshot();
|