Gecko:Printing API: Difference between revisions
Jump to navigation
Jump to search
Tschneider (talk | contribs) (known bugs) |
m (Jonathan Watt moved page Printing to Gecko:Printing API: This document is far to niche to be the top level "Printing" page) |
||
| (6 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
;Editors | |||
:[[User:Tschneider|Tobias Schneider]] | |||
== Scope == | == Scope == | ||
| Line 5: | Line 8: | ||
== Example == | == Example == | ||
window.startPrintJob(function (state) { | // Gets fired after user clicked File->Print. | ||
window.onprint = function (e) { | |||
customPrint(); | |||
// Event can and should be canceled. | |||
e.preventDefault(); | |||
}; | |||
function customPrint() { | |||
// By calling startPrintJob, the Browser opens the system printing dialog. | |||
// After the user clicks the print button, the print page handler gets | |||
// called for every page in the user defined range and returns a Promise | |||
// that resolves to a HTML element to be printed on that page. | |||
// | |||
// A final Promise is returned by startPrintJob which resolves after | |||
// successfully printing all pages, or rejects if one of the page promises | |||
// was rejected, an error occured during rendering or if the printing was | |||
// canceled by the user. | |||
window.startPrintJob(function (state) { | |||
var canvas = document.createElement("canvas"); | |||
var ctx = canvas.getContext("2d"); | |||
if (!state.isPreview) { | |||
// We can draw at higher resolution for actual printing output. | |||
canvas.width *= 5; | |||
canvas.height *= 5; | |||
ctx.scale(5, 5); | |||
} | |||
switch (state.currentPage) { | |||
case 1: | |||
ctx.drawText(0, 0, state.title); | |||
break; | |||
case 2: | |||
case 3: | |||
ctx.drawText(0, 0, "Page " + state.currentPage + " of " + state.totalPages); | |||
break; | |||
default: | |||
return Promise.reject(Error("Invalid page number")); | |||
} | |||
return Promise.resolve(canvas); | |||
}, /* Total number of Pages */ 3, /* Document title */ "Printing Example") | |||
.then(function () { | |||
console.log('Printing was successful'); | |||
}, function () { | |||
console.error('Printing failed'); | |||
}); | |||
} | |||
== IDL | == IDL additions == | ||
interface WindowEventHandlers { | |||
attribute EventHandler onprint; | |||
... | |||
}; | |||
interface PrintPageHandler { | interface PrintPageHandler { | ||
Promise getPageElement(PrintState state); | Promise getPageElement(PrintState state); | ||
} | }; | ||
interface PrintState { | interface PrintState { | ||
| Line 42: | Line 69: | ||
readonly attribute DOMString title; | readonly attribute DOMString title; | ||
readonly attribute double currentPage; | readonly attribute double currentPage; | ||
readonly boolean isPreview | readonly attribute boolean isPreview; | ||
} | }; | ||
dictionary PrintOptions { | dictionary PrintOptions { | ||
double totalPages = 1; | double totalPages = 1; | ||
DOMString title = | DOMString title = null; | ||
... | ... | ||
}; | }; | ||
interface | interface WindowPrinting { | ||
Promise startPrintJob(PrintPageHandler handler, | |||
optional (unrestricted double or PrintOptions) options); | |||
}; | }; | ||
Window implements | Window implements WindowPrinting; | ||
== Bugs == | == Bugs == | ||
| Line 62: | Line 89: | ||
* https://bugzilla.mozilla.org/show_bug.cgi?id=1209273 | * https://bugzilla.mozilla.org/show_bug.cgi?id=1209273 | ||
* https://bugzilla.mozilla.org/show_bug.cgi?id=132035 | * https://bugzilla.mozilla.org/show_bug.cgi?id=132035 | ||
== See Also == | |||
[http://www.w3.org/TR/css3-page/ CSS Paged Media Module Level 3] | |||
Latest revision as of 21:53, 15 December 2017
- Editors
- Tobias Schneider
Scope
Purpose of this document is to define a set of standards and API's to give web developers more control over printing output.
Example
// Gets fired after user clicked File->Print.
window.onprint = function (e) {
customPrint();
// Event can and should be canceled.
e.preventDefault();
};
function customPrint() {
// By calling startPrintJob, the Browser opens the system printing dialog.
// After the user clicks the print button, the print page handler gets
// called for every page in the user defined range and returns a Promise
// that resolves to a HTML element to be printed on that page.
//
// A final Promise is returned by startPrintJob which resolves after
// successfully printing all pages, or rejects if one of the page promises
// was rejected, an error occured during rendering or if the printing was
// canceled by the user.
window.startPrintJob(function (state) {
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
if (!state.isPreview) {
// We can draw at higher resolution for actual printing output.
canvas.width *= 5;
canvas.height *= 5;
ctx.scale(5, 5);
}
switch (state.currentPage) {
case 1:
ctx.drawText(0, 0, state.title);
break;
case 2:
case 3:
ctx.drawText(0, 0, "Page " + state.currentPage + " of " + state.totalPages);
break;
default:
return Promise.reject(Error("Invalid page number"));
}
return Promise.resolve(canvas);
}, /* Total number of Pages */ 3, /* Document title */ "Printing Example")
.then(function () {
console.log('Printing was successful');
}, function () {
console.error('Printing failed');
});
}
IDL additions
interface WindowEventHandlers {
attribute EventHandler onprint;
...
};
interface PrintPageHandler {
Promise getPageElement(PrintState state);
};
interface PrintState {
readonly attribute double totalPages;
readonly attribute DOMString title;
readonly attribute double currentPage;
readonly attribute boolean isPreview;
};
dictionary PrintOptions {
double totalPages = 1;
DOMString title = null;
...
};
interface WindowPrinting {
Promise startPrintJob(PrintPageHandler handler,
optional (unrestricted double or PrintOptions) options);
};
Window implements WindowPrinting;
Bugs
- https://bugzilla.mozilla.org/show_bug.cgi?id=1209273
- https://bugzilla.mozilla.org/show_bug.cgi?id=132035