Dhalang 0.5.0 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4bd71cb9e8eb643b85d2d2e68909db725b088b10ef29cd717634bc5429067ed4
4
- data.tar.gz: 77b10c45d0c30132e3888c2cc04998502b34ae24a0e363e70b971987ec6a722c
3
+ metadata.gz: affc5a7a677cade8adf37073ab71155ad812fdb0765c947c560ecb8de50dafc3
4
+ data.tar.gz: 72a7122ea73080759aef9d5ccd375167da9efffee1836f92df990c7046df08d7
5
5
  SHA512:
6
- metadata.gz: d36638644a8b02e167b76f4b943b527fd06dd20cdc6ef4c77ce37af1e41bb24f4f03f13549b0b3cfcd9a1693b7aad535448ebbfd01b6a54cb53be39d32bbd063
7
- data.tar.gz: e31806a5c74557130c558eda263a0b72045814db398c1554fe5bfaa5ce0240f14b22b52ce1afd1d8c488936097a8c407ff255c53c166cf346054d57cff749c80
6
+ metadata.gz: a007aa999b92d55b8d10bf6e0683d9f5ddf833f5a3f26914abfbdda562a25bb197ba29595f4d55edbcd9c11ea15eb0a2c57da8bb5595e38f521e5a465c13aa24
7
+ data.tar.gz: 2bdf23451af29feaa0ba70b78c7af7ca603bc02db2dfffcbbd815dd766b4fc99972328a7cf2d6b15dad23dadfc51aa8f3ea10713652ed087df4fa46890211f3e
data/.travis.yml CHANGED
@@ -1,13 +1,21 @@
1
1
  language: ruby
2
2
  node_js:
3
- - node
3
+ - node
4
4
  rvm:
5
- - 2.6
5
+ - 2.6
6
6
  addons:
7
7
  chrome: stable
8
8
  before_install:
9
- - nvm install v15.0.1
9
+ - nvm install v15.0.1
10
10
  install:
11
- - bundle install
12
- - npm install
13
- script: bundle exec rake spec
11
+ - bundle install
12
+ - npm install
13
+ script: bundle exec rake spec
14
+ deploy:
15
+ provider: rubygems
16
+ gemspec: Dhalang.gemspec
17
+ api_key:
18
+ secure: PMiaYF8Xtyyl9Kuhf9WwDRaUzUcqbe430DF8Rwk2qYM1oI8eiYbESswHVavAnrT+ZLKZVzp0+avXJ1TFjoz2tHBa7YVmmz76+RvECozwBXFVofK0GX8c6PVZTuuHgoanr1RVv0KTJOZXs5Aa+372Er31ESuwaaRPU2y/d8aotxtoe7MUoVle67S7Z28H53jwDCut54WFq6hKi+gNO/B+7y/e2efPgrqSktHxaPDGcaUaX9v8fAYBJgAulJLYMeRj+m7IQccajIjppr7vWDE1m9XEDAK71i3EZis96/z+KexB2bo3+Qc+TvpTWP5NeKw61uhtPn9HU4+XCaOV8Vz0e6PH2JfS82h333BcSifXjcFb9iQU8aEMpSpcjraUvvvu8JZepUUO//vtGEJLyOJ0L4f9XiUPhQ15OYjBsJ2ADFdNcqyFeASDuLoeKbAQrWunyTStb+wFrr5TzAoQp9ItZbEt6eHaI6WSd4qhJfzqU7NOzE06Ns03J1jMdmzMxREl8ENJFle65fQouQfRnfcZTS6qW7UX/7YZUiMw60Cpa15qHW+92glCkOuiZUhr4D999ejqMFY5Jd3tHGVBX0/1MWc2jl0fPk2/fueA/YW8xLd3xKP9p0KhbqWZhiMxIct5w718nfOLuSoL1VLJRImj4HAKmoCFKP4RE+1Vl95UwEY=
19
+ on:
20
+ branch: master
21
+ tags: true
data/Dhalang.gemspec CHANGED
@@ -22,9 +22,9 @@ Gem::Specification.new do |spec|
22
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  spec.require_paths = ["lib"]
24
24
 
