AbobosSoftware / cordova-plugin-brother-label-printer

Cordova and Capacitor plugin for Bother Label Printers
MIT License
10 stars 16 forks source link

printViaSDK Not Printing #64

Closed Jack-Drake closed 1 month ago

Jack-Drake commented 2 months ago

Thank you for such a great plugin. I have however ran into a problem where the printer is not printing when the printViaSDK function is called.

Context The printer is a Brother QL-820NWB Label Printer. The label printer is connected via wifi and the android tablet is using wifi to connect to the printer. I have successfully setPrinter prior to calling the printViaSDK so I do know that the printer is being communicated with. The issue rises when I use the code below:

The code below converts the text variable to a bitmap using the function renderBitmapFromString. Please let me know if I am using this function wrong? Is the bitmap incorrect?

`const text = "Hello\nWorld!"; const bitmap = renderBitmapFromString(text, 200, 100);

var bitmap_image_index = bitmap.lastIndexOf(","); console.log(bitmap.substring(bitmap_image_index + 1)); window.plugin.brotherPrinter.printViaSDK(bitmap.substring(bitmap_image_index + 1), () => { console.log("printed"); });

const renderBitmapFromString = (string, width, height) => { const canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; const ctx = canvas.getContext('2d');

// Clear the canvas ctx.clearRect(0, 0, canvas.width, canvas.height);

// Set font and text alignment ctx.font = '20px Arial'; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';

// Split the string into lines const lines = string.split('\n');

// Calculate line height const lineHeight = height / lines.length;

// Draw each line lines.forEach((line, index) => { // Calculate Y position for the line const y = (index + 0.5) * lineHeight; // Draw the text ctx.fillText(line, width / 2, y); });

// Convert the canvas to a bitmap image const bitmap = canvas.toDataURL(); return bitmap; } `

Jack-Drake commented 2 months ago

image

I found this error occur during the function call of the printViaSDK

arcadius commented 2 months ago

1) what framework are you using: ionic cordova or ionic capacitor?

2) The screenshot provided does not have enough info. Please provide full error log

3) You need to set the label among others. We have an example at https://github.com/AbobosSoftware/cordova-plugin-brother-label-printer?tab=readme-ov-file#sample-code

Jack-Drake commented 2 months ago

Thank you for your message. I just found the error code being sent back as shown below: image You referenced in your README that the error codes can be found in the Brother's SDK API. Is there a link to access this?

This is the code that I have added in hopes of solving the problem. Unfortunately, the error is still present even after manually setting the paperLabelName and the orientation. What is it that I am doing this here? image

arcadius commented 1 month ago

for ERROR_WRONG_LABEL , you want to try replacing in your code W62 by W62RB and see

Jack-Drake commented 1 month ago

That worked perfect thank you.

My only remaining question is do you recommend a particular way to create a bitmap? I have an issue where running the function with any bitmap results in having a full black page printed. i have many things, but it does not seem to change the outcome of the background color.

arcadius commented 1 month ago

@Jack-Drake about your other question about black page being printed, I am not sure I can help with that as this is specific to your application. You need to experiment with the HTML canvas being drawn

Jack-Drake commented 1 month ago

Do you have any recommended libraries or possible base64 encoded bitmap that i can just test with since till now I havent gotten text to be visible?

arcadius commented 1 month ago

I wish I could help .... but I do not know such libs. Maybe google is your best friend?

Jack-Drake commented 1 month ago

Do you have any sample encoded base64 that you have previously used just for me to test?

arcadius commented 1 month ago

Check https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL

I think you are already converting the canvas to base64

I can see you are dynamically creating the canvas. The simpler way to test this is to have your HTML with an empty canvas element in it Then draw the data in it. This allows you to visually see what you have drawn and be confident about its appearance before you start playing with the label printer.