Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions test/integration/viewer_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1302,4 +1302,57 @@ describe("PDF viewer", () => {
);
});
});

describe("Printing can be disallowed for some pdfs (bug 1978985)", () => {
let pages;

beforeEach(async () => {
pages = await loadAndWait(
"print_protection.pdf",
"#passwordDialog",
null,
null,
{ enablePermissions: true }
);
});

afterEach(async () => {
await closePages(pages);
});

it("must check that printing is disallowed", async () => {
await Promise.all(
pages.map(async ([browserName, page]) => {
await page.waitForSelector("#printButton", {
visible: true,
});

const selector = "#passwordDialog input#password";
await page.waitForSelector(selector, { visible: true });
await page.type(selector, "1234");
await page.click("#passwordDialog button#passwordSubmit");

await page.waitForSelector(".textLayer .endOfContent");

// The print button should be hidden.
await page.waitForSelector("#printButton", {
hidden: true,
});
await page.waitForSelector("#secondaryPrint", {
hidden: true,
});

const hasThrown = await page.evaluate(() => {
try {
window.print();
} catch {
return true;
}
return false;
});
expect(hasThrown).withContext(`In ${browserName}`).toBeTrue();
})
);
});
});
});
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -738,3 +738,4 @@
!bug1978317.pdf
!dates.pdf
!dates_save.pdf
!print_protection.pdf
Binary file added test/pdfs/print_protection.pdf
Binary file not shown.
42 changes: 37 additions & 5 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ const PDFViewerApplication = {
_caretBrowsing: null,
_isScrolling: false,
editorUndoBar: null,
_printPermissionPromise: null,

// Called once when the document is loaded.
async initialize(appConfig) {
Expand Down Expand Up @@ -369,6 +370,7 @@ const PDFViewerApplication = {
enableAutoLinking: x => x === "true",
enableFakeMLManager: x => x === "true",
enableGuessAltText: x => x === "true",
enablePermissions: x => x === "true",
enableUpdatedAddImage: x => x === "true",
highlightEditorColors: x => x,
maxCanvasPixels: x => parseInt(x),
Expand Down Expand Up @@ -407,6 +409,7 @@ const PDFViewerApplication = {
)
: new EventBus();
this.eventBus = AppOptions.eventBus = eventBus;

mlManager?.setEventBus(eventBus, abortSignal);

const overlayManager = (this.overlayManager = new OverlayManager());
Expand Down Expand Up @@ -798,9 +801,19 @@ const PDFViewerApplication = {
});
}

const togglePrintingButtons = visible => {
appConfig.toolbar?.print?.classList.toggle("hidden", !visible);
appConfig.secondaryToolbar?.printButton.classList.toggle(
"hidden",
!visible
);
};
if (!this.supportsPrinting) {
appConfig.toolbar?.print?.classList.add("hidden");
appConfig.secondaryToolbar?.printButton.classList.add("hidden");
togglePrintingButtons(false);
} else {
eventBus.on("printingallowed", ({ isAllowed }) =>
togglePrintingButtons(isAllowed)
);
}

if (!this.supportsFullscreen) {
Expand Down Expand Up @@ -1335,6 +1348,25 @@ const PDFViewerApplication = {
load(pdfDocument) {
this.pdfDocument = pdfDocument;

this._printPermissionPromise = new Promise(resolve => {
this.eventBus.on(
"printingallowed",
({ isAllowed }) => {
if (
typeof PDFJSDev !== "undefined" &&
PDFJSDev.test("MOZCENTRAL") &&
Comment thread
calixteman marked this conversation as resolved.
!isAllowed
) {
window.print = () => {
console.warn("Printing is not allowed.");
};
}
resolve(isAllowed);
},
{ once: true }
);
});

pdfDocument.getDownloadInfo().then(({ length }) => {
this._contentLength = length; // Ensure that the correct length is used.
this.loadingBar?.hide();
Expand Down Expand Up @@ -1893,7 +1925,7 @@ const PDFViewerApplication = {
return;
}

if (!this.supportsPrinting) {
if (!this.supportsPrinting || !this.pdfViewer.printingAllowed) {
this._otherError("pdfjs-printing-not-supported");
return;
}
Expand Down Expand Up @@ -1961,8 +1993,8 @@ const PDFViewerApplication = {
this.pdfPresentationMode?.request();
},

triggerPrinting() {
if (this.supportsPrinting) {
async triggerPrinting() {
if (this.supportsPrinting && (await this._printPermissionPromise)) {
window.print();
}
},
Expand Down
4 changes: 4 additions & 0 deletions web/pdf_print_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ window.print = function () {
dispatchEvent("beforeprint");
} finally {
if (!activeService) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("TESTING")) {
// eslint-disable-next-line no-unsafe-finally
throw new Error("window.print() is not supported");
Comment thread
calixteman marked this conversation as resolved.
}
console.error("Expected print service to be initialized.");
ensureOverlay().then(function () {
overlayManager.closeIfActive(dialog);
Expand Down
22 changes: 22 additions & 0 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ class PDFViewer {

#mlManager = null;

#printingAllowed = true;

#scrollTimeoutId = null;

#switchAnnotationEditorModeAC = null;
Expand Down Expand Up @@ -415,6 +417,10 @@ class PDFViewer {
}
}

get printingAllowed() {
return this.#printingAllowed;
}

get pagesCount() {
return this._pages.length;
}
Expand Down Expand Up @@ -672,9 +678,23 @@ class PDFViewer {
textLayerMode: this.#textLayerMode,
};
if (!permissions) {
this.#printingAllowed = true;
this.eventBus.dispatch("printingallowed", {
source: this,
isAllowed: this.#printingAllowed,
});

return params;
}

this.#printingAllowed =
permissions.includes(PermissionFlag.PRINT_HIGH_QUALITY) ||
permissions.includes(PermissionFlag.PRINT);
this.eventBus.dispatch("printingallowed", {
source: this,
isAllowed: this.#printingAllowed,
});

if (
!permissions.includes(PermissionFlag.COPY) &&
this.#textLayerMode === TextLayerMode.ENABLE
Expand Down Expand Up @@ -843,6 +863,8 @@ class PDFViewer {

this.#annotationEditorUIManager?.destroy();
this.#annotationEditorUIManager = null;

this.#printingAllowed = true;
}

this.pdfDocument = pdfDocument;
Expand Down