This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b580efb
commit 2b07559
Showing
2 changed files
with
101 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { remote, WebviewTag } from 'electron'; | ||
import * as FileExtraUtils from 'fs-extra'; | ||
|
||
export class PngExporter { | ||
public static exportToPng(path: string, webview: WebviewTag): void { | ||
// code that gets executed inside the webview to get | ||
// the actual size of the preview body | ||
const code = ` | ||
bodyTag = document.querySelector('body'); | ||
({ | ||
pageHeight: bodyTag.getBoundingClientRect().height, | ||
pageWidth: bodyTag.getBoundingClientRect().width | ||
}); | ||
`; | ||
|
||
webview.executeJavaScript(code, false, webviewSize => { | ||
// set the height of the webview tag to the preview body height | ||
// This is needed because capturePage can not capture anything that renders | ||
// outside the webview area (https://github.com/electron/electron/issues/9845) | ||
webview.style.height = webviewSize.pageHeight; | ||
|
||
// Delay the page capture to make sure that the style height changes are done. | ||
// This is only needed because of the change in height in the above line | ||
setTimeout(() => { | ||
const scaleFactor = remote.screen.getPrimaryDisplay().scaleFactor; | ||
webview.capturePage( | ||
{ | ||
x: 0, | ||
y: 0, | ||
// round the numbers to remove possible floating numbers | ||
// also multiply by scaleFactor for devices with higher pixel ratio: | ||
// https://github.com/electron/electron/issues/8314 | ||
width: Math.round(webviewSize.pageWidth * scaleFactor), | ||
height: Math.round(webviewSize.pageHeight * scaleFactor) | ||
}, | ||
capture => { | ||
const pngBuffer: Buffer = capture.toPNG(); | ||
FileExtraUtils.writeFileSync(path, pngBuffer); | ||
|
||
// reset the webview height | ||
webview.style.height = '100%'; | ||
} | ||
); | ||
}, 100); | ||
}); | ||
} | ||
} |