forked from mozilla/pdf.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This shim does the following: 1. Intercept window.print() 2. For a window.print() call (if allowed, ie. no previous print job): 1. Dispatch the beforeprint event. 2. Render a printg progress dialog. 3. For each canvas, call mozPrintCallback if existent (one at a time, async). 4. During each mozPrintCallback callback, update the progress dialog. 5. When all <canvas>es have been rendered, invoke the real window.print(). 6. Dispatch the afterprint event. The shim is not included in Firefox through the preprocessor. Keyboard shortcuts (Ctrl/Cmd + P) are intercepted and default behavior (i.e. printing) is prevented, and the steps for window.print() are run. window.attachEvent is used, in order to cancel printing in IE10 and earlier (courtesy of Stack Overflow - http://stackoverflow.com/a/15302847). Unfortunately, this doesn't work in IE11 - if Ctrl + P is used, the print dialog will be shown twice: Once because of Ctrl + P, and again when all pages have finished rendering. This logic of this polyfill is not specific to PDF.js, so it can also be used in other projects. There's one additional modification in PDF.js's viewer.js: The printed <canvas> element is wrapped in a <div>. This is needed, because Chrome would otherwise print one canvas on two pages.
- Loading branch information
Showing
5 changed files
with
225 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ | ||
/* Copyright 2013 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. | ||
*/ | ||
/* globals HTMLCanvasElement */ | ||
|
||
'use strict'; | ||
(function mozPrintCallbackPolyfillClosure() { | ||
if ('mozPrintCallback' in document.createElement('canvas')) { | ||
return; | ||
} | ||
// Cause positive result on feature-detection: | ||
HTMLCanvasElement.prototype.mozPrintCallback = undefined; | ||
|
||
var canvases; // During print task: non-live NodeList of <canvas> elements | ||
var index; // Index of <canvas> element that is being processed | ||
|
||
var print = window.print; | ||
window.print = function print() { | ||
if (canvases) { | ||
console.warn('Ignored window.print() because of a pending print job.'); | ||
return; | ||
} | ||
try { | ||
dispatchEvent('beforeprint'); | ||
} finally { | ||
canvases = document.querySelectorAll('canvas'); | ||
index = -1; | ||
next(); | ||
} | ||
}; | ||
|
||
function dispatchEvent(eventType) { | ||
var event = document.createEvent('CustomEvent'); | ||
event.initCustomEvent(eventType, false, false, 'custom'); | ||
window.dispatchEvent(event); | ||
} | ||
|
||
function next() { | ||
if (!canvases) { | ||
return; // Print task cancelled by user (state reset in abort()) | ||
} | ||
|
||
renderProgress(); | ||
if (++index < canvases.length) { | ||
var canvas = canvases[index]; | ||
if (typeof canvas.mozPrintCallback === 'function') { | ||
canvas.mozPrintCallback({ | ||
context: canvas.getContext('2d'), | ||
abort: abort, | ||
done: next | ||
}); | ||
} else { | ||
next(); | ||
} | ||
} else { | ||
renderProgress(); | ||
print.call(window); | ||
setTimeout(abort, 20); // Tidy-up | ||
} | ||
} | ||
|
||
function abort() { | ||
if (canvases) { | ||
canvases = null; | ||
renderProgress(); | ||
dispatchEvent('afterprint'); | ||
} | ||
} | ||
|
||
function renderProgress() { | ||
var progressContainer = document.getElementById('mozPrintCallback-shim'); | ||
if (canvases) { | ||
var progress = Math.round(100 * index / canvases.length); | ||
var progressBar = progressContainer.querySelector('progress'); | ||
var progressPerc = progressContainer.querySelector('.relative-progress'); | ||
progressBar.value = progress; | ||
progressPerc.textContent = progress + '%'; | ||
progressContainer.removeAttribute('hidden'); | ||
progressContainer.onclick = abort; | ||
} else { | ||
progressContainer.setAttribute('hidden', ''); | ||
} | ||
} | ||
|
||
var hasAttachEvent = !!document.attachEvent; | ||
|
||
window.addEventListener('keydown', function(event) { | ||
if (event.keyCode === 80/*P*/ && (event.ctrlKey || event.metaKey)) { | ||
window.print(); | ||
if (hasAttachEvent) { | ||
// Only attachEvent can cancel Ctrl + P dialog in IE <=10 | ||
// attachEvent is gone in IE11, so the dialog will re-appear in IE11. | ||
return; | ||
} | ||
event.preventDefault(); | ||
if (event.stopImmediatePropagation) { | ||
event.stopImmediatePropagation(); | ||
} else { | ||
event.stopPropagation(); | ||
} | ||
return; | ||
} | ||
if (event.keyCode === 27 && canvases) { // Esc | ||
abort(); | ||
} | ||
}, true); | ||
if (hasAttachEvent) { | ||
document.attachEvent('onkeydown', function(event) { | ||
event = event || window.event; | ||
if (event.keyCode === 80/*P*/ && event.ctrlKey) { | ||
event.keyCode = 0; | ||
return false; | ||
} | ||
}); | ||
} | ||
|
||
if ('onbeforeprint' in window) { | ||
// Do not propagate before/afterprint events when they are not triggered | ||
// from within this polyfill. (FF/IE). | ||
var stopPropagationIfNeeded = function(event) { | ||
if (event.detail !== 'custom' && event.stopImmediatePropagation) { | ||
event.stopImmediatePropagation(); | ||
} | ||
}; | ||
window.addEventListener('beforeprint', stopPropagationIfNeeded, false); | ||
window.addEventListener('afterprint', stopPropagationIfNeeded, false); | ||
} | ||
})(); |
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,71 @@ | ||
<div id="mozPrintCallback-shim" hidden> | ||
<style scoped> | ||
#mozPrintCallback-shim { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
height: 100%; | ||
width: 100%; | ||
z-index: 9999999; | ||
|
||
display: block; | ||
text-align: center; | ||
background-color: rgba(0, 0, 0, 0.5); | ||
} | ||
#mozPrintCallback-shim[hidden] { | ||
display: none; | ||
} | ||
@media print { | ||
#mozPrintCallback-shim { | ||
display: none; | ||
} | ||
} | ||
|
||
#mozPrintCallback-shim .mozPrintCallback-dialog-box { | ||
display: inline-block; | ||
margin: -50px auto 0; | ||
position: relative; | ||
top: 45%; | ||
left: 0; | ||
min-width: 220px; | ||
max-width: 400px; | ||
|
||
padding: 9px; | ||
|
||
border: 1px solid hsla(0, 0%, 0%, .5); | ||
border-radius: 2px; | ||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3); | ||
|
||
background-color: #474747; | ||
|
||
color: hsl(0, 0%, 85%); | ||
font-size: 16px; | ||
line-height: 20px; | ||
} | ||
#mozPrintCallback-shim .progress-row { | ||
clear: both; | ||
padding: 1em 0; | ||
} | ||
#mozPrintCallback-shim progress { | ||
width: 100%; | ||
} | ||
#mozPrintCallback-shim .relative-progress { | ||
clear: both; | ||
float: right; | ||
} | ||
#mozPrintCallback-shim .progress-actions { | ||
clear: both; | ||
} | ||
</style> | ||
<div class="mozPrintCallback-dialog-box"> | ||
<!-- TODO: Localise the following strings --> | ||
Preparing document for printing... | ||
<div class="progress-row"> | ||
<progress value="0" max="100"></progress> | ||
<span class="relative-progress">0%</span> | ||
</div> | ||
<div class="progress-actions"> | ||
<input type="button" value="Cancel" class="mozPrintCallback-cancel"> | ||
</div> | ||
</div> | ||
</div> |
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