Unable to load image form UInt8Array. I get this error: TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes. Received Uint8Array(6191)
adnanlah opened this issue ยท 2 comments
Hello,
I tried to load an image from a base64 data this way:
const buffer = Buffer.from(logo, 'base64')
const uint8Array = new Uint8Array(buffer)
const image = await Image.load(uint8Array)
printer = await printer.image(
image,
's8' // changing with image
)
but I get this error:
TypeError [ERR_INVALID_ARG_VALUE]: The argument 'path' must be a string or Uint8Array without null bytes. Received Uint8Array(6191)
Hey @adnanlah, back at it with the binary ballet, I see! Hope you're doing well.
Based on the error message you're receiving, it seems like the Uint8Array
you're passing to Image.load()
might contain null bytes, which are not allowed.
The Image.load()
method in the @node-escpos/core
library does support loading images from a Uint8Array
. The method signature for load
accepts a parameter that can be either a string or a Uint8Array
, as indicated by its type annotation url: string | Uint8Array
. This means you can pass a Uint8Array
containing image data directly to the load
method, in addition to passing a file path as a string.
static load(url: string | Uint8Array, type: ImageMimeType | null = null): Promise<Image>
You can find this in the packages/core/src/image.ts file.
However, the error message suggests that the Uint8Array
you're passing might contain null bytes, which are not allowed. You might want to check the data you're passing to Image.load()
to ensure it doesn't contain any null bytes.
If you're sure that your Uint8Array
doesn't contain any null bytes, it would be helpful if you could provide a bit more context or a more complete code sample. This would help in identifying any potential issues with how you're using the Image.load()
method or the @node-escpos/core
library.
Sources
About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a ๐ if this is helpful and ๐ if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.
I tried this method:
import base from 'base64-arraybuffer'
export const base64ToArrayBuffer = (base64: string): Uint8Array => {
const arrayBuffer = base.decode(base64)
return new Uint8Array(arrayBuffer)
}
and this method:
export const base64ToArrayBuffer = (base64: string): Uint8Array => {
const binaryString = atob(base64)
const bytes = new Uint8Array(binaryString.length)
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i)
}
return bytes
}
And still both methods gave me the same error.