Dhalang 0.6.0 → 0.6.4

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: a2357c5cdfe5dd841c64329fdab12478c719c6839fce8567520916eefc487f4e
4
- data.tar.gz: c2507de8a15eed19c3807ad43c4f64d58cd7f1a6c32a1a61a37f828fed08cab3
3
+ metadata.gz: 70f9719f301ba33dc769df19417b0b597adc574a9c7df54ed710f4f23d19e2a0
4
+ data.tar.gz: d7d6b46261a0dc227e9fb26cc79a8c7377c9148d464c2ab8e3e383d2f7115c7a
5
5
  SHA512:
6
- metadata.gz: d127ec4402220b31566af6ef16a12e375329515c60d7d6070663379e8f90315340cc0cadf9c0aa89604b5cea371c75a6f8f90af0f87ba98535125e59a614e965
7
- data.tar.gz: '055953f266b9ad18c24405e1cd8d9c455722d8dfc23ca20e0c0a3224710b5bf91e0d0b31a555403cb7f70ddd20e927de1d325e3d477b56ced98112a6a7fa2f88'
6
+ metadata.gz: 69b4cede399f0200064ec2ed56df24e42826f7b112d67d3026ba04f40139d02a72ff62bc407be94d10116adffc701ff8977a253e9ac56783402c3220d5d9989d
7
+ data.tar.gz: ea4f6c687f7fa8ab3951883ae8cbec53f0a23a71c118c3f742605e3ca4dad9b398c2d718c776718cf89ab43fb331cebe2442d7db9e1ee0f5cf9e25815567120d
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,50 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- Dhalang (0.6.0)
4
+ Dhalang (0.6.4)
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
+ arm64-darwin-21
39
+ x86-mingw32
42
40
 
43
41
  DEPENDENCIES
44
42
  Dhalang!
45
- bundler (~> 1.16)
46
- fastimage (~> 1.8)
47
- pdf-reader (~> 2.2)
48
- rake (~> 13.0.1)
43
+ bundler (~> 2.3.6)
44
+ fastimage (~> 2.2.6)
45
+ pdf-reader (~> 2.9.0)
46
+ rake (~> 13.0.6)
49
47
  rspec (~> 3.0)
50
48
 
51
49
  BUNDLED WITH
52
- 1.17.3
50
+ 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,31 +54,40 @@ 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
+ | printToPDFTimeout | Amount of milliseconds until Puppeteer while timeout when calling Page.printToPDF | 0 (unlimited) |
75
91
  | navigationWaitForSelector | If set, Dhalang will wait for the specified selector to appear before creating the screenshot or PDF | None |
76
92
  | navigationWaitForXPath | If set, Dhalang will wait for the specified XPath to appear before creating the screenshot or PDF | None |
77
93
  | userAgent | User agent to send with the request | Default Puppeteer one |
@@ -79,30 +95,30 @@ Below table lists all possible configuration parameters that can be set:
79
95
  | isAutoHeight | When set to true the height of generated PDFs will be based on the scrollHeight property of the document body | false |
80
96
  | viewPort | Custom viewport to use for the request | Default Puppeteer one |
81
97
  | httpAuthenticationCredentials | Custom HTTP authentication credentials to use for the request | None |
82
-
98
+ | 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']` | [] |
83
99
 
84
100
 
85
101
  ## Examples of using Dhalang
86
102
  To return a PDF from a Rails controller you can do the following:
87
- ```
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')
103
+ ```ruby
104
+ def example_controller_method
105
+ binary_pdf = Dhalang::PDF.get_from_url("https://www.google.com")
106
+ send_data(binary_pdf, filename: 'pdfofgoogle.pdf', type: 'application/pdf')
91
107
  end
92
108
  ```
93
109
 
94
110
  To return a PNG from a Rails controller you can do the following:
95
- ```
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')
111
+ ```ruby
112
+ def example_controller_method
113
+ binary_png = Dhalang::Screenshot.get_from_url_as_png("https://www.google.com")
114
+ send_data(binary_png, filename: 'screenshotofgoogle.png', type: 'image/png')
99
115
  end
100
116
  ```
101
117
 