25
- spec.add_development_dependency "bundler", "~> 1.16"
26
- spec.add_development_dependency "rake", "~> 13.0.1"
25
+ spec.add_development_dependency "bundler", "~> 2.3.6"
26
+ spec.add_development_dependency "fastimage", "~> 2.2.6"
27
+ spec.add_development_dependency "pdf-reader", "~> 2.9.0"
28
+ spec.add_development_dependency "rake", "~> 13.0.6"
27
29
  spec.add_development_dependency "rspec", "~> 3.0"
28
- spec.add_development_dependency "pdf-reader", "~> 2.2"
29
- spec.add_development_dependency "fastimage", "~> 1.8"
30
30
  end
data/Gemfile.lock CHANGED
@@ -1,52 +1,49 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- Dhalang (0.3.1)
4
+ Dhalang (0.6.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- Ascii85 (1.0.3)
10
- addressable (2.3.8)
9
+ Ascii85 (1.1.0)
11
10
  afm (0.2.2)
12
- diff-lcs (1.3)
13
- fastimage (1.9.0)
14
- addressable (~> 2.3.5)
11
+ diff-lcs (1.5.0)
12
+ fastimage (2.2.6)
15
13
  hashery (2.1.2)
16
- pdf-reader (2.4.0)
17
- Ascii85 (~> 1.0.0)
14
+ pdf-reader (2.9.0)
15
+ Ascii85 (~> 1.0)
18
16
  afm (~> 0.2.1)
19
17
  hashery (~> 2.0)
20
18
  ruby-rc4
21
19
  ttfunk
22
- rake (13.0.1)
23
- rspec (3.9.0)
24
- rspec-core (~> 3.9.0)
25
- rspec-expectations (~> 3.9.0)
26
- rspec-mocks (~> 3.9.0)
27
- rspec-core (3.9.1)
28
- rspec-support (~> 3.9.1)
29
- rspec-expectations (3.9.0)
20
+ rake (13.0.6)
21
+ rspec (3.10.0)
22
+ rspec-core (~> 3.10.0)
23
+ rspec-expectations (~> 3.10.0)
24
+ rspec-mocks (~> 3.10.0)
25
+ rspec-core (3.10.2)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-expectations (3.10.2)
30
28
  diff-lcs (>= 1.2.0, < 2.0)
31
- rspec-support (~> 3.9.0)
32
- rspec-mocks (3.9.1)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-mocks (3.10.3)
33
31
  diff-lcs (>= 1.2.0, < 2.0)
34
- rspec-support (~> 3.9.0)
35
- rspec-support (3.9.2)
32
+ rspec-support (~> 3.10.0)
33
+ rspec-support (3.10.3)
36
34
  ruby-rc4 (0.1.5)
37
- ttfunk (1.6.2.1)
35
+ ttfunk (1.7.0)
38
36
 
39
37
  PLATFORMS
40
- ruby
41
- x64-mingw32
38
+ x86-mingw32
42
39
 
43
40
  DEPENDENCIES
44
41
  Dhalang!
45
- bundler (~> 1.16)
46
- fastimage (~> 1.8)
47
- pdf-reader (~> 2.2)
48
- rake (~> 13.0.1)
42
+ bundler (~> 2.3.6)
43
+ fastimage (~> 2.2.6)
44
+ pdf-reader (~> 2.9.0)
45
+ rake (~> 13.0.6)
49
46
  rspec (~> 3.0)
50
47
 
51
48
  BUNDLED WITH
52
- 1.17.3
49
+ 2.3.6
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2019 NielsS
3
+ Copyright (c) 2021 NielsS
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -27,18 +27,25 @@ Install puppeteer in your application's root directory:
27
27
  <sub>NodeJS v10.18.1 or greater is required</sub>
28
28
  ## Usage
29
29
  __Get a PDF of a website url__
30
- `Dhalang::PDF.get_from_url("https://www.google.com")`
30
+ ```ruby
31
+ Dhalang::PDF.get_from_url("https://www.google.com")
32
+ ```
31
33
  It is important to pass the complete url, leaving out https://, http:// or www. will result in an error.
32
-
34
+
33
35
  __Get a PDF of a HTML string__
34
- `Dhalang::PDF.get_from_html("<html><head></head><body><h1>examplestring</h1></body></html>")`
36
+ ```ruby
37
+ Dhalang::PDF.get_from_html("<html><head></head><body><h1>examplestring</h1></body></html>")
38
+ ```
35
39
 
36
40
  __Get a PNG screenshot of a website__
37
- `Dhalang::Screenshot.get_from_url_as_png("https://www.google.com")`
41
+ ```ruby
42
+ Dhalang::Screenshot.get_from_url_as_png("https://www.google.com")
43
+ ```
38
44
 
39
45
  __Get a JPEG screenshot of a website__
40
- `Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com")`
41
-
46
+ ```ruby
47
+ Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com")
48
+ ```
42
49
  All methods return a string containing the PDF or JPEG/PNG in binary.
43
50
 
44
51
 
@@ -47,59 +54,70 @@ All methods return a string containing the PDF or JPEG/PNG in binary.
47
54
  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
55
 
49
56
  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
- `
57
+ ```ruby
58
+ Dhalang::PDF.get_from_url("https://www.google.com", {margin: { top: 100, right: 100, bottom: 100, left: 100}})
59
+ ```
53
60
 
54
61
  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
- `
62
+ ```ruby
63
+ Dhalang::Screenshot.get_from_url_as_png("https://www.google.com", {fullPage: false})
64
+ ```
57
65
 
58
66
  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
67
 
60
68
  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
69
 
62
-
70
+ > The default Puppeteer options contain the options `headerTemplate` and `footerTemplate`. Puppeteer expects these to be HTML strings. By default, the Dhalang
71
+ > gem passes all options as arguments in a `node ...` shell command. In case the HTML strings are too long they might surpass the maximum
72
+ > argument length of the host. For example, on Linux the `MAX_ARG_LEN` is 128kB. Therefore, you can also pass the headers and footers as file path using the
73
+ > options `headerTemplateFile` and `footerTemplateFile`. These non-Puppeteer-options will be used to populate the Puppeteer-options `headerTemplate` and `footerTemplate`.
74
+ >
75
+ > For example: `Dhalang::PDF.get_from_url("https://www.google.com", {headerTemplateFile: '/tmp/header.html', footerTemplateFile: '/tmp/footer.html'})`
63
76
 
64
77
 
65
78
  ## Custom user options
66
79
  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
80
 
68
81
  For example to set a custom navigation timeout:
69
- `Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com", {navigationTimeout: 20000})`
82
+ ```ruby
83
+ Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com", {navigationTimeout: 20000})
84
+ ```
70
85
 
71
86
  Below table lists all possible configuration parameters that can be set:
72
87
  | Key | Description | Default |
73
88
  |--------------------|-----------------------------------------------------------------------------------------|---------------------------------|
74
89
  | navigationTimeout | Amount of milliseconds until Puppeteer while timeout when navigating to the given page | 10000 |
90
+ | navigationWaitForSelector | If set, Dhalang will wait for the specified selector to appear before creating the screenshot or PDF | None |
91
+ | navigationWaitForXPath | If set, Dhalang will wait for the specified XPath to appear before creating the screenshot or PDF | None |
75
92
  | userAgent | User agent to send with the request | Default Puppeteer one |
76
93
  | isHeadless | Indicates if Chromium should be launched headless | true |
94
+ | isAutoHeight | When set to true the height of generated PDFs will be based on the scrollHeight property of the document body | false |
77
95
  | viewPort | Custom viewport to use for the request | Default Puppeteer one |
78
96
  | httpAuthenticationCredentials | Custom HTTP authentication credentials to use for the request | None |
79
-
97
+ | chromeOptions | A array of [options](https://peter.sh/experiments/chromium-command-line-switches/) that can be passed to puppeteer in addition to the mandatory `['--no-sandbox', '--disable-setuid-sandbox']` | [] |
80
98
 
81
99
 
82
100
  ## Examples of using Dhalang
83
101
  To return a PDF from a Rails controller you can do the following:
84
- ```
85
- def example_controller_method
86
- binary_pdf = Dhalang::PDF.get_from_url("https://www.google.com")
87
- send_data(binary_pdf, filename: 'pdfofgoogle.pdf', type: 'application/pdf')
102
+ ```ruby
103
+ def example_controller_method
104
+ binary_pdf = Dhalang::PDF.get_from_url("https://www.google.com")
105
+ send_data(binary_pdf, filename: 'pdfofgoogle.pdf', type: 'application/pdf')
88
106
  end
89
107
  ```
90
108
 
91
109
  To return a PNG from a Rails controller you can do the following:
92
- ```
93
- def example_controller_method
94
- binary_png = Dhalang::Screenshot.get_from_url_as_png("https://www.google.com")
95
- send_data(binary_png, filename: 'screenshotofgoogle.png', type: 'image/png')
110
+ ```ruby
111
+ def example_controller_method
112
+ binary_png = Dhalang::Screenshot.get_from_url_as_png("https://www.google.com")
113
+ send_data(binary_png, filename: 'screenshotofgoogle.png', type: 'image/png')
96
114
  end
97
115
  ```
98
116
 
99
117
  To return a JPEG from a Rails controller you can do the following:
100
- ```
101
- def example_controller_method
102
- binary_jpeg = Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com")
103
- send_data(binary_jpeg, filename: 'screenshotofgoogle.jpeg', type: 'image/jpeg')
118
+ ```ruby
119
+ def example_controller_method
120
+ binary_jpeg = Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com")
121
+ send_data(binary_jpeg, filename: 'screenshotofgoogle.jpeg', type: 'image/jpeg')
104
122
  end
105
123
  ```
@@ -7,10 +7,14 @@ module Dhalang
7
7
  USER_OPTIONS = {
8
8
  navigationTimeout: 10000,
9
9
  navigationWaitUntil: 'load',
10
+ navigationWaitForSelector: '',
11
+ navigationWaitForXPath: '',
10
12
  userAgent: '',
11
13
  isHeadless: true,
12
14
  viewPort: '',
13
- httpAuthenticationCredentials: ''
15
+ httpAuthenticationCredentials: '',
16
+ isAutoHeight: false,
17
+ chromeOptions: []
14
18
  }
15
19
  private_constant :USER_OPTIONS
16
20
 
@@ -19,6 +23,8 @@ module Dhalang
19
23
  displayHeaderFooter: false,
20
24
  headerTemplate: '',
21
25
  footerTemplate: '',
26
+ headerTemplateFile: '',
27
+ footerTemplateFile: '',
22
28
  printBackground: true,
23
29
  landscape: false,
24
30
  pageRanges: '',
@@ -26,7 +32,8 @@ module Dhalang
26
32
  width: '',
27
33
  height: '',
28
34
  margin: { top: 36, right: 36, bottom: 20, left: 36 },
29
- preferCSSPageSiz: false
35
+ preferCSSPageSize: true,
36
+ omitBackground: false
30
37
  }
31
38
  private_constant :DEFAULT_PDF_OPTIONS
32
39
 
@@ -1,3 +1,3 @@
1
1
  module Dhalang
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.3"
3
3
  end
data/lib/js/dhalang.js CHANGED
@@ -1,3 +1,5 @@
1
+ const fs = require('fs')
2
+
1
3
  /**
2
4
  * @typedef {Object} Configuration
3
5
  * @property {string} webPageUrl - The url of the webpage to visit.
@@ -5,7 +7,7 @@
5
7
  * @property {string} puppeteerModulePath - The path of the Puppeteer module.
6
8
  * @property {string} imageType - The type of image to save ( undefined for pdfgenerator ).
7
9
  * @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.
10
+ * @property {Object} pdfOptions - User defined and default parameters to use when creating PDFs. Note: Do not use directly, rather use {@link getConfiguredPdfOptions}.
9
11
  * @property {Object} pngOptions - User defined and default parameters to use when creating PNGs.
10
12
  * @property {Object} jpegOptions - User defined and default parameters to use when creating JPEGs.
11
13
  */
@@ -14,10 +16,13 @@
14
16
  * @typedef {Object} UserOptions
15
17
  * @property {number} navigationTimeout - Maximum in milliseconds until navigation times out, we use a default of 10 seconds as timeout.
16
18
  * @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 ).
19
+ * @property {string} navigationWaitForSelector - If set, specifies the selector Puppeteer should wait for to appear before continuing.
20
+ * @property {string} navigationWaitForXPath - If set, specifies the XPath Puppeteer should wait for to appear before continuing.
17
21
  * @property {string} userAgent - The user agent to send with requests.
18
22
  * @property {boolean} isHeadless - Indicates if Puppeteer should launch Chromium in headless mode.
19
23
  * @property {Object} viewPort - The view port to use.
20
24
  * @property {Object} httpAuthenticationCredentials - The credentials to use for HTTP authentication.
25
+ * @property {boolean} isAutoHeight - The height is automatically set
21
26
  */
22
27
 
23
28
  /**
@@ -26,6 +31,11 @@
26
31
  * @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 ).
27
32
  */
28
33
 
34
+ /**
35
+ * @typedef {Object} WaitingParameters
36
+ * @property {number} timeout - Maximum in milliseconds until navigation times out, we use a default of 10 seconds as timeout.
37
+ */
38
+
29
39
  /**
30
40
  * Parses the given configuration process argument from Ruby to a JS object.
31
41
  * @returns {Configuration}
@@ -38,13 +48,13 @@ exports.getConfiguration = function () {
38
48
  /**
39
49
  * Launches Puppeteer and returns its instance.
40
50
  * @param {UserOptions} configuration - The configuration to use.
41
- * @returns {Promise<Object>}
51
+ * @returns {Promise<Object>}
42
52
  * The launched instance of Puppeteer.
43
53
  */
44
54
  exports.launchPuppeteer = async function (configuration) {
45
55
  module.paths.push(configuration.puppeteerPath);
46
56
  const puppeteer = require('puppeteer');
47
- const launchArgs = ['--no-sandbox', '--disable-setuid-sandbox'];
57
+ const launchArgs = ['--no-sandbox', '--disable-setuid-sandbox'].concat(configuration.userOptions.chromeOptions).filter((item, index, self) => self.indexOf(item) === index);
48
58
  return await puppeteer.launch({
49
59
  args: launchArgs,
50
60
  headless: configuration.userOptions.isHeadless
@@ -52,28 +62,84 @@ exports.launchPuppeteer = async function (configuration) {
52
62
  }
53
63
 
54
64
  /**
55
- * Configures the given Puppeteer page.
65
+ * Configures the given Puppeteer page object.
66
+ * @param {Object} page - The Puppeteer page object to configure.
67
+ * @param {UserOptions} userOptions - The user options to use.
68
+ */
69
+ exports.configure = async function (page, userOptions) {
70
+ if (userOptions.userAgent !== "") {
71
+ await page.setUserAgent(userOptions.userAgent)
72
+ }
73
+
74
+ if (userOptions.viewPort !== "") {
75
+ await page.setViewport(userOptions.viewPort)
76
+ }
77
+
78
+ if (userOptions.httpAuthenticationCredentials !== "") {
79
+ await page.authenticate(userOptions.httpAuthenticationCredentials)
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Makes the Puppeteer page object open the url with the specified navigation logic as specified in the given configuration.
85
+ * @param {Object} page - The Puppeteer page object to use for navigation.
86
+ * @param {Configuration} configuration - The configuration to use.
87
+ */
88
+ exports.navigate = async function (page, configuration) {
89
+ const navigationWaitForSelector = configuration.userOptions.navigationWaitForSelector;
90
+ const navigationWaitForXPath = configuration.userOptions.navigationWaitForXPath;
91
+
92
+ await page.goto(configuration.webPageUrl, this.getNavigationParameters(configuration));
93
+
94
+ if (navigationWaitForSelector !== "") {
95
+ await page.waitForSelector(navigationWaitForSelector, this.getWaitingParameters(configuration));
96
+ } else if (navigationWaitForXPath !== "") {
97
+ await page.waitForXPath(navigationWaitForXPath, this.getWaitingParameters(configuration));
98
+ } else {
99
+ await page.waitForTimeout(250);
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Returns the PDF options to pass to Puppeteer based on the set user options and the documents body.
56
105
  * @param {Object} page - The Puppeteer page to configure.
57
106
  * @param {UserOptions} configuration - The configuration to use.
107
+ * @returns {Object} - pdfOptions
58
108
  */
59
- exports.configurePage = async function (page, configuration) {
60
- if (configuration.userAgent !== "") {
61
- await page.setUserAgent(configuration.userAgent)
62
- }
109
+ exports.getConfiguredPdfOptions = async function (page, configuration) {
110
+ const pdfOptions = configuration.pdfOptions
63
111
 
64
- if (configuration.viewPort !== "") {
65
- await page.setViewport(configuration.viewPort)
112
+ if (pdfOptions['headerTemplateFile'] !== '') {
113
+ try {
114
+ pdfOptions['headerTemplate'] = fs.readFileSync(pdfOptions['headerTemplateFile'], 'utf8')
115
+ } catch(err) {
116
+ // ignoring file related errors
117
+ }
118
+ }
119
+ if (pdfOptions['footerTemplateFile'] !== '') {
120
+ try {
121
+ pdfOptions['footerTemplate'] = fs.readFileSync(pdfOptions['footerTemplateFile'], 'utf8')
122
+ } catch(err) {
123
+ // ignoring file related errors
124
+ }
66
125
  }
67
126
 
68
- if (configuration.httpAuthenticationCredentials !== "") {
69
- await page.authenticate(configuration.authenticationCredentials)
127
+ if (configuration.userOptions.isAutoHeight === true) {
128
+ const pageHeight = await page.evaluate(() => {
129
+ return Math.max(document.body.scrollHeight, document.body.offsetHeight);
130
+ })
131
+ if (pageHeight) {
132
+ pdfOptions['height'] = pageHeight + 1 + 'px'
133
+ }
70
134
  }
135
+
136
+ return pdfOptions
71
137
  }
72
138
 
73
139
  /**
74
140
  * Extracts the navigation parameters from the configuration in a format that is usable by Puppeteer.
75
141
  * @param {Configuration} configuration - The configuration to extract the navigation parameters from.
76
- * @returns {NavigationParameters}
142
+ * @returns {NavigationParameters}
77
143
  * The extracted navigation parameters.
78
144
  */
79
145
  exports.getNavigationParameters = function (configuration) {
@@ -81,4 +147,17 @@ exports.getNavigationParameters = function (configuration) {
81
147
  timeout: configuration.userOptions.navigationTimeout,
82
148
  waituntil: configuration.userOptions.navigationWaitUntil
83
149
  }
84
- }
150
+ }
151
+
152
+
153
+ /**
154
+ * Extracts the waiting parameters from the configuration in a format that is usable by Puppeteer.
155
+ * @param {Configuration} configuration - The configuration to extract the waiting parameters from.
156
+ * @returns {WaitingParameters}
157
+ * The extracted waiting parameters.
158
+ */
159
+ exports.getWaitingParameters = function (configuration) {
160
+ return {
161
+ timeout: configuration.userOptions.navigationTimeout
162
+ }
163
+ }
@@ -8,14 +8,14 @@ const createPdf = async () => {
8
8
  try {
9
9
  browser = await dhalang.launchPuppeteer(configuration);
10
10
  const page = await browser.newPage();
11
- await dhalang.configurePage(page, configuration.userOptions);
12
- await page.goto(configuration.webPageUrl, dhalang.getNavigationParameters(configuration));
13
- await page.waitForTimeout(250);
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
- ...configuration.pdfOptions
18
+ ...pdfOptions
19
19
  });
20
20
  } catch (error) {
21
21
  console.error(error.message);
@@ -1,16 +1,16 @@
1
1
  'use strict';
2
2
  const dhalang = require('./dhalang')
3
3
 
4
- const createPdf = async () => {
4
+ const createScreenshot = async () => {
5
5
  const configuration = dhalang.getConfiguration();
6
6
 
7
7
  let browser;
8
8
  try {
9
9
  browser = await dhalang.launchPuppeteer(configuration);
10
10
  const page = await browser.newPage();
11
- await dhalang.configurePage(page, configuration.userOptions);
12
- await page.goto(configuration.webPageUrl, dhalang.getNavigationParameters(configuration));
13
- await page.waitForTimeout(250);
11
+ await dhalang.configure(page, configuration.userOptions);
12
+ await dhalang.navigate(page, configuration);
13
+
14
14
  const screenshotOptions = configuration.imageType === "png" ? configuration.pngOptions : configuration.jpegOptions
15
15
  await page.screenshot({
16
16
  ...{
@@ -29,4 +29,4 @@ const createPdf = async () => {
29
29
  process.exit();
30
30
  }
31
31
  };
32
- createPdf();
32
+ createScreenshot();