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 @@

Print Job:

Printers:

+ + @@ -41,9 +43,11 @@

Printers:

+ +
Default Recently used CapabilitiesSupports 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 @@ - - - - - Printers - - - - - -

Print Job:

-

On some systems, the print job may leave the queue too quickly to be visible in this list.

-
- - - - - - - - - -
CancelJob IdStatus
-
- -

Printers:

-
- - - - - - - - - - - -
PrintNameDescriptionSizeSupports trimStatus
-
- - diff --git a/api-samples/printing/roll-printing/printers.js b/api-samples/printing/roll-printing/printers.js deleted file mode 100644 index 82769e498d..0000000000 --- a/api-samples/printing/roll-printing/printers.js +++ /dev/null @@ -1,179 +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. - -function onPrintButtonClicked(printerId, dpi, width, performTrim) { - let ticket = { - version: '1.0', - print: { - color: { type: 'STANDARD_MONOCHROME' }, - duplex: { type: 'NO_DUPLEX' }, - page_orientation: { type: 'LANDSCAPE' }, - copies: { copies: 1 }, - dpi: { - horizontal_dpi: dpi.horizontal_dpi, - vertical_dpi: dpi.vertical_dpi - }, - media_size: { - width_microns: width, - // 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 - }, - collate: { collate: false } - } - }; - // This only makese sense to specify if the printer supports the trim option. - if (performTrim) { - ticket.print.vendor_ticket_item = [{id: 'finishings', value: 'trim'}]; - } - - fetch('test.pdf') - .then((response) => response.arrayBuffer()) - .then((arrayBuffer) => { - const request = { - job: { - printerId: printerId, - title: 'test job', - ticket: ticket, - contentType: 'application/pdf', - document: new Blob([new Uint8Array(arrayBuffer)], { - type: 'application/pdf' - }) - } - }; - chrome.printing.submitJob(request).then((response) => { - if (response !== undefined) { - console.log(response.status); - } - if (chrome.runtime.lastError !== undefined) { - console.log(chrome.runtime.lastError.message); - } - window.scrollTo(0, document.body.scrollHeight); - }); - }); -} - -function onCancelButtonClicked(jobId) { - chrome.printing.cancelJob(jobId).then((response) => { - if (response !== undefined) { - console.log(response.status); - } - if (chrome.runtime.lastError !== undefined) { - console.log(chrome.runtime.lastError.message); - } - }); -} - -function createButton(label, onClicked) { - const button = document.createElement('button'); - button.innerHTML = label; - button.onclick = onClicked; - return button; -} - -function addCell(parent) { - const newCell = document.createElement('td'); - parent.appendChild(newCell); - return newCell; -} - -function supportsRollPrinting(printerInfo) { - // Only keep the media size options that support continuous feed. When - // creating a print job ticket with a variable height, one of these widths - // must be used. These options don't really need to be filtered - just doing - // this for display purposes and to demonstrate how to determine which - // printers support continuous feed. - const newOptions = printerInfo.capabilities.printer.media_size.option.filter( - (option) => option.is_continuous_feed); - if (newOptions.length > 0) { - printerInfo.capabilities.printer.media_size.option = newOptions; - 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) => { - if (supportsRollPrinting(printerInfo)) { - // The printer needs to support this specific vendor capability if the - // print job ticket is going to specify the trim option. - const supportsTrim = - printerInfo.capabilities.printer.vendor_capability.some( - (capability) => capability.display_name == "finishings/11"); - printerInfo.capabilities.printer.media_size.option.forEach((option) => { - const columnValues = [ - printer.name, - printer.description, - JSON.stringify(option), - supportsTrim, - printerInfo.status - ]; - - let tr = document.createElement('tr'); - const printTd = document.createElement('td'); - printTd.appendChild( - createButton('Print', () => { - onPrintButtonClicked( - printer.id, - printerInfo.capabilities.printer.dpi.option[0], - option.width_microns, - supportsTrim - ); - }) - ); - - 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); - } - - tbody.appendChild(tr); - })}}); - }); - const table = document.getElementById('printersTable'); - table.appendChild(tbody); - }); - - chrome.printing.onJobStatusChanged.addListener((jobId, status) => { - console.log(`jobId: ${jobId}, status: ${status}`); - let jobTr = document.getElementById(jobId); - if (jobTr == undefined) { - jobTr = document.createElement('tr'); - jobTr.setAttribute('id', jobId); - - const cancelTd = addCell(jobTr); - let cancelBtn = createButton('Cancel', () => { - onCancelButtonClicked(jobId); - }); - cancelBtn.setAttribute('id', `id ${jobId}-cancelBtn`); - cancelTd.appendChild(cancelBtn); - - const jobIdTd = addCell(jobTr); - jobIdTd.appendChild(document.createTextNode(jobId)); - - let jobStatusTd = addCell(jobTr); - jobStatusTd.id = `${jobId}-status`; - jobStatusTd.appendChild(document.createTextNode(status)); - - document.getElementById('printJobTbody').appendChild(jobTr); - } else { - document.getElementById(`${jobId}-status`).innerText = status; - if (status !== 'PENDING' && status !== 'IN_PROGRESS') { - jobTr.remove(); - } - } - }); -} - -document.addEventListener('DOMContentLoaded', () => { - createPrintersTable(); -}); diff --git a/api-samples/printing/roll-printing/test.pdf b/api-samples/printing/test-rollprinting.pdf similarity index 100% rename from api-samples/printing/roll-printing/test.pdf rename to api-samples/printing/test-rollprinting.pdf