Skip to content

Commit ffa45a2

Browse files
committed
Split the renderImage function into two evaluations to get the final size of the capture rect correct. Need to resize the viewport before determining the size of the capture area.
1 parent fb35584 commit ffa45a2

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "capture-template",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"description": "This library is responsible for expanding a template web page and then capturing it PNG or PDF.",
55
"main": "build/index.js",
66
"types": "build/index.d.ts",

src/web-page-renderer.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,21 @@ export class WebPageRenderer implements IWebPageRenderer {
151151
this.preRenderCheck(options);
152152
this.nightmare.goto(webPageUrl);
153153
this.nightmare.wait(options.waitSelector);
154-
await this.nightmare.evaluate(
154+
await this.nightmare.evaluate(() => {
155+
const body = document.querySelector('body');
156+
return {
157+
width: body.scrollWidth,
158+
height: body.scrollHeight,
159+
};
160+
})
161+
.then((bodySize: any) => {
162+
return this.nightmare.viewport(bodySize.width, bodySize.height)
163+
})
164+
this.nightmare.evaluate(
155165
(captureSelector: string) => {
156-
const body = document.querySelector('body');
157166
const element = document.querySelector(captureSelector);
158167
const rect = element.getBoundingClientRect();
159168
return {
160-
bodyWidth: body.scrollWidth,
161-
bodyHeight: body.scrollHeight,
162169
x: Math.ceil(rect.left),
163170
y: Math.ceil(rect.top),
164171
height: Math.ceil(rect.bottom - rect.top),
@@ -168,9 +175,7 @@ export class WebPageRenderer implements IWebPageRenderer {
168175
options.captureSelector || options.waitSelector,
169176
)
170177
.then((rect: any) => {
171-
return this.nightmare
172-
.viewport(rect.bodyWidth, rect.bodyHeight)
173-
.screenshot(outputFilePath, rect);
178+
return this.nightmare.screenshot(outputFilePath, rect);
174179
});
175180
}
176181

0 commit comments

Comments
 (0)