[JS] Some functions (print, alert,...) must be called only after a user activation

- Some events, which require a user interaction, will allow those functions to be called.
But after few seconds, if there are no more user interaction, it won't be possible
anymore.
The idea is to give an opportunity to the user to leave the pdf.
- Disable print function when we're printing, the same with saving and disallow to save
on open events.
This commit is contained in:
Calixte Denizet 2022-10-24 18:56:52 +02:00
parent 27b251ac99
commit 0de804a256
4 changed files with 103 additions and 5 deletions

View file

@ -96,6 +96,9 @@ class Doc extends PDFObject {
this._actions = createActionsMap(data.actions);
this._globalEval = data.globalEval;
this._pageActions = new Map();
this._userActivation = false;
this._disablePrinting = false;
this._disableSaving = false;
}
_dispatchDocEvent(name) {
@ -108,12 +111,27 @@ class Doc extends PDFObject {
"DidPrint",
"OpenAction",
]);
// When a pdf has just been opened it doesn't really make sense
// to save it: it's up to the user to decide if they want to do that.
// A pdf can contain an action /FooBar which will trigger a save
// even if there are no WillSave/DidSave (which are themselves triggered
// after a save).
this._disableSaving = true;
for (const actionName of this._actions.keys()) {
if (!dontRun.has(actionName)) {
this._runActions(actionName);
}
}
this._runActions("OpenAction");
this._disableSaving = false;
} else if (name === "WillPrint") {
this._disablePrinting = true;
this._runActions(name);
this._disablePrinting = false;
} else if (name === "WillSave") {
this._disableSaving = true;
this._runActions(name);
this._disableSaving = false;
} else {
this._runActions(name);
}
@ -361,6 +379,11 @@ class Doc extends PDFObject {
}
set layout(value) {
if (!this._userActivation) {
return;
}
this._userActivation = false;
if (typeof value !== "string") {
return;
}
@ -480,6 +503,11 @@ class Doc extends PDFObject {
}
set pageNum(value) {
if (!this._userActivation) {
return;
}
this._userActivation = false;
if (typeof value !== "number" || value < 0 || value >= this._numPages) {
return;
}
@ -628,6 +656,11 @@ class Doc extends PDFObject {
}
set zoomType(type) {
if (!this._userActivation) {
return;
}
this._userActivation = false;
if (typeof type !== "string") {
return;
}
@ -662,6 +695,11 @@ class Doc extends PDFObject {
}
set zoom(value) {
if (!this._userActivation) {
return;
}
this._userActivation = false;
if (typeof value !== "number" || value < 8.33 || value > 6400) {
return;
}
@ -1057,6 +1095,11 @@ class Doc extends PDFObject {
bAnnotations = true,
printParams = null
) {
if (this._disablePrinting || !this._userActivation) {
return;
}
this._userActivation = false;
if (bUI && typeof bUI === "object") {
nStart = bUI.nStart;
nEnd = bUI.nEnd;