WebAPI/WebPrintAPI
< WebAPI
Jump to navigation
Jump to search
First iteration: WebPrintAPI
Scope
This is about implementing a new API for printing on the web. It's a "low level" API that allows web developers to have rich control about printing output.
Proposed API
- Use points as metric for printing (72 points per inch)
- Open Questions
- Should the developer set the page size or should the page size be set by choosing a printer and then using the printer's paper size setup?
- What is (0,0)? The top left corner of the physical page OR of the page taking into account the print margin?
- Should there be different color spaces then RGB? How to make them work, as there are no CSS colors defined for CMYK.
- Lot more... TBD
var pDoc = new PrintDocument( 'documentTitle', // Title 'letter', // Paper size false // optional: isLandscape? ); // --- // Following things MUST be defined/set by the developer. // --- // The number of pages to print. pDoc.calcPageCount = function(fontCtx) { var pageCount; // .. // The fontCtx is only a very limited subset of the normal print ctx, that // basically allows to do font-size measurements. fontCtx.font = '...'; fontCtx.measureText(...); //... return pageCount; // 42 }; // Actually drawing function. Each page is painted individually. This is // handy for page preview or if the user picks only a subset of the total // pages. pDoc.renderPage = function(pageNumber, ctx) { if (ctx.isPreview) { var dpi = ctx.DPI; // Only draw images in low resolution for preview // ... } // Render the page <pageNumber> to the ctx. ctx.draw... // ... // If the drawing takes very long, the developer might want to use // setTimeouts and continue the painting later one. if (printingTakesLong()) { setTimeout(function() { // Continue drawing... // This page is done drawing. ctx.endPage(); }) } else { ctx.endPage(); } } // --- // Other APIs // --- pDoc.getPageSize = function() { return { width: <widthInInch>, height: <heightInInch>, } } pDoc.isPrinting; // Start the printing progress. This opens the print dialog. pDoc.print(); pDoc.abort();
Implementation
None.