2016-10-08 21:36:55 +09:00
|
|
|
/* Copyright 2016 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2024-02-25 22:12:36 +09:00
|
|
|
import {
|
|
|
|
AnnotationMode,
|
|
|
|
PixelsPerInch,
|
|
|
|
RenderingCancelledException,
|
|
|
|
shadow,
|
|
|
|
} from "pdfjs-lib";
|
2021-06-18 18:48:29 +09:00
|
|
|
import { getXfaHtmlForPrinting } from "./print_utils.js";
|
2017-03-28 08:07:27 +09:00
|
|
|
|
2017-05-26 21:52:23 +09:00
|
|
|
let activeService = null;
|
2022-03-25 22:10:22 +09:00
|
|
|
let dialog = null;
|
2017-05-26 21:52:23 +09:00
|
|
|
let overlayManager = null;
|
2024-02-09 20:01:59 +09:00
|
|
|
let viewerApp = { initialized: false };
|
2017-03-28 08:07:27 +09:00
|
|
|
|
|
|
|
// Renders the page to the canvas of the given print service, and returns
|
|
|
|
// the suggested dimensions of the output page.
|
2020-08-05 20:24:32 +09:00
|
|
|
function renderPage(
|
|
|
|
activeServiceOnEntry,
|
|
|
|
pdfDocument,
|
|
|
|
pageNumber,
|
|
|
|
size,
|
[api-minor] Add support for toggling of Optional Content in the viewer (issue 12096)
*Besides, obviously, adding viewer support:* This patch attempts to improve the general API for Optional Content Groups slightly, by adding a couple of new methods for interacting with the (more complex) data structures of `OptionalContentConfig`-instances. (Thus allowing us to mark some of the data as "private", given that it probably shouldn't be manipulated directly.)
By utilizing not just the "raw" Optional Content Groups, but the data from the `/Order` array when available, we can thus display the Layers in a proper tree-structure with collapsible headings for PDF documents that utilizes that feature.
Note that it's possible to reset all Optional Content Groups to their default visibility state, simply by double-clicking on the Layers-button in the sidebar.
(Currently that's indicated in the Layers-button tooltip, which is obviously easy to overlook, however it's probably the best we can do for now without adding more buttons, or even a dropdown-toolbar, to the sidebar.)
Also, the current Layers-button icons are a little rough around the edges, quite literally, but given that the viewer will soon have its UI modernized anyway they hopefully suffice in the meantime.
To give users *full* control of the visibility of the various Optional Content Groups, even those which according to the `/Order` array should not (by default) be toggleable in the UI, this patch will place those under a *custom* heading which:
- Is collapsed by default, and placed at the bottom of the Layers-tree, to be a bit less obtrusive.
- Uses a slightly different formatting, compared to the "regular" headings.
- Is localizable.
Finally, note that the thumbnails are *purposely* always rendered with all Optional Content Groups at their default visibility state, since that seems the most useful and it's also consistent with other viewers.
To ensure that this works as intended, we'll thus disable the `PDFThumbnailView.setImage` functionality when the Optional Content Groups have been changed in the viewer. (This obviously means that we'll re-render thumbnails instead of using the rendered pages. However, this situation ought to be rare enough for this to not really be a problem.)
2020-08-07 04:01:03 +09:00
|
|
|
printResolution,
|
2022-06-13 20:35:58 +09:00
|
|
|
optionalContentConfigPromise,
|
|
|
|
printAnnotationStoragePromise
|
2020-08-05 20:24:32 +09:00
|
|
|
) {
|
2019-12-27 08:22:32 +09:00
|
|
|
const scratchCanvas = activeService.scratchCanvas;
|
2017-03-28 08:07:27 +09:00
|
|
|
|
|
|
|
// The size of the canvas in pixels for printing.
|
2021-09-11 18:11:32 +09:00
|
|
|
const PRINT_UNITS = printResolution / PixelsPerInch.PDF;
|
2017-03-28 08:07:27 +09:00
|
|
|
scratchCanvas.width = Math.floor(size.width * PRINT_UNITS);
|
|
|
|
scratchCanvas.height = Math.floor(size.height * PRINT_UNITS);
|
|
|
|
|
2019-12-27 08:22:32 +09:00
|
|
|
const ctx = scratchCanvas.getContext("2d");
|
2017-03-28 08:07:27 +09:00
|
|
|
ctx.save();
|
|
|
|
ctx.fillStyle = "rgb(255, 255, 255)";
|
|
|
|
ctx.fillRect(0, 0, scratchCanvas.width, scratchCanvas.height);
|
|
|
|
ctx.restore();
|
|
|
|
|
2022-06-13 20:35:58 +09:00
|
|
|
return Promise.all([
|
|
|
|
pdfDocument.getPage(pageNumber),
|
|
|
|
printAnnotationStoragePromise,
|
|
|
|
]).then(function ([pdfPage, printAnnotationStorage]) {
|
2021-03-26 16:51:53 +09:00
|
|
|
const renderContext = {
|
|
|
|
canvasContext: ctx,
|
|
|
|
transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
|
|
|
|
viewport: pdfPage.getViewport({ scale: 1, rotation: size.rotation }),
|
|
|
|
intent: "print",
|
[api-minor] Introduce a new `annotationMode`-option, in `PDFPageProxy.{render, getOperatorList}`
*This is a follow-up to PRs 13867 and 13899.*
This patch is tagged `api-minor` for the following reasons:
- It replaces the `renderInteractiveForms`/`includeAnnotationStorage`-options, in the `PDFPageProxy.render`-method, with the single `annotationMode`-option that controls which annotations are being rendered and how. Note that the old options were mutually exclusive, and setting both to `true` would result in undefined behaviour.
- For improved consistency in the API, the `annotationMode`-option will also work together with the `PDFPageProxy.getOperatorList`-method.
- It's now also possible to disable *all* annotation rendering in both the API and the Viewer, since the other changes meant that this could now be supported with a single added line on the worker-thread[1]; fixes 7282.
---
[1] Please note that in order to simplify the overall implementation, we'll purposely only support disabling of *all* annotations and that the option is being shared between the API and the Viewer. For any more "specialized" use-cases, where e.g. only some annotation-types are being rendered and/or the API and Viewer render different sets of annotations, that'll have to be handled in third-party implementations/forks of the PDF.js code-base.
2021-08-08 21:36:28 +09:00
|
|
|
annotationMode: AnnotationMode.ENABLE_STORAGE,
|
2021-03-26 16:51:53 +09:00
|
|
|
optionalContentConfigPromise,
|
2022-06-13 20:35:58 +09:00
|
|
|
printAnnotationStorage,
|
2021-03-26 16:51:53 +09:00
|
|
|
};
|
2024-02-25 22:12:36 +09:00
|
|
|
const renderTask = pdfPage.render(renderContext);
|
|
|
|
|
|
|
|
return renderTask.promise.catch(reason => {
|
|
|
|
if (!(reason instanceof RenderingCancelledException)) {
|
|
|
|
console.error(reason);
|
|
|
|
}
|
|
|
|
throw reason;
|
|
|
|
});
|
2021-03-26 16:51:53 +09:00
|
|
|
});
|
2017-03-28 08:07:27 +09:00
|
|
|
}
|
|
|
|
|
2023-04-14 16:51:37 +09:00
|
|
|
class PDFPrintService {
|
2024-02-14 05:28:02 +09:00
|
|
|
constructor({
|
2023-04-14 16:51:37 +09:00
|
|
|
pdfDocument,
|
|
|
|
pagesOverview,
|
|
|
|
printContainer,
|
|
|
|
printResolution,
|
2024-02-14 05:28:02 +09:00
|
|
|
printAnnotationStoragePromise = null,
|
|
|
|
}) {
|
2023-04-14 16:51:37 +09:00
|
|
|
this.pdfDocument = pdfDocument;
|
|
|
|
this.pagesOverview = pagesOverview;
|
|
|
|
this.printContainer = printContainer;
|
|
|
|
this._printResolution = printResolution || 150;
|
2024-02-25 22:12:36 +09:00
|
|
|
this._optionalContentConfigPromise = pdfDocument.getOptionalContentConfig({
|
|
|
|
intent: "print",
|
|
|
|
});
|
2023-04-14 16:51:37 +09:00
|
|
|
this._printAnnotationStoragePromise =
|
|
|
|
printAnnotationStoragePromise || Promise.resolve();
|
|
|
|
this.currentPage = -1;
|
|
|
|
// The temporary canvas where renderPage paints one page at a time.
|
|
|
|
this.scratchCanvas = document.createElement("canvas");
|
|
|
|
}
|
2017-03-28 08:07:27 +09:00
|
|
|
|
2017-04-28 19:02:42 +09:00
|
|
|
layout() {
|
2017-03-28 08:07:27 +09:00
|
|
|
this.throwIfInactive();
|
|
|
|
|
2019-07-10 23:49:31 +09:00
|
|
|
const body = document.querySelector("body");
|
2017-03-28 08:07:27 +09:00
|
|
|
body.setAttribute("data-pdfjsprinting", true);
|
|
|
|
|
2023-04-14 16:42:03 +09:00
|
|
|
const { width, height } = this.pagesOverview[0];
|
|
|
|
const hasEqualPageSizes = this.pagesOverview.every(
|
|
|
|
size => size.width === width && size.height === height
|
|
|
|
);
|
2017-03-28 08:07:27 +09:00
|
|
|
if (!hasEqualPageSizes) {
|
|
|
|
console.warn(
|
2023-04-14 16:42:03 +09:00
|
|
|
"Not all pages have the same size. The printed result may be incorrect!"
|
2017-03-28 08:07:27 +09:00
|
|
|
);
|
|
|
|
}
|
2016-10-08 21:36:55 +09:00
|
|
|
|
2017-03-28 08:07:27 +09:00
|
|
|
// Insert a @page + size rule to make sure that the page size is correctly
|
|
|
|
// set. Note that we assume that all pages have the same size, because
|
|
|
|
// variable-size pages are not supported yet (e.g. in Chrome & Firefox).
|
|
|
|
// TODO(robwu): Use named pages when size calculation bugs get resolved
|
|
|
|
// (e.g. https://crbug.com/355116) AND when support for named pages is
|
|
|
|
// added (http://www.w3.org/TR/css3-page/#using-named-pages).
|
2023-04-01 07:20:38 +09:00
|
|
|
// In browsers where @page + size is not supported, the next stylesheet
|
|
|
|
// will be ignored and the user has to select the correct paper size in
|
|
|
|
// the UI if wanted.
|
2017-03-28 08:07:27 +09:00
|
|
|
this.pageStyleSheet = document.createElement("style");
|
2023-04-14 16:42:03 +09:00
|
|
|
this.pageStyleSheet.textContent = `@page { size: ${width}pt ${height}pt;}`;
|
2022-06-12 19:20:25 +09:00
|
|
|
body.append(this.pageStyleSheet);
|
2023-04-14 16:51:37 +09:00
|
|
|
}
|
2017-03-28 08:07:27 +09:00
|
|
|
|
2017-04-28 19:02:42 +09:00
|
|
|
destroy() {
|
2017-03-28 08:07:27 +09:00
|
|
|
if (activeService !== this) {
|
|
|
|
// |activeService| cannot be replaced without calling destroy() first,
|
2022-03-25 22:10:13 +09:00
|
|
|
// so if it differs then an external consumer has a stale reference to us.
|
2016-10-08 21:36:55 +09:00
|
|
|
return;
|
|
|
|
}
|
2017-03-28 08:07:27 +09:00
|
|
|
this.printContainer.textContent = "";
|
2019-07-10 23:49:31 +09:00
|
|
|
|
|
|
|
const body = document.querySelector("body");
|
|
|
|
body.removeAttribute("data-pdfjsprinting");
|
|
|
|
|
2018-02-11 00:19:45 +09:00
|
|
|
if (this.pageStyleSheet) {
|
|
|
|
this.pageStyleSheet.remove();
|
2017-03-28 08:07:27 +09:00
|
|
|
this.pageStyleSheet = null;
|
|
|
|
}
|
|
|
|
this.scratchCanvas.width = this.scratchCanvas.height = 0;
|
|
|
|
this.scratchCanvas = null;
|
|
|
|
activeService = null;
|
2020-04-14 19:28:14 +09:00
|
|
|
ensureOverlay().then(function () {
|
2022-03-25 22:10:22 +09:00
|
|
|
if (overlayManager.active === dialog) {
|
|
|
|
overlayManager.close(dialog);
|
2016-10-14 06:36:57 +09:00
|
|
|
}
|
2016-10-08 21:36:55 +09:00
|
|
|
});
|
2023-04-14 16:51:37 +09:00
|
|
|
}
|
2016-10-08 21:36:55 +09:00
|
|
|
|
2017-04-28 19:02:42 +09:00
|
|
|
renderPages() {
|
2021-05-27 23:56:31 +09:00
|
|
|
if (this.pdfDocument.isPureXfa) {
|
|
|
|
getXfaHtmlForPrinting(this.printContainer, this.pdfDocument);
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2019-12-27 08:22:32 +09:00
|
|
|
const pageCount = this.pagesOverview.length;
|
|
|
|
const renderNextPage = (resolve, reject) => {
|
2017-03-28 08:07:27 +09:00
|
|
|
this.throwIfInactive();
|
|
|
|
if (++this.currentPage >= pageCount) {
|
2023-10-19 23:30:57 +09:00
|
|
|
renderProgress(pageCount, pageCount);
|
2017-03-28 08:07:27 +09:00
|
|
|
resolve();
|
|
|
|
return;
|
2016-10-08 21:36:55 +09:00
|
|
|
}
|
2019-12-27 08:22:32 +09:00
|
|
|
const index = this.currentPage;
|
2023-10-19 23:30:57 +09:00
|
|
|
renderProgress(index, pageCount);
|
2020-08-05 20:24:32 +09:00
|
|
|
renderPage(
|
|
|
|
this,
|
|
|
|
this.pdfDocument,
|
|
|
|
/* pageNumber = */ index + 1,
|
|
|
|
this.pagesOverview[index],
|
[api-minor] Add support for toggling of Optional Content in the viewer (issue 12096)
*Besides, obviously, adding viewer support:* This patch attempts to improve the general API for Optional Content Groups slightly, by adding a couple of new methods for interacting with the (more complex) data structures of `OptionalContentConfig`-instances. (Thus allowing us to mark some of the data as "private", given that it probably shouldn't be manipulated directly.)
By utilizing not just the "raw" Optional Content Groups, but the data from the `/Order` array when available, we can thus display the Layers in a proper tree-structure with collapsible headings for PDF documents that utilizes that feature.
Note that it's possible to reset all Optional Content Groups to their default visibility state, simply by double-clicking on the Layers-button in the sidebar.
(Currently that's indicated in the Layers-button tooltip, which is obviously easy to overlook, however it's probably the best we can do for now without adding more buttons, or even a dropdown-toolbar, to the sidebar.)
Also, the current Layers-button icons are a little rough around the edges, quite literally, but given that the viewer will soon have its UI modernized anyway they hopefully suffice in the meantime.
To give users *full* control of the visibility of the various Optional Content Groups, even those which according to the `/Order` array should not (by default) be toggleable in the UI, this patch will place those under a *custom* heading which:
- Is collapsed by default, and placed at the bottom of the Layers-tree, to be a bit less obtrusive.
- Uses a slightly different formatting, compared to the "regular" headings.
- Is localizable.
Finally, note that the thumbnails are *purposely* always rendered with all Optional Content Groups at their default visibility state, since that seems the most useful and it's also consistent with other viewers.
To ensure that this works as intended, we'll thus disable the `PDFThumbnailView.setImage` functionality when the Optional Content Groups have been changed in the viewer. (This obviously means that we'll re-render thumbnails instead of using the rendered pages. However, this situation ought to be rare enough for this to not really be a problem.)
2020-08-07 04:01:03 +09:00
|
|
|
this._printResolution,
|
2022-06-13 20:35:58 +09:00
|
|
|
this._optionalContentConfigPromise,
|
|
|
|
this._printAnnotationStoragePromise
|
2020-08-05 20:24:32 +09:00
|
|
|
)
|
2021-06-03 04:37:49 +09:00
|
|
|
.then(this.useRenderedPage.bind(this))
|
|
|
|
.then(function () {
|
2017-03-28 08:07:27 +09:00
|
|
|
renderNextPage(resolve, reject);
|
|
|
|
}, reject);
|
2017-05-05 00:09:50 +09:00
|
|
|
};
|
2017-03-28 08:07:27 +09:00
|
|
|
return new Promise(renderNextPage);
|
2023-04-14 16:51:37 +09:00
|
|
|
}
|
2017-03-28 08:07:27 +09:00
|
|
|
|
2021-03-26 16:51:53 +09:00
|
|
|
useRenderedPage() {
|
2017-03-28 08:07:27 +09:00
|
|
|
this.throwIfInactive();
|
2019-12-27 08:22:32 +09:00
|
|
|
const img = document.createElement("img");
|
|
|
|
const scratchCanvas = this.scratchCanvas;
|
[api-minor] Remove support for browsers/environments without fully working `URL.createObjectURL` implementations
This `disableCreateObjectURL` option was originally introduced all the way back in PR 4103 (eight years ago), in order to work-around `URL.createObjectURL()`-bugs specific to Internet Explorer.
In PR 8081 (five years ago) the `disableCreateObjectURL` option was extended to cover Google Chrome on iOS-devices as well, since that configuration apparently also suffered from `URL.createObjectURL()`-bugs.[1]
At this point in time, I thus think that it makes sense to re-evaluate if we should still keep the `disableCreateObjectURL` option.
- For Internet Explorer, support was explicitly removed in PDF.js version `2.7.570` which was released one year ago and all IE-specific compatibility code (and polyfills) have since been removed.
- For Google Chrome on iOS-devices, while we still "support" such configurations, it's *not* the focus of any development and platform-specific bugs are thus often closed as WONTFIX.
Note here that at this point in time, the `disableCreateObjectURL` option is *only* being used in the viewer and any `URL.createObjectURL()`-bugs browser/platform bugs will thus not affect the main PDF.js library. Furthermore, given where the `disableCreateObjectURL` option is being used in the viewer the basic functionality should also remain unaffected by these changes.[2]
Furthermore, it's also possible that the `URL.createObjectURL()`-bugs have been fixed in *browser* itself since PR 8081 was submitted.[3]
Obviously you could argue that this isn't a lot of code, w.r.t. number of lines, and you'd be technically correct. However, it does add additional complexity in a few different viewer components which thus add overhead when reading and working with this code.
Finally, assuming the `URL.createObjectURL()`-bugs are still present in Google Chrome on iOS-devices, I think that we should ask ourselves if it's reasonable for the PDF.js project (and its contributors) to keep attempting to support a configuration if the *browser* developers still haven't fixed these kind of bugs!?
---
[1] According to https://groups.google.com/a/chromium.org/forum/#!topic/chromium-html5/RKQ0ZJIj7c4, which is linked in PR 8081, that bug was mentioned/reported as early as the 2014 (eight years ago).
[2] Viewer functionality such as e.g. downloading and printing may be affected.
[3] I don't have access to any iOS-devices to test with.
2022-01-02 21:18:18 +09:00
|
|
|
if ("toBlob" in scratchCanvas) {
|
2020-04-14 19:28:14 +09:00
|
|
|
scratchCanvas.toBlob(function (blob) {
|
2017-03-28 08:07:27 +09:00
|
|
|
img.src = URL.createObjectURL(blob);
|
2016-10-13 17:47:11 +09:00
|
|
|
});
|
2017-03-28 08:07:27 +09:00
|
|
|
} else {
|
|
|
|
img.src = scratchCanvas.toDataURL();
|
2016-10-08 21:36:55 +09:00
|
|
|
}
|
|
|
|
|
2019-12-27 08:22:32 +09:00
|
|
|
const wrapper = document.createElement("div");
|
2021-06-03 04:37:49 +09:00
|
|
|
wrapper.className = "printedPage";
|
2022-06-12 19:20:25 +09:00
|
|
|
wrapper.append(img);
|
|
|
|
this.printContainer.append(wrapper);
|
2016-10-08 21:36:55 +09:00
|
|
|
|
2020-04-14 19:28:14 +09:00
|
|
|
return new Promise(function (resolve, reject) {
|
2017-03-28 08:07:27 +09:00
|
|
|
img.onload = resolve;
|
|
|
|
img.onerror = reject;
|
|
|
|
});
|
2023-04-14 16:51:37 +09:00
|
|
|
}
|
2017-03-28 08:07:27 +09:00
|
|
|
|
2017-04-28 19:02:42 +09:00
|
|
|
performPrint() {
|
2017-03-28 08:07:27 +09:00
|
|
|
this.throwIfInactive();
|
2017-05-05 00:09:50 +09:00
|
|
|
return new Promise(resolve => {
|
2017-03-28 08:07:27 +09:00
|
|
|
// Push window.print in the macrotask queue to avoid being affected by
|
|
|
|
// the deprecation of running print() code in a microtask, see
|
|
|
|
// https://github.com/mozilla/pdf.js/issues/7547.
|
2017-05-05 00:09:50 +09:00
|
|
|
setTimeout(() => {
|
2017-03-28 08:07:27 +09:00
|
|
|
if (!this.active) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
print.call(window);
|
|
|
|
// Delay promise resolution in case print() was not synchronous.
|
|
|
|
setTimeout(resolve, 20); // Tidy-up.
|
2017-05-05 00:09:50 +09:00
|
|
|
}, 0);
|
|
|
|
});
|
2023-04-14 16:51:37 +09:00
|
|
|
}
|
2017-03-28 08:07:27 +09:00
|
|
|
|
|
|
|
get active() {
|
|
|
|
return this === activeService;
|
2023-04-14 16:51:37 +09:00
|
|
|
}
|
2017-03-28 08:07:27 +09:00
|
|
|
|
2017-04-28 19:02:42 +09:00
|
|
|
throwIfInactive() {
|
2017-03-28 08:07:27 +09:00
|
|
|
if (!this.active) {
|
|
|
|
throw new Error("This print request was cancelled or completed.");
|
2016-10-08 21:36:55 +09:00
|
|
|
}
|
2023-04-14 16:51:37 +09:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 21:36:55 +09:00
|
|
|
|
2019-12-27 08:22:32 +09:00
|
|
|
const print = window.print;
|
2020-04-14 19:28:14 +09:00
|
|
|
window.print = function () {
|
2017-03-28 08:07:27 +09:00
|
|
|
if (activeService) {
|
|
|
|
console.warn("Ignored window.print() because of a pending print job.");
|
|
|
|
return;
|
|
|
|
}
|
2020-04-14 19:28:14 +09:00
|
|
|
ensureOverlay().then(function () {
|
2017-03-28 08:07:27 +09:00
|
|
|
if (activeService) {
|
2022-03-25 22:10:22 +09:00
|
|
|
overlayManager.open(dialog);
|
2017-03-28 08:07:27 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
dispatchEvent("beforeprint");
|
|
|
|
} finally {
|
|
|
|
if (!activeService) {
|
|
|
|
console.error("Expected print service to be initialized.");
|
2020-04-14 19:28:14 +09:00
|
|
|
ensureOverlay().then(function () {
|
2022-03-25 22:10:22 +09:00
|
|
|
if (overlayManager.active === dialog) {
|
|
|
|
overlayManager.close(dialog);
|
2017-05-26 21:52:23 +09:00
|
|
|
}
|
|
|
|
});
|
2017-03-28 08:07:27 +09:00
|
|
|
return; // eslint-disable-line no-unsafe-finally
|
2016-10-08 21:36:55 +09:00
|
|
|
}
|
2019-12-27 08:22:32 +09:00
|
|
|
const activeServiceOnEntry = activeService;
|
2017-10-22 23:13:14 +09:00
|
|
|
activeService
|
|
|
|
.renderPages()
|
2020-04-14 19:28:14 +09:00
|
|
|
.then(function () {
|
2017-03-28 08:07:27 +09:00
|
|
|
return activeServiceOnEntry.performPrint();
|
2017-10-22 23:13:14 +09:00
|
|
|
})
|
2020-04-14 19:28:14 +09:00
|
|
|
.catch(function () {
|
2017-03-28 08:07:27 +09:00
|
|
|
// Ignore any error messages.
|
2017-10-22 23:13:14 +09:00
|
|
|
})
|
2020-04-14 19:28:14 +09:00
|
|
|
.then(function () {
|
2017-03-28 08:07:27 +09:00
|
|
|
// aborts acts on the "active" print request, so we need to check
|
|
|
|
// whether the print request (activeServiceOnEntry) is still active.
|
|
|
|
// Without the check, an unrelated print request (created after aborting
|
|
|
|
// this print request while the pages were being generated) would be
|
|
|
|
// aborted.
|
|
|
|
if (activeServiceOnEntry.active) {
|
|
|
|
abort();
|
2016-10-08 21:36:55 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-03-28 08:07:27 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
function dispatchEvent(eventType) {
|
2023-04-23 20:01:35 +09:00
|
|
|
const event = new CustomEvent(eventType, {
|
|
|
|
bubbles: false,
|
|
|
|
cancelable: false,
|
|
|
|
detail: "custom",
|
|
|
|
});
|
2017-03-28 08:07:27 +09:00
|
|
|
window.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
function abort() {
|
|
|
|
if (activeService) {
|
|
|
|
activeService.destroy();
|
|
|
|
dispatchEvent("afterprint");
|
2016-10-08 21:36:55 +09:00
|
|
|
}
|
2017-03-28 08:07:27 +09:00
|
|
|
}
|
|
|
|
|
2023-10-19 23:30:57 +09:00
|
|
|
function renderProgress(index, total) {
|
2023-03-16 22:22:14 +09:00
|
|
|
if (typeof PDFJSDev === "undefined" && window.isGECKOVIEW) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-25 22:10:22 +09:00
|
|
|
dialog ||= document.getElementById("printServiceDialog");
|
2019-12-27 08:22:32 +09:00
|
|
|
const progress = Math.round((100 * index) / total);
|
2022-03-25 22:10:22 +09:00
|
|
|
const progressBar = dialog.querySelector("progress");
|
|
|
|
const progressPerc = dialog.querySelector(".relative-progress");
|
2017-03-28 08:07:27 +09:00
|
|
|
progressBar.value = progress;
|
2023-10-19 23:30:57 +09:00
|
|
|
progressPerc.setAttribute("data-l10n-args", JSON.stringify({ progress }));
|
2017-03-28 08:07:27 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
"keydown",
|
2020-04-14 19:28:14 +09:00
|
|
|
function (event) {
|
2017-03-28 08:07:27 +09:00
|
|
|
// Intercept Cmd/Ctrl + P in all browsers.
|
|
|
|
// Also intercept Cmd/Ctrl + Shift + P in Chrome and Opera
|
|
|
|
if (
|
|
|
|
event.keyCode === /* P= */ 80 &&
|
|
|
|
(event.ctrlKey || event.metaKey) &&
|
|
|
|
!event.altKey &&
|
|
|
|
(!event.shiftKey || window.chrome || window.opera)
|
|
|
|
) {
|
|
|
|
window.print();
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
|
2017-03-28 08:07:27 +09:00
|
|
|
event.preventDefault();
|
2022-09-24 22:02:12 +09:00
|
|
|
event.stopImmediatePropagation();
|
2016-10-08 21:36:55 +09:00
|
|
|
}
|
2017-03-28 08:07:27 +09:00
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
if ("onbeforeprint" in window) {
|
|
|
|
// Do not propagate before/afterprint events when they are not triggered
|
2020-09-06 00:00:52 +09:00
|
|
|
// from within this polyfill. (FF / Chrome 63+).
|
2020-04-14 19:28:14 +09:00
|
|
|
const stopPropagationIfNeeded = function (event) {
|
2022-09-24 22:02:12 +09:00
|
|
|
if (event.detail !== "custom") {
|
2017-03-28 08:07:27 +09:00
|
|
|
event.stopImmediatePropagation();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
window.addEventListener("beforeprint", stopPropagationIfNeeded);
|
|
|
|
window.addEventListener("afterprint", stopPropagationIfNeeded);
|
|
|
|
}
|
|
|
|
|
2017-10-22 23:13:14 +09:00
|
|
|
let overlayPromise;
|
2017-03-28 08:07:27 +09:00
|
|
|
function ensureOverlay() {
|
2023-03-16 22:22:14 +09:00
|
|
|
if (typeof PDFJSDev === "undefined" && window.isGECKOVIEW) {
|
|
|
|
return Promise.reject(
|
|
|
|
new Error("ensureOverlay not implemented in GECKOVIEW development mode.")
|
|
|
|
);
|
|
|
|
}
|
2017-03-28 08:07:27 +09:00
|
|
|
if (!overlayPromise) {
|
2024-02-09 20:01:59 +09:00
|
|
|
overlayManager = viewerApp.overlayManager;
|
2017-05-26 21:52:23 +09:00
|
|
|
if (!overlayManager) {
|
|
|
|
throw new Error("The overlay manager has not yet been initialized.");
|
|
|
|
}
|
2022-03-25 22:10:22 +09:00
|
|
|
dialog ||= document.getElementById("printServiceDialog");
|
2017-05-26 21:52:23 +09:00
|
|
|
|
|
|
|
overlayPromise = overlayManager.register(
|
2022-03-25 22:10:13 +09:00
|
|
|
dialog,
|
|
|
|
/* canForceClose = */ true
|
2017-03-28 08:07:27 +09:00
|
|
|
);
|
2022-03-25 22:10:13 +09:00
|
|
|
|
2017-03-28 08:07:27 +09:00
|
|
|
document.getElementById("printCancel").onclick = abort;
|
2022-03-25 22:10:13 +09:00
|
|
|
dialog.addEventListener("close", abort);
|
2017-03-28 08:07:27 +09:00
|
|
|
}
|
|
|
|
return overlayPromise;
|
|
|
|
}
|
2016-10-08 21:36:55 +09:00
|
|
|
|
2024-02-07 21:53:29 +09:00
|
|
|
/**
|
|
|
|
* @implements {IPDFPrintServiceFactory}
|
|
|
|
*/
|
|
|
|
class PDFPrintServiceFactory {
|
2024-02-09 20:01:59 +09:00
|
|
|
static initGlobals(app) {
|
|
|
|
viewerApp = app;
|
|
|
|
}
|
|
|
|
|
2024-02-07 21:53:29 +09:00
|
|
|
static get supportsPrinting() {
|
|
|
|
return shadow(this, "supportsPrinting", true);
|
|
|
|
}
|
2016-10-08 21:36:55 +09:00
|
|
|
|
2024-02-14 05:28:02 +09:00
|
|
|
static createPrintService(params) {
|
2017-03-28 08:07:27 +09:00
|
|
|
if (activeService) {
|
|
|
|
throw new Error("The print service is created and active.");
|
2016-10-08 21:36:55 +09:00
|
|
|
}
|
2024-02-14 05:28:02 +09:00
|
|
|
return (activeService = new PDFPrintService(params));
|
2024-02-07 21:53:29 +09:00
|
|
|
}
|
|
|
|
}
|
2016-10-08 21:36:55 +09:00
|
|
|
|
2024-02-07 21:53:29 +09:00
|
|
|
export { PDFPrintServiceFactory };
|