diff --git a/api-samples/printing/README.md b/api-samples/printing/README.md index 2ba7e74fdd..1f477b8163 100644 --- a/api-samples/printing/README.md +++ b/api-samples/printing/README.md @@ -8,6 +8,8 @@ The `chrome.printing` namespace only works on ChromeOS. The sample demonstrates Calling `submitJob()` triggers a dialog box asking the user to confirm printing. Use the [`PrintingAPIExtensionsAllowlist`](https://chromeenterprise.google/policies/#PrintingAPIExtensionsAllowlist") policy to bypass confirmation. +If the `'Roll Printers` checkbox is selected, only printers capable of roll printing will appear in the table. In this case, a separate test file is printed and the height of the media can be variable. + ## Implementation Notes Before Chrome 120, `submitJob()` function throws an error when returning a promise. diff --git a/api-samples/printing/printers.html b/api-samples/printing/printers.html index 261f6d1d32..46408898ec 100644 --- a/api-samples/printing/printers.html +++ b/api-samples/printing/printers.html @@ -29,6 +29,8 @@
Default | Recently used | Capabilities | +Supports trim | Status |
---|
On some systems, the print job may leave the queue too quickly to be visible in this list.
-Cancel | -Job Id | -Status | -
---|
Name | -Description | -Size | -Supports trim | -Status | -
---|
diff --git a/api-samples/printing/printers.js b/api-samples/printing/printers.js index 9e68aaea1c..2f4640b138 100644 --- a/api-samples/printing/printers.js +++ b/api-samples/printing/printers.js @@ -2,7 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -function onPrintButtonClicked(printerId, dpi) { +let listOfPrinters = []; + +function rollPrintingEnabled() { + return document.getElementById('rollPrinters').checked; +} + +function onPrintButtonClicked(printerId, dpi, performTrim) { let ticket = { version: '1.0', print: { @@ -14,16 +20,34 @@ function onPrintButtonClicked(printerId, dpi) { horizontal_dpi: dpi.horizontal_dpi, vertical_dpi: dpi.vertical_dpi }, - media_size: { - width_microns: 210000, - height_microns: 297000, - vendor_id: 'iso_a4_210x297mm' - }, collate: { collate: false } } }; - fetch('test.pdf') + if (rollPrintingEnabled()) { + filename = 'test-rollprinting.pdf'; + ticket.print.media_size = { + width_microns: 72320, + // Note that this value needs to be between min_height_microns and + // max_height_microns. Usually this matches the height of the document + // being printed. + height_microns: 110000 + }; + // This only makese sense to specify if the printer supports the trim + // option. + if (performTrim) { + ticket.print.vendor_ticket_item = [{id: 'finishings', value: 'trim'}]; + } + } else { + filename = 'test.pdf'; + ticket.print.media_size = { + width_microns: 210000, + height_microns: 297000, + vendor_id: 'iso_a4_210x297mm' + }; + } + + fetch(filename) .then((response) => response.arrayBuffer()) .then((arrayBuffer) => { const request = { @@ -73,48 +97,67 @@ function addCell(parent) { return newCell; } +function supportsRollPrinting(printerInfo) { + // If any of the media size optionis support continuous feed, return true. + const newOptions = printerInfo.capabilities.printer.media_size.option.filter( + (option) => option.is_continuous_feed); + if (newOptions.length > 0) { + return true; + } + return false; +} + function createPrintersTable() { - chrome.printing.getPrinters().then((printers) => { - const tbody = document.createElement('tbody'); - printers.forEach((printer) => { - chrome.printing.getPrinterInfo(printer.id).then((printerInfo) => { - const columnValues = [ - printer.id, - printer.name, - printer.description, - printer.uri, - printer.source, - printer.isDefault, - printer.recentlyUsedRank, - JSON.stringify(printerInfo.capabilities), - printerInfo.status - ]; - - let tr = document.createElement('tr'); - const printTd = document.createElement('td'); - printTd.appendChild( + // Reset this so the table can be rebuilt with either all printers or just + // printers capable of roll printing. + let tbody = document.getElementById('tbody'); + while (tbody.firstChild) { + tbody.removeChild(tbody.firstChild); + } + + listOfPrinters.forEach((printer) => { + if (!rollPrintingEnabled() || supportsRollPrinting(printer.info)) { + // The printer needs to support this specific vendor capability if the + // print job ticket is going to specify the trim option. + const supportsTrim = + printer.info.capabilities.printer.vendor_capability.some( + (capability) => capability.display_name == "finishings/11"); + const columnValues = [ + printer.data.id, + printer.data.name, + printer.data.description, + printer.data.uri, + printer.data.source, + printer.data.isDefault, + printer.data.recentlyUsedRank, + JSON.stringify(printer.info.capabilities), + supportsTrim, + printer.info.status + ]; + + let tr = document.createElement('tr'); + const printTd = document.createElement('td'); + printTd.appendChild( createButton('Print', () => { onPrintButtonClicked( - printer.id, - printerInfo.capabilities.printer.dpi.option[0] + printer.data.id, + printer.info.capabilities.printer.dpi.option[0], + supportsTrim ); }) - ); + ); - tr.appendChild(printTd); + tr.appendChild(printTd); - for (const columnValue of columnValues) { - const td = document.createElement('td'); - td.appendChild(document.createTextNode(columnValue)); - td.setAttribute('align', 'center'); - tr.appendChild(td); - } + for (const columnValue of columnValues) { + const td = document.createElement('td'); + td.appendChild(document.createTextNode(columnValue)); + td.setAttribute('align', 'center'); + tr.appendChild(td); + } - tbody.appendChild(tr); - }); - }); - const table = document.getElementById('printersTable'); - table.appendChild(tbody); + tbody.appendChild(tr); + } }); chrome.printing.onJobStatusChanged.addListener((jobId, status) => { @@ -149,5 +192,17 @@ function createPrintersTable() { } document.addEventListener('DOMContentLoaded', () => { - createPrintersTable(); + chrome.printing.getPrinters().then((printers) => { + printers.forEach((printer) => { + chrome.printing.getPrinterInfo(printer.id).then((printerInfo) => { + listOfPrinters.push({data: printer, info: printerInfo}); + createPrintersTable(); + }); + }); + }); + + let checkbox = document.getElementById('rollPrinters') + checkbox.addEventListener('change', function() { + createPrintersTable(); + }); }); diff --git a/api-samples/printing/roll-printing/README.md b/api-samples/printing/roll-printing/README.md deleted file mode 100644 index 64154da910..0000000000 --- a/api-samples/printing/roll-printing/README.md +++ /dev/null @@ -1,13 +0,0 @@ -# chrome.printing - -This sample demonstrates printing to a roll printer with the `chrome.printing` namespace. - -## Overview - -The `chrome.printing` namespace only works on ChromeOS. The sample demonstrates how to get a list of available printers and display the ones that support roll printing. A new entry is added to the table for each unique width for each printer. A 'Print' button sends a sample PDF to the selected printer and makes a 'Cancel Printing' button visible. This button is visible while the print job's status is `"PENDING"` or `"IN_PROGRESS"`. Note that on some systems, the print job is passed to the printer so quickly that you may never see the 'Cancel Printing' button. - -Calling `submitJob()` triggers a dialog box asking the user to confirm printing. Use the [`PrintingAPIExtensionsAllowlist`](https://chromeenterprise.google/policies/#PrintingAPIExtensionsAllowlist") policy to bypass confirmation. - -## Implementation Notes - -Before Chrome 120, `submitJob()` function throws an error when returning a promise. diff --git a/api-samples/printing/roll-printing/icons/icon.png b/api-samples/printing/roll-printing/icons/icon.png deleted file mode 100644 index 19057a5f32..0000000000 Binary files a/api-samples/printing/roll-printing/icons/icon.png and /dev/null differ diff --git a/api-samples/printing/roll-printing/icons/icon128.png b/api-samples/printing/roll-printing/icons/icon128.png deleted file mode 100644 index 13a19d6d81..0000000000 Binary files a/api-samples/printing/roll-printing/icons/icon128.png and /dev/null differ diff --git a/api-samples/printing/roll-printing/icons/icon16.png b/api-samples/printing/roll-printing/icons/icon16.png deleted file mode 100644 index 7591630c86..0000000000 Binary files a/api-samples/printing/roll-printing/icons/icon16.png and /dev/null differ diff --git a/api-samples/printing/roll-printing/icons/icon48.png b/api-samples/printing/roll-printing/icons/icon48.png deleted file mode 100644 index f51b2ff243..0000000000 Binary files a/api-samples/printing/roll-printing/icons/icon48.png and /dev/null differ diff --git a/api-samples/printing/roll-printing/manifest.json b/api-samples/printing/roll-printing/manifest.json deleted file mode 100644 index cc6cac612b..0000000000 --- a/api-samples/printing/roll-printing/manifest.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "name": "Roll-printing Extension", - "version": "1.0", - "description": "Demonstrates roll-printing using the chrome.printing namespace.", - "permissions": ["printing"], - "action": { - "default_popup": "printers.html", - "default_icon": { - "16": "icons/icon16.png", - "48": "icons/icon48.png", - "128": "icons/icon128.png" - } - }, - "icons": { - "16": "icons/icon16.png", - "48": "icons/icon48.png", - "128": "icons/icon128.png" - }, - "manifest_version": 3 -} diff --git a/api-samples/printing/roll-printing/printers.css b/api-samples/printing/roll-printing/printers.css deleted file mode 100644 index e97ffda6c4..0000000000 --- a/api-samples/printing/roll-printing/printers.css +++ /dev/null @@ -1,18 +0,0 @@ -/** - * Copyright 2020 The Chromium Authors. All rights reserved. - * Use of this source code is governed by a BSD-style license that can be - * found in the LICENSE file. - */ - -table, th, td, tr { - border: 1px solid black; -} - -table { - width: 100%; - border-collapse: collapse; -} - -div { - overflow: auto; -} diff --git a/api-samples/printing/roll-printing/printers.html b/api-samples/printing/roll-printing/printers.html deleted file mode 100644 index 6bcfa1ca8e..0000000000 --- a/api-samples/printing/roll-printing/printers.html +++ /dev/null @@ -1,46 +0,0 @@ - - - -
-
- - - - -
-