Skip to content

Commit

Permalink
Consolidate the roll-printing sample with the regular sample
Browse files Browse the repository at this point in the history
There is now a checkbox on the main UI to select roll printers or all
printers.  For roll printing, this sample now demonstrates how to check
if the printer is capable of roll printing, if the printer is capable of
trimming the paper after printing, and how the media length can be a
variable value.
  • Loading branch information
nmuggli committed Oct 26, 2023
1 parent 3212720 commit 5947e09
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 318 deletions.
2 changes: 2 additions & 0 deletions api-samples/printing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 4 additions & 0 deletions api-samples/printing/printers.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ <h2>Print Job:</h2>

<h2>Printers:</h2>
<div id="printers">
<input type='checkbox' id='rollPrinters' name='rollPrinters'>
<label for='rollPrinters'>Roll Printers</label>
<table id="printersTable">
<thead>
<tr>
Expand All @@ -41,9 +43,11 @@ <h2>Printers:</h2>
<th>Default</th>
<th>Recently used</th>
<th>Capabilities</th>
<th>Supports trim</th>
<th>Status</th>
</tr>
</thead>
<tbody id="tbody"/>
</table>
</div>
</body>
Expand Down
139 changes: 97 additions & 42 deletions api-samples/printing/printers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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();
});
});
13 changes: 0 additions & 13 deletions api-samples/printing/roll-printing/README.md

This file was deleted.

Binary file removed api-samples/printing/roll-printing/icons/icon.png
Binary file not shown.
Binary file removed api-samples/printing/roll-printing/icons/icon128.png
Binary file not shown.
Binary file removed api-samples/printing/roll-printing/icons/icon16.png
Binary file not shown.
Binary file removed api-samples/printing/roll-printing/icons/icon48.png
Binary file not shown.
20 changes: 0 additions & 20 deletions api-samples/printing/roll-printing/manifest.json

This file was deleted.

18 changes: 0 additions & 18 deletions api-samples/printing/roll-printing/printers.css

This file was deleted.

46 changes: 0 additions & 46 deletions api-samples/printing/roll-printing/printers.html

This file was deleted.

Loading

0 comments on commit 5947e09

Please sign in to comment.