Gecko:Printing API: Difference between revisions

Jump to navigation Jump to search
extend example
(IDL updates)
(extend example)
Line 8: Line 8:
== Example ==
== Example ==


  window.startPrintJob(function (state) {
// Gets fired after user clicked File->Print.
var canvas = document.createElement("canvas");
  window.onprint = function (e) {
var ctx = canvas.getContext("2d");
customPrint();
if (!state.isPreview) {
// Event can and should be canceled.
canvas.width *= 5;
e.preventDefault();
canvas.height *= 5;
};
ctx.scale(5, 5);
}
function customPrint() {
switch (state.currentPage) {
// By calling startPrintJob, the Browser opens the system printing dialog.
case 1:
// After the user clicks the print button, the print page handler gets
ctx.drawText(0, 0, state.title);
// called for every page in the user defined range and returns a Promise
break;
// that resolves to a HTML element to be printed on that page.
case 2:
//
case 3:
// A final Promise is returned by startPrintJob which resolves after
ctx.drawText(0, 0, "Page " + state.currentPage + " of " + state.totalPages);
// successfully printing all pages, or rejects if one of the page promises
break;
// was rejected, an error occured during rendering or if the printing was
default:
// canceled by the user.
return Promise.reject(Error("Invalid page number"));
window.startPrintJob(function (state) {
}
var canvas = document.createElement("canvas");
return Promise.resolve(canvas);
var ctx = canvas.getContext("2d");
}, 3, "Printing Example")
if (!state.isPreview) {
.then(function () {
// We can draw at higher resolution for actual printing output.
console.log('Printing done');
canvas.width *= 5;
}, function () {
canvas.height *= 5;
console.error('Printing failed');
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 ==
== IDL additions ==
31

edits

Navigation menu