Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 54729b1a51 | |||
| 9483f369d7 |
@ -1,170 +0,0 @@
|
||||
# Testing Scenarios
|
||||
|
||||
## Current Status
|
||||
|
||||
The code is working! It correctly detects when no Bluetooth printer is configured and attempts to fall back to standard print.
|
||||
|
||||
## Enhanced Logging
|
||||
|
||||
Added comprehensive logging to track:
|
||||
1. When `originalPrintHtml` is called
|
||||
2. What it returns
|
||||
3. Any errors that occur
|
||||
4. Connection status checks
|
||||
5. Reconnection attempts
|
||||
|
||||
## Test Scenarios
|
||||
|
||||
### Scenario 1: No Bluetooth Printer Configured ✅
|
||||
**Current behavior:**
|
||||
```
|
||||
[BluetoothPrint] printHtml() called
|
||||
[BluetoothPrint] Web Bluetooth API available
|
||||
[BluetoothPrint] No Bluetooth printer configured, using standard print
|
||||
[BluetoothPrint] Calling originalPrintHtml with: [element]
|
||||
[BluetoothPrint] originalPrintHtml returned: [result]
|
||||
```
|
||||
|
||||
**Expected:** Browser print dialog should open
|
||||
**If not working:** Check the console for errors from `originalPrintHtml`
|
||||
|
||||
### Scenario 2: Bluetooth Printer Configured but Not Connected
|
||||
**Steps:**
|
||||
1. Connect to printer once (saves config)
|
||||
2. Disconnect printer or turn it off
|
||||
3. Try to print receipt
|
||||
|
||||
**Expected logs:**
|
||||
```
|
||||
[BluetoothPrint] Bluetooth printer configured: RPP02N
|
||||
[BluetoothPrint] Current connection status: disconnected
|
||||
[BluetoothPrint] Printer not connected, attempting to reconnect...
|
||||
[BluetoothPrint] Reconnection failed: [error]
|
||||
[BluetoothPrint] Falling back to standard print
|
||||
```
|
||||
|
||||
**Expected:** Browser print dialog should open
|
||||
|
||||
### Scenario 3: Bluetooth Printer Connected ✅
|
||||
**Steps:**
|
||||
1. Click Bluetooth icon
|
||||
2. Connect to RPP02N
|
||||
3. Make a sale and print
|
||||
|
||||
**Expected logs:**
|
||||
```
|
||||
[BluetoothPrint] Bluetooth printer configured: RPP02N
|
||||
[BluetoothPrint] Current connection status: connected
|
||||
[BluetoothPrint] Attempting bluetooth print...
|
||||
[BluetoothPrint] Starting bluetooth print from HTML...
|
||||
[BluetoothPrint] Parsing receipt data from HTML...
|
||||
[BluetoothPrint] Parsed X lines from HTML
|
||||
[BluetoothPrint] Generating ESC/POS commands...
|
||||
[BluetoothPrint] Generated XXX bytes of ESC/POS data
|
||||
[BluetoothPrint] Sending to printer...
|
||||
[BluetoothPrint] Print completed successfully
|
||||
```
|
||||
|
||||
**Expected:** Receipt prints to Bluetooth printer
|
||||
|
||||
### Scenario 4: Bluetooth Print Fails Mid-Process
|
||||
**What happens:** Connection drops during print
|
||||
|
||||
**Expected logs:**
|
||||
```
|
||||
[BluetoothPrint] Attempting bluetooth print...
|
||||
[BluetoothPrint] Bluetooth print failed: [error]
|
||||
[BluetoothPrint] Falling back to standard print after error
|
||||
[BluetoothPrint] Fallback print returned: [result]
|
||||
```
|
||||
|
||||
**Expected:** Browser print dialog opens as fallback
|
||||
|
||||
## What to Test Now
|
||||
|
||||
### Test 1: Connect Bluetooth Printer
|
||||
1. Update module: `./odoo-bin -u pos_bluetooth_thermal_printer -d your_database`
|
||||
2. Clear browser cache
|
||||
3. Click Bluetooth icon in POS
|
||||
4. Scan and connect to RPP02N
|
||||
5. Make a sale and print receipt
|
||||
6. **Check console logs**
|
||||
7. **Verify receipt prints to Bluetooth printer**
|
||||
|
||||
### Test 2: Standard Print Fallback
|
||||
1. Disconnect Bluetooth printer (turn off or unpair)
|
||||
2. Make a sale and print receipt
|
||||
3. **Check console logs** - should show reconnection attempt
|
||||
4. **Verify browser print dialog opens**
|
||||
|
||||
## Debugging Standard Print Issue
|
||||
|
||||
If standard print (browser dialog) doesn't open, check:
|
||||
|
||||
### 1. Check Console for Errors
|
||||
Look for:
|
||||
```
|
||||
[BluetoothPrint] Error calling originalPrintHtml: [error]
|
||||
[BluetoothPrint] Standard print failed: [error]
|
||||
[BluetoothPrint] Fallback print also failed: [error]
|
||||
```
|
||||
|
||||
### 2. Check originalPrintHtml Return Value
|
||||
Look for:
|
||||
```
|
||||
[BluetoothPrint] originalPrintHtml returned: [value]
|
||||
```
|
||||
|
||||
If it returns `undefined` or `null`, the original method might not be opening the print dialog.
|
||||
|
||||
### 3. Possible Issues
|
||||
|
||||
**Issue A: Original method doesn't exist**
|
||||
- `originalPrintHtml` might be `undefined`
|
||||
- Check if `PosPrinterService.prototype.printHtml` exists before patching
|
||||
|
||||
**Issue B: Original method needs different context**
|
||||
- Might need to pass additional parameters
|
||||
- Might need different `this` binding
|
||||
|
||||
**Issue C: Browser blocks print dialog**
|
||||
- Some browsers block print dialogs not triggered by user action
|
||||
- Might need user interaction first
|
||||
|
||||
### 4. Alternative Fallback
|
||||
|
||||
If `originalPrintHtml` doesn't work, we can implement a direct print fallback:
|
||||
|
||||
```javascript
|
||||
// Direct browser print as last resort
|
||||
const printWindow = window.open('', '_blank');
|
||||
printWindow.document.write(el.outerHTML);
|
||||
printWindow.document.close();
|
||||
printWindow.print();
|
||||
```
|
||||
|
||||
## Expected Results After Update
|
||||
|
||||
### With Bluetooth Printer Connected:
|
||||
✅ Receipt prints to Bluetooth printer automatically
|
||||
✅ No browser print dialog
|
||||
✅ Console shows successful print process
|
||||
|
||||
### Without Bluetooth Printer:
|
||||
✅ Console shows "No Bluetooth printer configured"
|
||||
✅ Falls back to standard print
|
||||
✅ Browser print dialog opens (or should open)
|
||||
|
||||
### With Bluetooth Error:
|
||||
✅ Console shows error details
|
||||
✅ Falls back to standard print
|
||||
✅ Sale completes successfully
|
||||
|
||||
## Next Steps
|
||||
|
||||
1. **Update module**
|
||||
2. **Test with Bluetooth printer connected** - Should print!
|
||||
3. **Test without Bluetooth printer** - Check if standard print works
|
||||
4. **Share console logs** if any issues
|
||||
|
||||
The Bluetooth printing should work now when printer is connected! 🎉
|
||||
@ -39,6 +39,7 @@ export class BluetoothPrinterConfig extends Component {
|
||||
characterSet: 'CP437',
|
||||
paperWidth: 48,
|
||||
paperWidthMm: 58, // Paper width in millimeters (58mm or 80mm)
|
||||
dpi: 203, // Printer DPI (203 or 304)
|
||||
autoReconnect: true,
|
||||
timeout: 10000,
|
||||
|
||||
@ -83,6 +84,7 @@ export class BluetoothPrinterConfig extends Component {
|
||||
this.state.characterSet = config.settings.characterSet || 'CP437';
|
||||
this.state.paperWidth = config.settings.paperWidth || 48;
|
||||
this.state.paperWidthMm = config.settings.paperWidthMm || 58;
|
||||
this.state.dpi = config.settings.dpi || 203;
|
||||
this.state.autoReconnect = config.settings.autoReconnect !== false;
|
||||
this.state.timeout = config.settings.timeout || 10000;
|
||||
}
|
||||
@ -287,6 +289,15 @@ export class BluetoothPrinterConfig extends Component {
|
||||
this._saveConfiguration();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle DPI change
|
||||
* @param {Event} event - Change event
|
||||
*/
|
||||
onDpiChange(event) {
|
||||
this.state.dpi = parseInt(event.target.value, 10);
|
||||
this._saveConfiguration();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle auto-reconnect toggle
|
||||
* @param {Event} event - Change event
|
||||
@ -325,6 +336,7 @@ export class BluetoothPrinterConfig extends Component {
|
||||
characterSet: this.state.characterSet,
|
||||
paperWidth: this.state.paperWidth,
|
||||
paperWidthMm: this.state.paperWidthMm,
|
||||
dpi: this.state.dpi,
|
||||
autoReconnect: this.state.autoReconnect,
|
||||
timeout: this.state.timeout
|
||||
}
|
||||
|
||||
@ -70,7 +70,7 @@ export class TimeoutError extends Error {
|
||||
export class BluetoothPrinterManager {
|
||||
constructor(errorNotificationService = null) {
|
||||
// Debug logging
|
||||
this.debugMode = true; // Set to false to disable verbose logging
|
||||
this.debugMode = false; // Set to false to disable verbose logging
|
||||
|
||||
// Connection state
|
||||
this.device = null;
|
||||
@ -309,14 +309,14 @@ export class BluetoothPrinterManager {
|
||||
|
||||
for (const serviceUUID of serviceUUIDs) {
|
||||
try {
|
||||
console.log(`Trying service UUID: ${serviceUUID}`);
|
||||
this._log(`Trying service UUID: ${serviceUUID}`);
|
||||
this.service = await this.server.getPrimaryService(serviceUUID);
|
||||
|
||||
// Try to find a writable characteristic
|
||||
for (const charUUID of characteristicUUIDs) {
|
||||
try {
|
||||
this.characteristic = await this.service.getCharacteristic(charUUID);
|
||||
console.log(`Found characteristic: ${charUUID}`);
|
||||
this._log(`Found characteristic: ${charUUID}`);
|
||||
serviceFound = true;
|
||||
break;
|
||||
} catch (charError) {
|
||||
@ -346,7 +346,7 @@ export class BluetoothPrinterManager {
|
||||
if (char.properties.write || char.properties.writeWithoutResponse) {
|
||||
this.service = service;
|
||||
this.characteristic = char;
|
||||
console.log(`Using fallback characteristic: ${char.uuid}`);
|
||||
this._log(`Using fallback characteristic: ${char.uuid}`);
|
||||
serviceFound = true;
|
||||
break;
|
||||
}
|
||||
@ -466,7 +466,7 @@ export class BluetoothPrinterManager {
|
||||
chunks.push(escposData.slice(i, i + chunkSize));
|
||||
}
|
||||
|
||||
console.log(`[Bluetooth] Sending ${chunks.length} chunks (${escposData.length} bytes, ${chunkSize} bytes/chunk)`);
|
||||
this._log(`Sending ${chunks.length} chunks (${escposData.length} bytes, ${chunkSize} bytes/chunk)`);
|
||||
|
||||
// Determine write method based on characteristic properties
|
||||
const useWriteWithoutResponse = this.characteristic.properties.writeWithoutResponse;
|
||||
@ -502,7 +502,7 @@ export class BluetoothPrinterManager {
|
||||
// Progress logging every 20%
|
||||
if (i % Math.ceil(chunks.length / 5) === 0) {
|
||||
const progress = Math.round((i / chunks.length) * 100);
|
||||
console.log(`[Bluetooth] Progress: ${progress}%`);
|
||||
this._log(`Progress: ${progress}%`);
|
||||
}
|
||||
} catch (chunkError) {
|
||||
console.error(`Failed to send chunk ${i + 1}/${chunks.length}:`, chunkError);
|
||||
@ -514,7 +514,7 @@ export class BluetoothPrinterManager {
|
||||
const duration = ((endTime - startTime) / 1000).toFixed(2);
|
||||
const speed = (escposData.length / 1024 / (duration || 1)).toFixed(2);
|
||||
|
||||
console.log(`[Bluetooth] Transmission complete in ${duration}s (${speed} KB/s)`);
|
||||
this._log(`Transmission complete in ${duration}s (${speed} KB/s)`);
|
||||
this.isPrinting = false;
|
||||
this._emit('print-completed', { success: true });
|
||||
|
||||
@ -594,7 +594,7 @@ export class BluetoothPrinterManager {
|
||||
}
|
||||
|
||||
try {
|
||||
console.log(`Reconnection attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts}`);
|
||||
this._log(`Reconnection attempt ${this.reconnectAttempts}/${this.maxReconnectAttempts}`);
|
||||
|
||||
// Try to reconnect
|
||||
await this.connectToPrinter(this.device);
|
||||
@ -602,7 +602,7 @@ export class BluetoothPrinterManager {
|
||||
this.isReconnecting = false;
|
||||
this.reconnectAttempts = 0;
|
||||
|
||||
console.log('Reconnection successful');
|
||||
this._log('Reconnection successful');
|
||||
|
||||
// Emit reconnection success event
|
||||
this._emit('reconnection-success', {
|
||||
@ -630,7 +630,7 @@ export class BluetoothPrinterManager {
|
||||
// Wait before next attempt (exponential backoff)
|
||||
if (attempt < this.maxReconnectAttempts - 1) {
|
||||
const delay = this.reconnectDelays[attempt];
|
||||
console.log(`Waiting ${delay}ms before next attempt`);
|
||||
this._log(`Waiting ${delay}ms before next attempt`);
|
||||
await this._sleep(delay);
|
||||
}
|
||||
}
|
||||
@ -694,12 +694,12 @@ export class BluetoothPrinterManager {
|
||||
* @private
|
||||
*/
|
||||
_onDisconnected() {
|
||||
console.log('Bluetooth device disconnected');
|
||||
this._log('Bluetooth device disconnected');
|
||||
this._setConnectionStatus('disconnected');
|
||||
|
||||
// Attempt auto-reconnection if enabled
|
||||
if (this.autoReconnectEnabled) {
|
||||
console.log('Starting auto-reconnection...');
|
||||
this._log('Starting auto-reconnection...');
|
||||
this.autoReconnect().catch(error => {
|
||||
console.error('Auto-reconnection failed:', error);
|
||||
});
|
||||
|
||||
@ -35,33 +35,38 @@ export class EscPosGraphics {
|
||||
* @returns {Uint8Array} Complete ESC/POS command sequence
|
||||
*/
|
||||
generateBitmapCommands(bitmap) {
|
||||
console.log('[EscPosGraphics] Generating bitmap commands (optimized)...');
|
||||
console.log('[EscPosGraphics] Original dimensions:', bitmap.width, 'x', bitmap.height);
|
||||
|
||||
|
||||
const startTime = performance.now();
|
||||
|
||||
// OPTIMIZATION: Remove blank lines from top and bottom
|
||||
const optimizedBitmap = this._removeBlankLines(bitmap);
|
||||
console.log('[EscPosGraphics] Optimized dimensions:', optimizedBitmap.width, 'x', optimizedBitmap.height);
|
||||
console.log('[EscPosGraphics] Saved', bitmap.height - optimizedBitmap.height, 'blank lines');
|
||||
|
||||
const commands = [];
|
||||
|
||||
// Initialize printer
|
||||
commands.push(...this.initialize());
|
||||
const initCmd = this.initialize();
|
||||
|
||||
// Print bitmap using raster graphics mode
|
||||
const rasterCommands = this._generateRasterGraphics(optimizedBitmap);
|
||||
commands.push(...rasterCommands);
|
||||
|
||||
// Feed paper and cut
|
||||
commands.push(...this._feedAndCut(4));
|
||||
const feedCmd = this._feedAndCut(4);
|
||||
|
||||
const result = new Uint8Array(commands);
|
||||
// Combine all commands safely without spreading large arrays
|
||||
const totalLength = initCmd.length + rasterCommands.length + feedCmd.length;
|
||||
const result = new Uint8Array(totalLength);
|
||||
|
||||
let offset = 0;
|
||||
result.set(initCmd, offset);
|
||||
offset += initCmd.length;
|
||||
|
||||
result.set(rasterCommands, offset);
|
||||
offset += rasterCommands.length;
|
||||
|
||||
result.set(new Uint8Array(feedCmd), offset);
|
||||
|
||||
const endTime = performance.now();
|
||||
console.log('[EscPosGraphics] Command generation took:', (endTime - startTime).toFixed(2), 'ms');
|
||||
console.log('[EscPosGraphics] Generated', result.length, 'bytes of commands');
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
@ -134,7 +139,6 @@ export class EscPosGraphics {
|
||||
* @returns {Array} Command bytes
|
||||
*/
|
||||
_generateRasterGraphics(bitmap) {
|
||||
const commands = [];
|
||||
const { data, width, height, bytesPerLine } = bitmap;
|
||||
|
||||
// Calculate dimensions
|
||||
@ -144,26 +148,29 @@ export class EscPosGraphics {
|
||||
const heightLow = height & 0xFF;
|
||||
const heightHigh = (height >> 8) & 0xFF;
|
||||
|
||||
console.log('[EscPosGraphics] Bitmap width:', width, 'pixels');
|
||||
console.log('[EscPosGraphics] Bitmap height:', height, 'lines');
|
||||
console.log('[EscPosGraphics] Bytes per line:', bytesPerLine);
|
||||
console.log('[EscPosGraphics] Width bytes (xL xH):', widthLow, widthHigh, '=', widthBytes);
|
||||
console.log('[EscPosGraphics] Height (yL yH):', heightLow, heightHigh, '=', height);
|
||||
console.log('[EscPosGraphics] Total data size:', data.length, 'bytes');
|
||||
console.log('[EscPosGraphics] Expected data size:', bytesPerLine * height, 'bytes');
|
||||
|
||||
|
||||
// GS v 0 - Print raster bitmap
|
||||
// Format: GS v 0 m xL xH yL yH d1...dk
|
||||
// m = mode (0 = normal, 1 = double width, 2 = double height, 3 = quadruple)
|
||||
commands.push(GS, 0x76, 0x30, 0x00); // GS v 0 m (m=0 for normal)
|
||||
commands.push(widthLow, widthHigh); // xL xH (width in bytes)
|
||||
commands.push(heightLow, heightHigh); // yL yH (height in dots)
|
||||
// Header length: 8 bytes
|
||||
const header = [
|
||||
GS, 0x76, 0x30, 0x00, // GS v 0 m (m=0 for normal)
|
||||
widthLow, widthHigh, // xL xH (width in bytes)
|
||||
heightLow, heightHigh // yL yH (height in dots)
|
||||
];
|
||||
|
||||
// Add bitmap data
|
||||
commands.push(...data);
|
||||
// Create Uint8Array for the complete command
|
||||
const totalLength = header.length + data.length + 1; // +1 for LF
|
||||
const commands = new Uint8Array(totalLength);
|
||||
|
||||
// Add line feed after image
|
||||
commands.push(LF);
|
||||
// Set header
|
||||
commands.set(header, 0);
|
||||
|
||||
// Set bitmap data
|
||||
commands.set(data, header.length);
|
||||
|
||||
// Set LF
|
||||
commands[totalLength - 1] = LF;
|
||||
|
||||
return commands;
|
||||
}
|
||||
@ -240,7 +247,7 @@ export class EscPosGraphics {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
|
||||
console.log('[EscPosGraphics] Split into', chunks.length, 'chunks');
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
|
||||
@ -5,6 +5,11 @@
|
||||
*
|
||||
* Converts HTML receipt elements to images for thermal printer graphics mode.
|
||||
* Uses canvas to render HTML and convert to bitmap format.
|
||||
*
|
||||
* FIXED: Improved rendering to preserve exact HTML layout, use 100% paper width,
|
||||
* and properly render images, borders, backgrounds, and text alignment.
|
||||
* The improved method uses getBoundingClientRect() to calculate actual element
|
||||
* positions and renders them with full styling preservation.
|
||||
*/
|
||||
|
||||
export class HtmlToImageConverter {
|
||||
@ -13,8 +18,8 @@ export class HtmlToImageConverter {
|
||||
// 58mm paper: 384 pixels (48mm printable * 8 dots/mm)
|
||||
// 80mm paper: 576 pixels (72mm printable * 8 dots/mm)
|
||||
this.paperWidthMm = 58;
|
||||
this.paperWidth = 384; // Default for 58mm
|
||||
this.dpi = 203; // Typical thermal printer DPI
|
||||
this.paperWidth = 464; // Default for 58mm (full width)
|
||||
this.dpi = 203; // Default thermal printer DPI (can be 203 or 304)
|
||||
this.scale = 2; // Higher scale for better quality
|
||||
}
|
||||
|
||||
@ -26,8 +31,7 @@ export class HtmlToImageConverter {
|
||||
* @returns {Promise<HTMLCanvasElement>} Canvas with rendered HTML
|
||||
*/
|
||||
async htmlToCanvas(element) {
|
||||
console.log('[HtmlToImage] Converting HTML to canvas...');
|
||||
console.log('[HtmlToImage] Paper width:', this.paperWidth, 'pixels');
|
||||
|
||||
|
||||
// Clone the element to avoid modifying the original
|
||||
const clone = element.cloneNode(true);
|
||||
@ -37,42 +41,113 @@ export class HtmlToImageConverter {
|
||||
clone.style.maxWidth = `${this.paperWidth}px`;
|
||||
clone.style.minWidth = `${this.paperWidth}px`;
|
||||
clone.style.boxSizing = 'border-box';
|
||||
clone.style.padding = '10px';
|
||||
clone.style.margin = '0';
|
||||
clone.style.padding = '0';
|
||||
clone.style.backgroundColor = 'white';
|
||||
clone.style.color = 'black';
|
||||
clone.style.fontFamily = 'monospace, Courier, "Courier New"';
|
||||
clone.style.fontSize = '12px';
|
||||
clone.style.lineHeight = '1.4';
|
||||
clone.style.overflow = 'visible';
|
||||
|
||||
// Scale all fonts by 150% (3x) to match test print readability
|
||||
// Test print uses ESC/POS text mode which has larger fonts
|
||||
const fontScale = 1.2;
|
||||
|
||||
|
||||
// Create a temporary container to measure height
|
||||
// Container must be large enough to hold the scaled content
|
||||
const container = document.createElement('div');
|
||||
container.style.position = 'fixed';
|
||||
container.style.left = '0';
|
||||
container.style.left = '-9999px'; // Move off-screen instead of using opacity
|
||||
container.style.top = '0';
|
||||
container.style.width = `${this.paperWidth}px`;
|
||||
container.style.maxWidth = `${this.paperWidth}px`;
|
||||
container.style.minWidth = `${this.paperWidth}px`;
|
||||
container.style.backgroundColor = 'white';
|
||||
container.style.overflow = 'visible';
|
||||
container.style.zIndex = '-1000';
|
||||
container.style.opacity = '0';
|
||||
container.style.boxSizing = 'border-box';
|
||||
|
||||
container.appendChild(clone);
|
||||
document.body.appendChild(container);
|
||||
|
||||
const allElements = clone.querySelectorAll('*');
|
||||
allElements.forEach(element => {
|
||||
const style = window.getComputedStyle(element);
|
||||
const currentFontSize = parseFloat(style.fontSize);
|
||||
|
||||
if (currentFontSize > 0) {
|
||||
const newFontSize = currentFontSize * fontScale;
|
||||
element.style.fontSize = `${newFontSize}px`;
|
||||
}
|
||||
|
||||
// Also reduce padding to prevent overflow and cropping
|
||||
const paddingLeft = parseFloat(style.paddingLeft) || 0;
|
||||
const paddingRight = parseFloat(style.paddingRight) || 0;
|
||||
|
||||
// If padding is excessive, reduce it
|
||||
if (paddingLeft > 8) {
|
||||
element.style.paddingLeft = '4px';
|
||||
}
|
||||
if (paddingRight > 8) {
|
||||
element.style.paddingRight = '4px';
|
||||
}
|
||||
});
|
||||
|
||||
// Also scale the root element font size
|
||||
const rootFontSize = parseFloat(window.getComputedStyle(clone).fontSize);
|
||||
if (rootFontSize > 0) {
|
||||
clone.style.fontSize = `${rootFontSize * fontScale}px`;
|
||||
}
|
||||
|
||||
// Reduce root padding to prevent cropping
|
||||
clone.style.paddingLeft = '4px';
|
||||
clone.style.paddingRight = '4px';
|
||||
|
||||
|
||||
|
||||
// Create a temporary container to measure height
|
||||
// Container must be large enough to hold the scaled content
|
||||
// Duplicate container declaration removed
|
||||
container.style.position = 'fixed';
|
||||
container.style.left = '-9999px'; // Move off-screen instead of using opacity
|
||||
container.style.top = '0';
|
||||
container.style.width = `${this.paperWidth}px`;
|
||||
container.style.maxWidth = `${this.paperWidth}px`;
|
||||
container.style.minWidth = `${this.paperWidth}px`;
|
||||
container.style.backgroundColor = 'white';
|
||||
container.style.overflow = 'visible';
|
||||
container.style.zIndex = '-1000';
|
||||
container.style.boxSizing = 'border-box';
|
||||
|
||||
container.appendChild(clone);
|
||||
document.body.appendChild(container);
|
||||
|
||||
try {
|
||||
// Wait for layout to settle and fonts to load
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
// Wait for layout to settle, fonts to load, and images to load
|
||||
await new Promise(resolve => setTimeout(resolve, 200));
|
||||
|
||||
// Load all images in the clone
|
||||
const images = clone.querySelectorAll('img');
|
||||
await Promise.all(Array.from(images).map(img => {
|
||||
return new Promise((resolve) => {
|
||||
if (img.complete) {
|
||||
resolve();
|
||||
} else {
|
||||
img.onload = resolve;
|
||||
img.onerror = resolve; // Continue even if image fails
|
||||
// Timeout after 2 seconds
|
||||
setTimeout(resolve, 2000);
|
||||
}
|
||||
});
|
||||
}));
|
||||
|
||||
// Get the actual rendered dimensions
|
||||
const rect = container.getBoundingClientRect();
|
||||
const rect = clone.getBoundingClientRect();
|
||||
const width = this.paperWidth;
|
||||
const height = Math.max(Math.ceil(rect.height), 100);
|
||||
const height = Math.ceil(rect.height);
|
||||
|
||||
console.log('[HtmlToImage] Measured dimensions:', width, 'x', height);
|
||||
console.log('[HtmlToImage] Container rect:', rect);
|
||||
console.log('[HtmlToImage] Receipt HTML preview (first 1000 chars):', clone.outerHTML.substring(0, 1000));
|
||||
console.log('[HtmlToImage] Receipt text content:', clone.textContent.substring(0, 500));
|
||||
|
||||
// Create canvas
|
||||
|
||||
// Create canvas with exact dimensions
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
@ -82,11 +157,12 @@ export class HtmlToImageConverter {
|
||||
ctx.fillStyle = 'white';
|
||||
ctx.fillRect(0, 0, width, height);
|
||||
|
||||
// Render the DOM to canvas manually
|
||||
console.log('[HtmlToImage] Rendering DOM to canvas...');
|
||||
await this._renderDomToCanvas(clone, ctx, width, height);
|
||||
// Render the DOM to canvas using improved method
|
||||
// Content is already at the correct width, no scaling needed
|
||||
|
||||
await this._renderDomToCanvasImproved(clone, ctx, 0, 0, width, height);
|
||||
|
||||
|
||||
console.log('[HtmlToImage] Canvas rendering complete');
|
||||
return canvas;
|
||||
|
||||
} finally {
|
||||
@ -96,7 +172,322 @@ export class HtmlToImageConverter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Render DOM element to canvas - Simple and reliable approach
|
||||
* Render DOM element to canvas - Improved method that preserves layout
|
||||
* Recursively renders elements with proper positioning, styling, and images
|
||||
*
|
||||
* @private
|
||||
* @param {HTMLElement} element - Element to render
|
||||
* @param {CanvasRenderingContext2D} ctx - Canvas context
|
||||
* @param {number} offsetX - X offset for rendering
|
||||
* @param {number} offsetY - Y offset for rendering
|
||||
* @param {number} canvasWidth - Canvas width
|
||||
* @param {number} canvasHeight - Canvas height
|
||||
* @returns {Promise<number>} Height used
|
||||
*/
|
||||
async _renderDomToCanvasImproved(element, ctx, offsetX, offsetY, canvasWidth, canvasHeight) {
|
||||
|
||||
|
||||
// Background is already filled, no need to fill again
|
||||
|
||||
// Get the bounding rect of the root element to calculate offsets
|
||||
const rootRect = element.getBoundingClientRect();
|
||||
|
||||
// Render element recursively with proper offset calculation
|
||||
await this._renderElement(element, ctx, -rootRect.left, -rootRect.top, rootRect);
|
||||
|
||||
|
||||
return canvasHeight;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively render an element and its children
|
||||
*
|
||||
* @private
|
||||
* @param {HTMLElement} element - Element to render
|
||||
* @param {CanvasRenderingContext2D} ctx - Canvas context
|
||||
* @param {number} offsetX - X offset
|
||||
* @param {number} offsetY - Y offset
|
||||
* @param {DOMRect} rootRect - Root element bounding rect for reference
|
||||
*/
|
||||
async _renderElement(element, ctx, offsetX, offsetY, rootRect) {
|
||||
if (!element || element.nodeType !== Node.ELEMENT_NODE) {
|
||||
return;
|
||||
}
|
||||
|
||||
const style = window.getComputedStyle(element);
|
||||
|
||||
// Skip hidden elements
|
||||
if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') {
|
||||
return;
|
||||
}
|
||||
|
||||
const rect = element.getBoundingClientRect();
|
||||
const x = rect.left + offsetX;
|
||||
const y = rect.top + offsetY;
|
||||
|
||||
// Render background
|
||||
const bgColor = style.backgroundColor;
|
||||
if (bgColor && bgColor !== 'rgba(0, 0, 0, 0)' && bgColor !== 'transparent') {
|
||||
ctx.fillStyle = bgColor;
|
||||
ctx.fillRect(x, y, rect.width, rect.height);
|
||||
}
|
||||
|
||||
// Render borders
|
||||
this._renderBorders(ctx, x, y, rect.width, rect.height, style);
|
||||
|
||||
// Render images
|
||||
if (element.tagName === 'IMG') {
|
||||
await this._renderImage(element, ctx, x, y, rect.width, rect.height);
|
||||
}
|
||||
|
||||
// Render children (text and elements)
|
||||
if (element.childNodes.length > 0) {
|
||||
// Check if this is a specialized text-only element (optimization + wrapping handling)
|
||||
// We keep this optimization for simple blocks, but improve the check
|
||||
// Actually, to fix mixed content, we should just iterate childNodes.
|
||||
// But we need to handle wrapping for long text nodes.
|
||||
// For now, let's assume standard recursion is safer for mixed content.
|
||||
|
||||
const hasOnlyText = Array.from(element.childNodes).every(
|
||||
node => node.nodeType === Node.TEXT_NODE || (node.nodeType === Node.ELEMENT_NODE && node.tagName === 'BR')
|
||||
);
|
||||
|
||||
// Use the block text renderer mainly for simple text blocks which might need wrapping logic
|
||||
// that _renderText provides (it handles word wrapping based on width).
|
||||
// However, relying ONLY on this means mixed content fails.
|
||||
|
||||
if (hasOnlyText) {
|
||||
this._renderText(element, ctx, x, y, rect.width, rect.height, style);
|
||||
} else {
|
||||
// Mixed content or complex structure
|
||||
for (const node of element.childNodes) {
|
||||
if (node.nodeType === Node.ELEMENT_NODE) {
|
||||
await this._renderElement(node, ctx, offsetX, offsetY, rootRect);
|
||||
} else if (node.nodeType === Node.TEXT_NODE) {
|
||||
const text = node.textContent.trim();
|
||||
if (text) {
|
||||
// Text node in mixed content
|
||||
// We need its exact position
|
||||
const range = document.createRange();
|
||||
range.selectNode(node);
|
||||
const textRect = range.getBoundingClientRect();
|
||||
|
||||
// Calculate relative position
|
||||
const textX = textRect.left + offsetX;
|
||||
const textY = textRect.top + offsetY;
|
||||
|
||||
// Draw the text node using parent's style
|
||||
// Note: Text nodes don't have padding/margin themselves effectively (handled by layout)
|
||||
this._drawTextNode(ctx, text, textX, textY, textRect.width, style);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw a single text node
|
||||
*
|
||||
* @private
|
||||
* @param {CanvasRenderingContext2D} ctx - Canvas context
|
||||
* @param {string} text - Text to draw
|
||||
* @param {number} x - X position
|
||||
* @param {number} y - Y position
|
||||
* @param {number} width - Node width
|
||||
* @param {CSSStyleDeclaration} style - Computed style of parent
|
||||
*/
|
||||
_drawTextNode(ctx, text, x, y, width, style) {
|
||||
// Set font
|
||||
const fontSize = parseFloat(style.fontSize) || 12;
|
||||
const fontWeight = style.fontWeight;
|
||||
const fontFamily = style.fontFamily.split(',')[0].replace(/['"]/g, '') || 'monospace';
|
||||
const isBold = fontWeight === 'bold' || parseInt(fontWeight) >= 600;
|
||||
|
||||
ctx.font = `${isBold ? 'bold' : 'normal'} ${fontSize}px ${fontFamily}`;
|
||||
ctx.fillStyle = style.color || 'black';
|
||||
ctx.textBaseline = 'top'; // Align to top of rect
|
||||
|
||||
// Handle alignment - though for a text node range rect, the rect SHOULD already be aligned by layout
|
||||
// So we can arguably just draw at x.
|
||||
// However, range rects can be tricky.
|
||||
|
||||
// Simple draw for now - rely on DOM layout for positioning
|
||||
ctx.textAlign = 'left';
|
||||
|
||||
// Text nodes usually don't need wrapping if they are part of a laid-out line
|
||||
// (the browser already wrapped them into lines, and if it spans lines, we might get a big rect)
|
||||
// If it spans lines, this simple fillText might be issue, but for missing words like "Total", it's fine.
|
||||
ctx.fillText(text, x, y);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render text content of an element
|
||||
*
|
||||
* @private
|
||||
* @param {HTMLElement} element - Element containing text
|
||||
* @param {CanvasRenderingContext2D} ctx - Canvas context
|
||||
* @param {number} x - X position
|
||||
* @param {number} y - Y position
|
||||
* @param {number} width - Element width
|
||||
* @param {number} height - Element height
|
||||
* @param {CSSStyleDeclaration} style - Computed style
|
||||
*/
|
||||
_renderText(element, ctx, x, y, width, height, style) {
|
||||
const text = element.textContent.trim();
|
||||
if (!text) return;
|
||||
|
||||
// Set font
|
||||
const fontSize = parseFloat(style.fontSize) || 12;
|
||||
const fontWeight = style.fontWeight;
|
||||
const fontFamily = style.fontFamily.split(',')[0].replace(/['"]/g, '') || 'monospace';
|
||||
const isBold = fontWeight === 'bold' || parseInt(fontWeight) >= 600;
|
||||
|
||||
ctx.font = `${isBold ? 'bold' : 'normal'} ${fontSize}px ${fontFamily}`;
|
||||
ctx.fillStyle = style.color || 'black';
|
||||
ctx.textBaseline = 'top';
|
||||
|
||||
// Handle text alignment
|
||||
const textAlign = style.textAlign || 'left';
|
||||
const paddingLeft = parseFloat(style.paddingLeft || 0);
|
||||
const paddingRight = parseFloat(style.paddingRight || 0);
|
||||
const paddingTop = parseFloat(style.paddingTop || 0);
|
||||
|
||||
let textX = x + paddingLeft;
|
||||
|
||||
if (textAlign === 'center') {
|
||||
ctx.textAlign = 'center';
|
||||
textX = x + width / 2;
|
||||
} else if (textAlign === 'right') {
|
||||
ctx.textAlign = 'right';
|
||||
textX = x + width - paddingRight;
|
||||
} else {
|
||||
ctx.textAlign = 'left';
|
||||
}
|
||||
|
||||
const textY = y + paddingTop;
|
||||
|
||||
// Word wrap if needed
|
||||
const maxWidth = width - paddingLeft - paddingRight;
|
||||
const lineHeight = parseFloat(style.lineHeight) || fontSize * 1.4;
|
||||
|
||||
this._wrapAndDrawText(ctx, text, textX, textY, maxWidth, lineHeight);
|
||||
|
||||
// Reset text align
|
||||
ctx.textAlign = 'left';
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap and draw text with proper line breaks
|
||||
*
|
||||
* @private
|
||||
* @param {CanvasRenderingContext2D} ctx - Canvas context
|
||||
* @param {string} text - Text to draw
|
||||
* @param {number} x - X position
|
||||
* @param {number} y - Y position
|
||||
* @param {number} maxWidth - Maximum width
|
||||
* @param {number} lineHeight - Line height
|
||||
*/
|
||||
_wrapAndDrawText(ctx, text, x, y, maxWidth, lineHeight) {
|
||||
const words = text.split(' ');
|
||||
let line = '';
|
||||
let currentY = y;
|
||||
|
||||
for (let i = 0; i < words.length; i++) {
|
||||
const testLine = line + (line ? ' ' : '') + words[i];
|
||||
const metrics = ctx.measureText(testLine);
|
||||
|
||||
if (metrics.width > maxWidth && line) {
|
||||
ctx.fillText(line, x, currentY);
|
||||
line = words[i];
|
||||
currentY += lineHeight;
|
||||
} else {
|
||||
line = testLine;
|
||||
}
|
||||
}
|
||||
|
||||
if (line) {
|
||||
ctx.fillText(line, x, currentY);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render borders of an element
|
||||
*
|
||||
* @private
|
||||
* @param {CanvasRenderingContext2D} ctx - Canvas context
|
||||
* @param {number} x - X position
|
||||
* @param {number} y - Y position
|
||||
* @param {number} width - Element width
|
||||
* @param {number} height - Element height
|
||||
* @param {CSSStyleDeclaration} style - Computed style
|
||||
*/
|
||||
_renderBorders(ctx, x, y, width, height, style) {
|
||||
const borderTop = parseFloat(style.borderTopWidth) || 0;
|
||||
const borderRight = parseFloat(style.borderRightWidth) || 0;
|
||||
const borderBottom = parseFloat(style.borderBottomWidth) || 0;
|
||||
const borderLeft = parseFloat(style.borderLeftWidth) || 0;
|
||||
|
||||
if (borderTop > 0) {
|
||||
ctx.strokeStyle = style.borderTopColor || 'black';
|
||||
ctx.lineWidth = borderTop;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x, y);
|
||||
ctx.lineTo(x + width, y);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
if (borderRight > 0) {
|
||||
ctx.strokeStyle = style.borderRightColor || 'black';
|
||||
ctx.lineWidth = borderRight;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x + width, y);
|
||||
ctx.lineTo(x + width, y + height);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
if (borderBottom > 0) {
|
||||
ctx.strokeStyle = style.borderBottomColor || 'black';
|
||||
ctx.lineWidth = borderBottom;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x, y + height);
|
||||
ctx.lineTo(x + width, y + height);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
if (borderLeft > 0) {
|
||||
ctx.strokeStyle = style.borderLeftColor || 'black';
|
||||
ctx.lineWidth = borderLeft;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x, y);
|
||||
ctx.lineTo(x, y + height);
|
||||
ctx.stroke();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render an image element
|
||||
*
|
||||
* @private
|
||||
* @param {HTMLImageElement} img - Image element
|
||||
* @param {CanvasRenderingContext2D} ctx - Canvas context
|
||||
* @param {number} x - X position
|
||||
* @param {number} y - Y position
|
||||
* @param {number} width - Display width
|
||||
* @param {number} height - Display height
|
||||
*/
|
||||
async _renderImage(img, ctx, x, y, width, height) {
|
||||
try {
|
||||
if (img.complete && img.naturalWidth > 0) {
|
||||
ctx.drawImage(img, x, y, width, height);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('[HtmlToImage] Failed to render image:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Render DOM element to canvas - Simple fallback method
|
||||
* Extracts text content and renders it line by line with proper formatting
|
||||
*
|
||||
* @private
|
||||
@ -106,7 +497,7 @@ export class HtmlToImageConverter {
|
||||
* @param {number} height - Canvas height
|
||||
*/
|
||||
async _renderDomToCanvas(element, ctx, width, height) {
|
||||
console.log('[HtmlToImage] Rendering DOM to canvas (simple method)...');
|
||||
|
||||
|
||||
// Set default styles
|
||||
ctx.fillStyle = 'white';
|
||||
@ -239,7 +630,7 @@ export class HtmlToImageConverter {
|
||||
// Start processing
|
||||
try {
|
||||
processElement(element);
|
||||
console.log('[HtmlToImage] Rendering complete, height used:', y);
|
||||
|
||||
} catch (error) {
|
||||
console.error('[HtmlToImage] Error during rendering:', error);
|
||||
// Ultimate fallback - just print all text
|
||||
@ -259,7 +650,7 @@ export class HtmlToImageConverter {
|
||||
* @returns {Uint8Array} Bitmap data
|
||||
*/
|
||||
canvasToBitmap(canvas) {
|
||||
console.log('[HtmlToImage] Converting canvas to bitmap (optimized)...');
|
||||
|
||||
const startTime = performance.now();
|
||||
|
||||
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
||||
@ -317,11 +708,7 @@ export class HtmlToImageConverter {
|
||||
}
|
||||
|
||||
const endTime = performance.now();
|
||||
console.log('[HtmlToImage] Bitmap conversion took:', (endTime - startTime).toFixed(2), 'ms');
|
||||
console.log('[HtmlToImage] Bitmap dimensions:', width, 'pixels x', height, 'pixels');
|
||||
console.log('[HtmlToImage] Bytes per line:', bytesPerLine, 'bytes');
|
||||
console.log('[HtmlToImage] Total bitmap size:', bitmapData.length, 'bytes');
|
||||
console.log('[HtmlToImage] Expected size:', bytesPerLine * height, 'bytes');
|
||||
|
||||
|
||||
return {
|
||||
data: bitmapData,
|
||||
@ -338,7 +725,7 @@ export class HtmlToImageConverter {
|
||||
* @returns {Promise<Object>} Bitmap data with dimensions
|
||||
*/
|
||||
async htmlToBitmap(element) {
|
||||
console.log('[HtmlToImage] Converting HTML to bitmap...');
|
||||
|
||||
|
||||
try {
|
||||
// Convert HTML to canvas
|
||||
@ -347,7 +734,7 @@ export class HtmlToImageConverter {
|
||||
// Convert canvas to bitmap
|
||||
const bitmap = this.canvasToBitmap(canvas);
|
||||
|
||||
console.log('[HtmlToImage] Conversion complete');
|
||||
|
||||
return bitmap;
|
||||
} catch (error) {
|
||||
console.error('[HtmlToImage] Conversion failed:', error);
|
||||
@ -356,28 +743,37 @@ export class HtmlToImageConverter {
|
||||
}
|
||||
|
||||
/**
|
||||
* Set paper width in millimeters
|
||||
* Set paper width in millimeters and DPI
|
||||
*
|
||||
* @param {number} widthMm - Paper width in millimeters (58 or 80)
|
||||
* @param {number} dpi - Printer DPI (203 or 304), defaults to 203
|
||||
*/
|
||||
setPaperWidth(widthMm) {
|
||||
setPaperWidth(widthMm, dpi = 203) {
|
||||
this.paperWidthMm = widthMm;
|
||||
this.dpi = dpi;
|
||||
|
||||
// Calculate dots per mm based on DPI
|
||||
// 203 DPI = 8 dots/mm (203 / 25.4)
|
||||
// 304 DPI = 12 dots/mm (304 / 25.4)
|
||||
const dotsPerMm = Math.round(dpi / 25.4);
|
||||
|
||||
// Calculate pixel width based on PRINTABLE width (essential for thermal printers)
|
||||
// 58mm Paper -> ~48mm Printable
|
||||
// 80mm Paper -> ~72mm Printable
|
||||
let printableWidthMm;
|
||||
|
||||
// Calculate pixel width based on paper size
|
||||
// Thermal printers: 8 dots per mm
|
||||
// Account for margins (5mm on each side)
|
||||
if (widthMm === 58) {
|
||||
// 58mm paper: 48mm printable width = 384 pixels
|
||||
this.paperWidth = 384;
|
||||
printableWidthMm = 48; // Standard printable width for 58mm
|
||||
} else if (widthMm === 80) {
|
||||
// 80mm paper: 72mm printable width = 576 pixels
|
||||
this.paperWidth = 576;
|
||||
printableWidthMm = 72; // Standard printable width for 80mm
|
||||
} else {
|
||||
// Custom width: use full width minus 10mm margins
|
||||
this.paperWidth = (widthMm - 10) * 8;
|
||||
// Custom width: subtract ~10mm margin
|
||||
printableWidthMm = Math.max(widthMm - 10, widthMm * 0.8);
|
||||
}
|
||||
|
||||
console.log('[HtmlToImage] Setting paper width to', widthMm, 'mm (', this.paperWidth, 'pixels)');
|
||||
this.paperWidth = Math.floor(printableWidthMm * dotsPerMm);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -60,18 +60,15 @@ patch(PosPrinterService.prototype, {
|
||||
async printHtml(el) {
|
||||
// Wrap entire method in try-catch to catch any errors
|
||||
try {
|
||||
console.log('[BluetoothPrint] printHtml() called');
|
||||
console.log('[BluetoothPrint] Element type:', el?.constructor?.name);
|
||||
console.log('[BluetoothPrint] Element tag:', el?.tagName);
|
||||
console.log('[BluetoothPrint] Element classes:', el?.className);
|
||||
|
||||
|
||||
// Check if Web Bluetooth API is available
|
||||
if (!navigator.bluetooth) {
|
||||
console.log('[BluetoothPrint] Web Bluetooth API not available, using browser print dialog');
|
||||
|
||||
await this._printViaBrowserDialog(el);
|
||||
return true;
|
||||
}
|
||||
console.log('[BluetoothPrint] Web Bluetooth API available');
|
||||
|
||||
|
||||
// Check if a Bluetooth printer is configured (from localStorage)
|
||||
// We don't need POS config - we check if user has configured a printer
|
||||
@ -79,16 +76,15 @@ patch(PosPrinterService.prototype, {
|
||||
const config = storage.loadConfiguration(1); // Default POS config ID
|
||||
|
||||
if (!config || !config.deviceId) {
|
||||
console.log('[BluetoothPrint] No Bluetooth printer configured, using standard print');
|
||||
console.log('[BluetoothPrint] Calling originalPrintHtml with:', el);
|
||||
|
||||
try {
|
||||
const result = await originalPrintHtml.call(this, el);
|
||||
console.log('[BluetoothPrint] originalPrintHtml returned:', result);
|
||||
|
||||
|
||||
// If original method returned false, it didn't handle the print
|
||||
// So we need to handle it ourselves
|
||||
if (result === false) {
|
||||
console.log('[BluetoothPrint] originalPrintHtml returned false, opening browser print dialog directly');
|
||||
|
||||
await this._printViaBrowserDialog(el);
|
||||
return true;
|
||||
}
|
||||
@ -97,32 +93,30 @@ patch(PosPrinterService.prototype, {
|
||||
} catch (error) {
|
||||
console.error('[BluetoothPrint] Error calling originalPrintHtml:', error);
|
||||
// Fallback to browser print dialog
|
||||
console.log('[BluetoothPrint] Falling back to browser print dialog');
|
||||
|
||||
await this._printViaBrowserDialog(el);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
console.log('[BluetoothPrint] Bluetooth printer configured:', config.deviceName);
|
||||
|
||||
|
||||
// Initialize bluetooth services
|
||||
console.log('[BluetoothPrint] Initializing bluetooth services...');
|
||||
const services = initializeBluetoothPrinting(null);
|
||||
console.log('[BluetoothPrint] Bluetooth services initialized');
|
||||
const services = initializeBluetoothPrinting(null); // Restored
|
||||
|
||||
// Check if printer is actually connected
|
||||
const connectionStatus = services.bluetoothManager.getConnectionStatus();
|
||||
console.log('[BluetoothPrint] Current connection status:', connectionStatus);
|
||||
|
||||
|
||||
if (connectionStatus !== 'connected') {
|
||||
console.log('[BluetoothPrint] Printer not connected, attempting to reconnect...');
|
||||
|
||||
// Try to reconnect
|
||||
try {
|
||||
await services.bluetoothManager.autoReconnect();
|
||||
console.log('[BluetoothPrint] Reconnection successful');
|
||||
|
||||
} catch (reconnectError) {
|
||||
console.error('[BluetoothPrint] Reconnection failed:', reconnectError);
|
||||
console.log('[BluetoothPrint] Falling back to browser print dialog');
|
||||
|
||||
await this._printViaBrowserDialog(el);
|
||||
return true;
|
||||
}
|
||||
@ -130,17 +124,17 @@ patch(PosPrinterService.prototype, {
|
||||
|
||||
try {
|
||||
// Attempt bluetooth printing
|
||||
console.log('[BluetoothPrint] Attempting bluetooth print...');
|
||||
|
||||
await this._printViaBluetoothFromHtml(el, services, config);
|
||||
|
||||
console.log('[BluetoothPrint] Print completed successfully');
|
||||
|
||||
return true;
|
||||
} catch (printError) {
|
||||
console.error('[BluetoothPrint] Bluetooth print failed:', printError);
|
||||
console.error('[BluetoothPrint] Error stack:', printError.stack);
|
||||
|
||||
// Fallback to browser print dialog
|
||||
console.log('[BluetoothPrint] Falling back to browser print dialog after error');
|
||||
|
||||
await this._printViaBrowserDialog(el);
|
||||
return true;
|
||||
}
|
||||
@ -149,7 +143,7 @@ patch(PosPrinterService.prototype, {
|
||||
console.error('[BluetoothPrint] Error name:', error.name);
|
||||
console.error('[BluetoothPrint] Error message:', error.message);
|
||||
console.error('[BluetoothPrint] Error stack:', error.stack);
|
||||
console.log('[BluetoothPrint] Falling back to browser print dialog due to critical error');
|
||||
|
||||
try {
|
||||
await this._printViaBrowserDialog(el);
|
||||
return true;
|
||||
@ -169,7 +163,7 @@ patch(PosPrinterService.prototype, {
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async _printViaBrowserDialog(el) {
|
||||
console.log('[BluetoothPrint] Opening browser print dialog...');
|
||||
|
||||
|
||||
return new Promise((resolve) => {
|
||||
// Create a hidden iframe for printing
|
||||
@ -191,7 +185,7 @@ patch(PosPrinterService.prototype, {
|
||||
}
|
||||
if (printFrame.parentNode) {
|
||||
document.body.removeChild(printFrame);
|
||||
console.log('[BluetoothPrint] Iframe cleaned up');
|
||||
|
||||
}
|
||||
resolve();
|
||||
};
|
||||
@ -205,7 +199,7 @@ patch(PosPrinterService.prototype, {
|
||||
try {
|
||||
printFrame.contentWindow.focus();
|
||||
printFrame.contentWindow.print();
|
||||
console.log('[BluetoothPrint] Print dialog opened');
|
||||
|
||||
|
||||
// Clean up after a delay
|
||||
setTimeout(cleanup, 1000);
|
||||
@ -266,11 +260,11 @@ patch(PosPrinterService.prototype, {
|
||||
frameDoc.write(printHtml);
|
||||
frameDoc.close();
|
||||
|
||||
console.log('[BluetoothPrint] Receipt content written to iframe');
|
||||
|
||||
|
||||
// Wait for content to load
|
||||
printFrame.contentWindow.addEventListener('load', () => {
|
||||
console.log('[BluetoothPrint] Iframe loaded, triggering print...');
|
||||
|
||||
// Small delay to ensure rendering is complete
|
||||
setTimeout(triggerPrint, 100);
|
||||
});
|
||||
@ -278,7 +272,7 @@ patch(PosPrinterService.prototype, {
|
||||
// Fallback if load event doesn't fire within 2 seconds
|
||||
timeoutId = setTimeout(() => {
|
||||
if (!printTriggered) {
|
||||
console.log('[BluetoothPrint] Load timeout, attempting print anyway...');
|
||||
|
||||
triggerPrint();
|
||||
}
|
||||
}, 2000);
|
||||
@ -304,14 +298,11 @@ patch(PosPrinterService.prototype, {
|
||||
async _printViaBluetoothFromHtml(el, services, config) {
|
||||
const { bluetoothManager } = services;
|
||||
|
||||
console.log('[BluetoothPrint] Starting bluetooth print from HTML...');
|
||||
console.log('[BluetoothPrint] Using GRAPHICS MODE for exact HTML layout');
|
||||
console.log('[BluetoothPrint] Element:', el);
|
||||
console.log('[BluetoothPrint] Config:', config);
|
||||
|
||||
|
||||
// Check connection status
|
||||
const status = bluetoothManager.getConnectionStatus();
|
||||
console.log('[BluetoothPrint] Connection status:', status);
|
||||
|
||||
|
||||
if (status !== 'connected') {
|
||||
throw new PrinterNotConnectedError('Bluetooth printer is not connected');
|
||||
@ -319,46 +310,32 @@ patch(PosPrinterService.prototype, {
|
||||
|
||||
try {
|
||||
// Convert HTML to bitmap image
|
||||
console.log('[BluetoothPrint] Converting HTML to bitmap...');
|
||||
console.log('[BluetoothPrint] Creating HtmlToImageConverter...');
|
||||
const converter = new HtmlToImageConverter();
|
||||
console.log('[BluetoothPrint] HtmlToImageConverter created successfully');
|
||||
|
||||
// Get paper width from configuration
|
||||
// Get paper width and DPI from configuration
|
||||
|
||||
|
||||
// Get paper width and DPI from configuration
|
||||
const paperWidthMm = config?.settings?.paperWidthMm || 58;
|
||||
console.log('[BluetoothPrint] Using paper width:', paperWidthMm, 'mm');
|
||||
console.log('[BluetoothPrint] Setting paper width on converter...');
|
||||
converter.setPaperWidth(paperWidthMm);
|
||||
console.log('[BluetoothPrint] Paper width set successfully');
|
||||
const dpi = config?.settings?.dpi || 203;
|
||||
converter.setPaperWidth(paperWidthMm, dpi);
|
||||
|
||||
console.log('[BluetoothPrint] Converting HTML to bitmap...');
|
||||
const bitmap = await converter.htmlToBitmap(el);
|
||||
console.log('[BluetoothPrint] Bitmap created:', bitmap.width, 'x', bitmap.height);
|
||||
console.log('[BluetoothPrint] Bitmap data length:', bitmap.data.length);
|
||||
|
||||
// Generate ESC/POS graphics commands
|
||||
console.log('[BluetoothPrint] Generating ESC/POS graphics commands...');
|
||||
console.log('[BluetoothPrint] Creating EscPosGraphics...');
|
||||
const graphicsGenerator = new EscPosGraphics();
|
||||
console.log('[BluetoothPrint] EscPosGraphics created successfully');
|
||||
|
||||
console.log('[BluetoothPrint] Generating bitmap commands...');
|
||||
const escposData = graphicsGenerator.generateBitmapCommands(bitmap);
|
||||
console.log('[BluetoothPrint] Generated', escposData.length, 'bytes of graphics data');
|
||||
|
||||
// Send data to printer
|
||||
console.log('[BluetoothPrint] Sending graphics to printer...');
|
||||
const startTime = performance.now();
|
||||
await bluetoothManager.sendData(escposData);
|
||||
const endTime = performance.now();
|
||||
console.log('[BluetoothPrint] Graphics print completed successfully in', (endTime - startTime).toFixed(0), 'ms');
|
||||
|
||||
} catch (error) {
|
||||
console.error('[BluetoothPrint] Graphics mode failed:', error);
|
||||
console.error('[BluetoothPrint] Error name:', error.name);
|
||||
console.error('[BluetoothPrint] Error message:', error.message);
|
||||
console.error('[BluetoothPrint] Error stack:', error.stack);
|
||||
console.log('[BluetoothPrint] Falling back to text mode...');
|
||||
|
||||
|
||||
// Fallback to text mode if graphics fails
|
||||
await this._printViaBluetoothTextMode(el, services);
|
||||
@ -377,22 +354,16 @@ patch(PosPrinterService.prototype, {
|
||||
async _printViaBluetoothTextMode(el, services) {
|
||||
const { bluetoothManager, escposGenerator } = services;
|
||||
|
||||
console.log('[BluetoothPrint] Using TEXT MODE (fallback)');
|
||||
|
||||
|
||||
// Parse receipt data from HTML element
|
||||
console.log('[BluetoothPrint] Parsing receipt data from HTML...');
|
||||
const receiptData = this._parseReceiptDataFromHtml(el);
|
||||
console.log('[BluetoothPrint] Parsed receipt data:', JSON.stringify(receiptData, null, 2));
|
||||
|
||||
// Generate ESC/POS commands
|
||||
console.log('[BluetoothPrint] Generating ESC/POS text commands...');
|
||||
const escposData = escposGenerator.generateReceipt(receiptData);
|
||||
console.log('[BluetoothPrint] Generated', escposData.length, 'bytes of text data');
|
||||
|
||||
// Send data to printer
|
||||
console.log('[BluetoothPrint] Sending text to printer...');
|
||||
await bluetoothManager.sendData(escposData);
|
||||
console.log('[BluetoothPrint] Text print completed successfully');
|
||||
},
|
||||
|
||||
/**
|
||||
@ -405,34 +376,25 @@ patch(PosPrinterService.prototype, {
|
||||
* @throws {Error} If printing fails
|
||||
*/
|
||||
async _printViaBluetooth(el, pos) {
|
||||
|
||||
const services = initializeBluetoothPrinting();
|
||||
const { bluetoothManager, escposGenerator } = services;
|
||||
|
||||
console.log('[BluetoothPrint] Starting bluetooth print...');
|
||||
console.log('[BluetoothPrint] Element:', el);
|
||||
|
||||
// Check connection status
|
||||
const status = bluetoothManager.getConnectionStatus();
|
||||
console.log('[BluetoothPrint] Connection status:', status);
|
||||
|
||||
if (status !== 'connected') {
|
||||
throw new PrinterNotConnectedError('Bluetooth printer is not connected');
|
||||
}
|
||||
|
||||
// Parse receipt data from POS order
|
||||
console.log('[BluetoothPrint] Parsing receipt data...');
|
||||
const receiptData = this._parseReceiptDataFromPos(pos);
|
||||
console.log('[BluetoothPrint] Receipt data:', receiptData);
|
||||
|
||||
// Generate ESC/POS commands
|
||||
console.log('[BluetoothPrint] Generating ESC/POS commands...');
|
||||
const escposData = escposGenerator.generateReceipt(receiptData);
|
||||
console.log('[BluetoothPrint] Generated', escposData.length, 'bytes of ESC/POS data');
|
||||
|
||||
// Send data to printer
|
||||
console.log('[BluetoothPrint] Sending to printer...');
|
||||
await bluetoothManager.sendData(escposData);
|
||||
console.log('[BluetoothPrint] Print completed successfully');
|
||||
},
|
||||
|
||||
/**
|
||||
@ -543,22 +505,16 @@ patch(PosPrinterService.prototype, {
|
||||
* @returns {Object} Structured receipt data
|
||||
*/
|
||||
_parseReceiptDataFromHtml(el) {
|
||||
console.log('[BluetoothPrint] _parseReceiptDataFromHtml called');
|
||||
console.log('[BluetoothPrint] Receipt HTML structure:', el.outerHTML.substring(0, 500));
|
||||
console.log('[BluetoothPrint] Receipt classes:', el.className);
|
||||
console.log('[BluetoothPrint] Receipt children count:', el.children.length);
|
||||
|
||||
// Extract text content from HTML
|
||||
const getText = (selector) => {
|
||||
const element = el.querySelector(selector);
|
||||
const text = element ? element.textContent.trim() : '';
|
||||
console.log(`[BluetoothPrint] getText('${selector}'):`, text || '(empty)');
|
||||
return text;
|
||||
};
|
||||
|
||||
const getAll = (selector) => {
|
||||
const elements = Array.from(el.querySelectorAll(selector));
|
||||
console.log(`[BluetoothPrint] getAll('${selector}'):`, elements.length, 'elements found');
|
||||
return elements;
|
||||
};
|
||||
|
||||
@ -579,7 +535,6 @@ patch(PosPrinterService.prototype, {
|
||||
};
|
||||
|
||||
// Parse order lines - try multiple selectors
|
||||
console.log('[BluetoothPrint] Searching for order lines...');
|
||||
let lineElements = getAll('.orderline');
|
||||
if (lineElements.length === 0) {
|
||||
lineElements = getAll('.pos-receipt-orderline');
|
||||
@ -595,17 +550,14 @@ patch(PosPrinterService.prototype, {
|
||||
lineElements = getAll('tbody tr');
|
||||
}
|
||||
|
||||
console.log('[BluetoothPrint] Found', lineElements.length, 'line elements');
|
||||
|
||||
const lines = lineElements.map((line, index) => {
|
||||
console.log(`[BluetoothPrint] Processing line ${index}:`, line.outerHTML.substring(0, 200));
|
||||
|
||||
const productName = line.querySelector('.product-name, td:first-child, .pos-receipt-left-align')?.textContent.trim() || '';
|
||||
const qtyText = line.querySelector('.qty, .quantity')?.textContent.trim() || '1';
|
||||
const priceText = line.querySelector('.price, .price-unit')?.textContent.trim() || '0';
|
||||
const totalText = line.querySelector('.price-total, .total, td:last-child, .pos-receipt-right-align')?.textContent.trim() || '0';
|
||||
|
||||
console.log(`[BluetoothPrint] Line ${index} raw data:`, { productName, qtyText, priceText, totalText });
|
||||
|
||||
|
||||
// Parse numbers (remove currency symbols and commas)
|
||||
const parseNumber = (str) => {
|
||||
@ -620,16 +572,15 @@ patch(PosPrinterService.prototype, {
|
||||
total: parseNumber(totalText)
|
||||
};
|
||||
|
||||
console.log(`[BluetoothPrint] Line ${index} parsed:`, parsedLine);
|
||||
|
||||
|
||||
return parsedLine;
|
||||
}).filter(line => line.productName); // Filter out empty lines
|
||||
|
||||
console.log('[BluetoothPrint] Parsed', lines.length, 'lines from HTML');
|
||||
console.log('[BluetoothPrint] All lines:', JSON.stringify(lines, null, 2));
|
||||
|
||||
|
||||
// Parse totals
|
||||
console.log('[BluetoothPrint] Parsing totals...');
|
||||
|
||||
const totals = {
|
||||
subtotal: this._parseAmount(getText('.pos-receipt-subtotal, .subtotal')),
|
||||
tax: this._parseAmount(getText('.pos-receipt-tax, .tax')),
|
||||
@ -637,14 +588,12 @@ patch(PosPrinterService.prototype, {
|
||||
total: this._parseAmount(getText('.pos-receipt-total, .total, .amount-total'))
|
||||
};
|
||||
|
||||
console.log('[BluetoothPrint] Parsed totals:', totals);
|
||||
|
||||
|
||||
// If totals not found in specific elements, calculate from lines
|
||||
if (totals.total === 0 && lines.length > 0) {
|
||||
console.log('[BluetoothPrint] Total is 0, calculating from lines...');
|
||||
totals.total = lines.reduce((sum, line) => sum + line.total, 0);
|
||||
totals.subtotal = totals.total;
|
||||
console.log('[BluetoothPrint] Calculated totals:', totals);
|
||||
}
|
||||
|
||||
// Parse payment info
|
||||
@ -669,7 +618,7 @@ patch(PosPrinterService.prototype, {
|
||||
footerData
|
||||
};
|
||||
|
||||
console.log('[BluetoothPrint] Parsed receipt data from HTML:', receiptData);
|
||||
|
||||
return receiptData;
|
||||
},
|
||||
|
||||
@ -695,8 +644,6 @@ patch(PosPrinterService.prototype, {
|
||||
* @returns {Object} Structured receipt data
|
||||
*/
|
||||
_parseReceiptDataFromPos(pos) {
|
||||
console.log('[BluetoothPrint] _parseReceiptDataFromPos called');
|
||||
console.log('[BluetoothPrint] POS object keys:', Object.keys(pos));
|
||||
|
||||
// In Odoo 18, get current order from POS
|
||||
// Try multiple ways to access the order
|
||||
@ -704,17 +651,13 @@ patch(PosPrinterService.prototype, {
|
||||
|
||||
if (typeof pos.get_order === 'function') {
|
||||
order = pos.get_order();
|
||||
console.log('[BluetoothPrint] Got order via get_order()');
|
||||
} else if (pos.selectedOrder) {
|
||||
order = pos.selectedOrder;
|
||||
console.log('[BluetoothPrint] Got order via selectedOrder');
|
||||
} else if (pos.orders && pos.orders.length > 0) {
|
||||
order = pos.orders[pos.orders.length - 1];
|
||||
console.log('[BluetoothPrint] Got order via orders array');
|
||||
}
|
||||
|
||||
console.log('[BluetoothPrint] Order:', order);
|
||||
console.log('[BluetoothPrint] Order keys:', order ? Object.keys(order) : 'null');
|
||||
|
||||
|
||||
if (!order) {
|
||||
throw new Error('No active order found');
|
||||
@ -722,7 +665,6 @@ patch(PosPrinterService.prototype, {
|
||||
|
||||
// Get company info
|
||||
const company = pos.company || {};
|
||||
console.log('[BluetoothPrint] Company:', company);
|
||||
|
||||
// Get cashier info - try multiple ways
|
||||
let cashierName = '';
|
||||
@ -733,7 +675,7 @@ patch(PosPrinterService.prototype, {
|
||||
} else if (pos.user) {
|
||||
cashierName = pos.user.name || '';
|
||||
}
|
||||
console.log('[BluetoothPrint] Cashier name:', cashierName);
|
||||
|
||||
|
||||
// Get customer info - try multiple ways
|
||||
let customerName = null;
|
||||
@ -744,7 +686,7 @@ patch(PosPrinterService.prototype, {
|
||||
} else if (order.partner_id) {
|
||||
customerName = order.partner_id[1] || null;
|
||||
}
|
||||
console.log('[BluetoothPrint] Customer name:', customerName);
|
||||
|
||||
|
||||
// Build receipt data structure
|
||||
const receiptData = {
|
||||
@ -769,7 +711,7 @@ patch(PosPrinterService.prototype, {
|
||||
}
|
||||
};
|
||||
|
||||
console.log('[BluetoothPrint] Parsed receipt data:', receiptData);
|
||||
|
||||
return receiptData;
|
||||
},
|
||||
|
||||
|
||||
@ -36,7 +36,7 @@ patch(PosStore.prototype, {
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async _initializeBluetoothPrinter() {
|
||||
console.log('Initializing bluetooth printer for POS session...');
|
||||
|
||||
|
||||
try {
|
||||
const notificationService = this.env?.services?.notification || null;
|
||||
@ -59,12 +59,12 @@ patch(PosStore.prototype, {
|
||||
|
||||
if (!config) {
|
||||
// No printer configured for this device
|
||||
console.log('No bluetooth printer configured for this device');
|
||||
// console.log('No bluetooth printer configured for this device');
|
||||
this._promptPrinterConfiguration();
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('Found printer configuration:', config.deviceName);
|
||||
// console.log('Found printer configuration:', config.deviceName);
|
||||
|
||||
// Enable auto-reconnect based on saved settings
|
||||
const autoReconnect = config.settings?.autoReconnect !== false;
|
||||
@ -94,7 +94,7 @@ patch(PosStore.prototype, {
|
||||
const { bluetoothManager } = services;
|
||||
|
||||
try {
|
||||
console.log('Attempting to connect to printer:', config.deviceName);
|
||||
// console.log('Attempting to connect to printer:', config.deviceName);
|
||||
|
||||
// Try to get the previously paired device
|
||||
const devices = await navigator.bluetooth.getDevices();
|
||||
@ -116,7 +116,7 @@ patch(PosStore.prototype, {
|
||||
// Connect to the printer
|
||||
await bluetoothManager.connectToPrinter(device);
|
||||
|
||||
console.log('Successfully connected to bluetooth printer');
|
||||
// console.log('Successfully connected to bluetooth printer');
|
||||
// Success notification is now handled by error service in bluetooth manager
|
||||
|
||||
} catch (error) {
|
||||
@ -172,7 +172,7 @@ patch(PosStore.prototype, {
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async _cleanupBluetoothPrinter() {
|
||||
console.log('Cleaning up bluetooth printer connection...');
|
||||
// console.log('Cleaning up bluetooth printer connection...');
|
||||
|
||||
try {
|
||||
const services = getBluetoothPrintingServices();
|
||||
@ -182,9 +182,9 @@ patch(PosStore.prototype, {
|
||||
const status = bluetoothManager.getConnectionStatus();
|
||||
|
||||
if (status === 'connected' || status === 'connecting') {
|
||||
console.log('Disconnecting bluetooth printer...');
|
||||
// console.log('Disconnecting bluetooth printer...');
|
||||
await bluetoothManager.disconnect();
|
||||
console.log('Bluetooth printer disconnected');
|
||||
// console.log('Bluetooth printer disconnected');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error cleaning up bluetooth printer:', error);
|
||||
@ -258,7 +258,7 @@ patch(PosStore.prototype, {
|
||||
sticky: false
|
||||
});
|
||||
} else {
|
||||
console.log(`[${type.toUpperCase()}] ${message}`);
|
||||
// console.log(`[${type.toUpperCase()}] ${message}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@ -127,6 +127,21 @@
|
||||
</small>
|
||||
</div>
|
||||
|
||||
<!-- Printer DPI -->
|
||||
<div class="form-group">
|
||||
<label for="dpi">Printer Resolution (DPI)</label>
|
||||
<select id="dpi"
|
||||
class="form-control"
|
||||
t-model="state.dpi"
|
||||
t-on-change="onDpiChange">
|
||||
<option value="203">203 DPI (Standard)</option>
|
||||
<option value="304">304 DPI (High Resolution)</option>
|
||||
</select>
|
||||
<small class="form-text text-muted">
|
||||
Select your printer's resolution. Most thermal printers use 203 DPI. Use 304 DPI for higher quality printers.
|
||||
</small>
|
||||
</div>
|
||||
|
||||
<!-- Auto Reconnect -->
|
||||
<div class="form-group form-check">
|
||||
<input type="checkbox"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user