This repository was archived by the owner on Sep 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathscreenshot.js
More file actions
176 lines (168 loc) · 5.26 KB
/
screenshot.js
File metadata and controls
176 lines (168 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const phantom = require('phantom');
const path = require('path');
const randomUserAgent = require('random-fake-useragent');
const USER_AGENT = randomUserAgent.getRandom('Chrome');
const screenshot = {
getScreenshot(url, options) {
let format = options.format;
let viewPort = options.viewPort;
let pageSize = options.pageSize;
let pageOrientation = options.pageOrientation;
let pageMarginInCm = options.pageMarginInCm;
const dpi = 72;
let fileName;
let page = null;
let instance = null;
return phantom.create([
'--ignore-ssl-errors=yes',
'--web-security=no',
'--load-images=true',
'--local-to-remote-url-access=true',
])
.then((instance_) => {
console.log('Creating instance');
instance = instance_;
return instance.createPage();
})
.then((page_) => {
console.log('Creating page');
page = page_;
page.on('onError', (msg) => {
// Silently ignore page errors
});
page.on('onLoadFinished', (msg) => {
console.log('onLoadFinished', msg);
});
page.on('onConsoleMessage', (msg) => {
console.log('From Phantom.js (console.log):', msg);
});
page.on('onCallback', (msg) => {
console.log('From Phantom.js (phantomCallback): type', typeof msg);
});
return page.property('settings', {
userAgent: USER_AGENT,
javascriptEnabled: true,
loadImages: true,
localToRemoteUrlAccessEnabled: true,
webSecurityEnabled: false,
});
})
.then(() => {
// Adapted from http://stackoverflow.com/questions/22017746/
pageSize = (pageSize || 'A4');
pageOrientation = (pageOrientation || 'portrait');
pageMarginInCm = pageMarginInCm || 0.5;
let pdfViewPortWidth;
let pdfViewPortHeight;
const cmToInchFactor = 0.393701;
let widthInInches;
let heightInInches;
switch (pageSize) {
case 'Letter':
widthInInches = 8.5;
heightInInches = 11;
break;
case 'Legal':
widthInInches = 8.5;
heightInInches = 14;
break;
case 'A3':
widthInInches = 11.69;
heightInInches = 16.54;
break;
case 'A4':
widthInInches = 8.27;
heightInInches = 11.69;
break;
case 'a5':
widthInInches = 5.83;
heightInInches = 8.27;
break;
case 'tabloid':
widthInInches = 11;
heightInInches = 17;
break;
default:
widthInInches = 8.27;
heightInInches = 11.69;
}
// Reduce by the margin (assuming 1cm margin on each side)
widthInInches -= 2 * pageMarginInCm * cmToInchFactor;
heightInInches -= 2 * pageMarginInCm * cmToInchFactor;
// Interchange if width is equal to height
if (pageOrientation === 'landscape') {
const temp = widthInInches;
widthInInches = heightInInches;
heightInInches = temp;
}
// Calculate corresponding view port dimension in pixels
pdfViewPortWidth = Math.floor(dpi * widthInInches);
pdfViewPortHeight = Math.floor(dpi * heightInInches);
viewPort = {
width: pdfViewPortWidth,
height: pdfViewPortHeight,
};
const pageSizeOptions = {
format: pageSize,
orientation: pageOrientation,
margin: pageMarginInCm + 'cm',
};
console.log('Setting page size to ' + JSON.stringify(pageSizeOptions));
return page.property('paperSize', pageSizeOptions);
})
.then(() => {
console.log('Setting view port to ' + JSON.stringify(viewPort));
return page.property('viewportSize', viewPort);
})
.then(() => {
console.log('Setting DPI to ', dpi);
return page.property('dpi', dpi);
})
.then(() => {
console.log('Start opening');
return page.open(url);
})
.then((status) => {
console.log('Status', status);
if (status !== 'success') {
return Promise.reject(Error('Can\'t open page ' + url + '. Status: ' +
status));
}
fileName = path.join('screenshots', format,
Date.now() + '.' + format);
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('Start rendering');
page.render(fileName)
.then(() => {
return resolve(fileName);
});
}, 2000);
});
})
.then(() => {
console.log('Exiting');
page.close().then(() => instance.exit());
return Promise.resolve(fileName);
}).catch((err) => {
console.error(err);
});
},
};
module.exports = screenshot;