102
118
  To return a JPEG from a Rails controller you can do the following:
103
- ```
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')
119
+ ```ruby
120
+ def example_controller_method
121
+ binary_jpeg = Dhalang::Screenshot.get_from_url_as_jpeg("https://www.google.com")
122
+ send_data(binary_jpeg, filename: 'screenshotofgoogle.jpeg', type: 'image/jpeg')
107
123
  end
108
124
  ```
@@ -5,15 +5,17 @@ module Dhalang
5
5
  private_constant :NODE_MODULES_PATH
6
6
 
7
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
8
+ navigationTimeout: 10000,
9
+ printToPDFTimeout: 0, # unlimited
10
+ navigationWaitUntil: 'load',
11
+ navigationWaitForSelector: '',
12
+ navigationWaitForXPath: '',
13
+ userAgent: '',
14
+ isHeadless: true,
15
+ viewPort: '',
16
+ httpAuthenticationCredentials: '',
17
+ isAutoHeight: false,
18
+ chromeOptions: []
17
19
  }
18
20
  private_constant :USER_OPTIONS
19
21
 
@@ -22,6 +24,8 @@ module Dhalang
22
24
  displayHeaderFooter: false,
23
25
  headerTemplate: '',
24
26
  footerTemplate: '',
27
+ headerTemplateFile: '',
28
+ footerTemplateFile: '',
25
29
  printBackground: true,
26
30
  landscape: false,
27
31
  pageRanges: '',
@@ -29,7 +33,8 @@ module Dhalang
29
33
  width: '',
30
34
  height: '',
31
35
  margin: { top: 36, right: 36, bottom: 20, left: 36 },
32
- preferCSSPageSiz: false
36
+ preferCSSPageSize: true,
37
+ omitBackground: false
33
38
  }
34
39
  private_constant :DEFAULT_PDF_OPTIONS
35
40
 
@@ -1,3 +1,3 @@
1
1
  module Dhalang
2
- VERSION = "0.6.0"
2
+ VERSION = "0.6.4"
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.
@@ -52,7 +54,7 @@ exports.getConfiguration = function () {
52
54
  exports.launchPuppeteer = async function (configuration) {
53
55
  module.paths.push(configuration.puppeteerPath);
54
56
  const puppeteer = require('puppeteer');
55
- 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);
56
58
  return await puppeteer.launch({
57
59
  args: launchArgs,
58
60
  headless: configuration.userOptions.isHeadless
@@ -74,7 +76,7 @@ exports.configure = async function (page, userOptions) {
74
76
  }
75
77
 
76
78
  if (userOptions.httpAuthenticationCredentials !== "") {
77
- await page.authenticate(userOptions.authenticationCredentials)
79
+ await page.authenticate(userOptions.httpAuthenticationCredentials)
78
80
  }
79
81
  }
80
82
 
@@ -107,6 +109,22 @@ exports.navigate = async function (page, configuration) {
107
109
  exports.getConfiguredPdfOptions = async function (page, configuration) {
108
110
  const pdfOptions = configuration.pdfOptions
109
111
 
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
+
120
+ if (pdfOptions['footerTemplateFile'] !== '') {
121
+ try {
122
+ pdfOptions['footerTemplate'] = fs.readFileSync(pdfOptions['footerTemplateFile'], 'utf8')
123
+ } catch(err) {
124
+ // ignoring file related errors
125
+ }
126
+ }
127
+
110
128
  if (configuration.userOptions.isAutoHeight === true) {
111
129
  const pageHeight = await page.evaluate(() => {
112
130
  return Math.max(document.body.scrollHeight, document.body.offsetHeight);
@@ -116,6 +134,8 @@ exports.getConfiguredPdfOptions = async function (page, configuration) {
116
134
  }
117
135
  }
118
136
 
137
+ pdfOptions['timeout'] = configuration.userOptions.printToPDFTimeout
138
+
119
139
  return pdfOptions
120
140
  }
121
141
 
@@ -143,4 +163,4 @@ exports.getWaitingParameters = function (configuration) {
143
163
  return {
144
164
  timeout: configuration.userOptions.navigationTimeout
145
165
  }
146
- }
166
+ }