uploadte to corect preview and print the labels for 1 row
This commit is contained in:
@@ -33,10 +33,10 @@ async function handlePrintLabel(printData, sendResponse) {
|
||||
|
||||
// Check if printing API is available
|
||||
if (!chrome.printing || !chrome.printing.getPrinters) {
|
||||
console.error('Chrome printing API not available');
|
||||
console.error('Chrome printing API not available - browser may be Brave, Edge, or older Chrome');
|
||||
sendResponse({
|
||||
success: false,
|
||||
error: 'Chrome printing API not available. Please ensure you are using Chrome 85+ and the extension has proper permissions.'
|
||||
error: 'Direct printing API not available in this browser. The extension works best in Google Chrome 85+. Using fallback method instead.'
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -50,13 +50,21 @@ async function handlePrintLabel(printData, sendResponse) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Find default printer or use first available
|
||||
const defaultPrinter = printers.find(p => p.isDefault) || printers[0];
|
||||
console.log('Using printer:', defaultPrinter);
|
||||
// Find selected printer or use default/first available
|
||||
let selectedPrinter;
|
||||
if (printData.printerId && printData.printerId !== 'default') {
|
||||
selectedPrinter = printers.find(p => p.id === printData.printerId);
|
||||
}
|
||||
|
||||
if (!selectedPrinter) {
|
||||
selectedPrinter = printers.find(p => p.isDefault) || printers[0];
|
||||
}
|
||||
|
||||
console.log('Using printer:', selectedPrinter);
|
||||
|
||||
// Create print job
|
||||
const printJob = {
|
||||
printerId: defaultPrinter.id,
|
||||
printerId: selectedPrinter.id,
|
||||
ticket: {
|
||||
version: '1.0',
|
||||
print: {
|
||||
@@ -100,7 +108,7 @@ async function handlePrintLabel(printData, sendResponse) {
|
||||
lastPrint: {
|
||||
timestamp: Date.now(),
|
||||
jobId: result.jobId,
|
||||
printer: defaultPrinter.displayName
|
||||
printer: selectedPrinter.displayName
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@@ -120,7 +128,7 @@ async function getPrinters(sendResponse) {
|
||||
if (!chrome.printing || !chrome.printing.getPrinters) {
|
||||
sendResponse({
|
||||
success: false,
|
||||
error: 'Chrome printing API not available'
|
||||
error: 'Direct printing API not available in this browser (works in Chrome only)'
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -144,38 +152,108 @@ chrome.runtime.onInstalled.addListener((details) => {
|
||||
});
|
||||
});
|
||||
|
||||
// Fallback print method using tabs API
|
||||
// Fallback print method using tabs API (works in Brave, Edge, Chrome)
|
||||
async function handleFallbackPrint(printData, sendResponse) {
|
||||
try {
|
||||
console.log('Using fallback print method');
|
||||
console.log('Using fallback print method for Brave/Edge/Chrome');
|
||||
|
||||
// Create a new tab with the print content
|
||||
// Create enhanced HTML with better print styles
|
||||
const enhancedHTML = `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Quality Recticel Label</title>
|
||||
<style>
|
||||
@page {
|
||||
margin: 10mm;
|
||||
size: A4;
|
||||
}
|
||||
body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
font-family: Arial, sans-serif;
|
||||
background: white;
|
||||
}
|
||||
.print-container {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
}
|
||||
.label-wrapper {
|
||||
transform: scale(1.5);
|
||||
transform-origin: center;
|
||||
page-break-inside: avoid;
|
||||
}
|
||||
@media print {
|
||||
body { padding: 0; margin: 0; }
|
||||
.print-container { min-height: auto; padding: 20px 0; }
|
||||
.no-print { display: none !important; }
|
||||
}
|
||||
.print-instructions {
|
||||
position: fixed;
|
||||
top: 10px;
|
||||
right: 10px;
|
||||
background: #007bff;
|
||||
color: white;
|
||||
padding: 10px;
|
||||
border-radius: 5px;
|
||||
font-size: 14px;
|
||||
z-index: 1000;
|
||||
}
|
||||
</style>
|
||||
<script>
|
||||
window.onload = function() {
|
||||
// Auto-print after 1 second
|
||||
setTimeout(() => {
|
||||
window.print();
|
||||
// Auto-close after printing (or after 10 seconds)
|
||||
setTimeout(() => {
|
||||
window.close();
|
||||
}, 2000);
|
||||
}, 1000);
|
||||
};
|
||||
|
||||
// Handle print events
|
||||
window.onbeforeprint = function() {
|
||||
console.log('Print dialog opened');
|
||||
};
|
||||
|
||||
window.onafterprint = function() {
|
||||
console.log('Print dialog closed');
|
||||
setTimeout(() => {
|
||||
window.close();
|
||||
}, 500);
|
||||
};
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="print-instructions no-print">
|
||||
🖨️ Printing will start automatically...<br>
|
||||
<small>If dialog doesn't appear, press Ctrl+P</small>
|
||||
</div>
|
||||
<div class="print-container">
|
||||
<div class="label-wrapper">
|
||||
${printData.html.replace(/<!DOCTYPE html>|<html>|<\/html>|<head>.*?<\/head>|<body>|<\/body>/gi, '')}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
|
||||
// Create a new tab with the enhanced print content
|
||||
const tab = await chrome.tabs.create({
|
||||
url: 'data:text/html,' + encodeURIComponent(printData.html),
|
||||
active: false
|
||||
url: 'data:text/html;charset=utf-8,' + encodeURIComponent(enhancedHTML),
|
||||
active: true // Make active so user can see the print dialog
|
||||
});
|
||||
|
||||
// Wait for tab to load, then print
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
await chrome.tabs.executeScript(tab.id, {
|
||||
code: 'window.print();'
|
||||
});
|
||||
|
||||
// Close the tab after printing
|
||||
setTimeout(() => {
|
||||
chrome.tabs.remove(tab.id);
|
||||
}, 1000);
|
||||
|
||||
sendResponse({success: true, method: 'fallback'});
|
||||
} catch (error) {
|
||||
sendResponse({success: false, error: 'Fallback print failed: ' + error.message});
|
||||
}
|
||||
}, 1000);
|
||||
console.log('Created print tab:', tab.id);
|
||||
sendResponse({success: true, method: 'fallback_tab', tabId: tab.id});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Fallback print error:', error);
|
||||
sendResponse({success: false, error: error.message});
|
||||
sendResponse({success: false, error: 'Browser print fallback failed: ' + error.message});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user