Comments (9)
i got this error : 'document is not defined' from encoder. i run on react native android real device
If anyone has a solution, please tell me.
const encoder = new EscPosEncoder();
const canvas = canvasRef.current;
canvas.width = 240;
canvas.height = 241;
let img = new CanvasImage(canvas,240,240);
img.src = 'https://sportshub.cbsistatic.com/i/2021/04/09/9df74632-fde2-421e-bc6f-d4bf631bf8e5/one-piece-trafalgar-law-ano-anime-1246430.jpg';
img.addEventListener('load', () => {
let result = encoder.initialize().image(img, 240, 240, 'atkinson').encode();
});
from escposencoder.
The example for images is intended for use in the browser, where Image is one of the standard classes. With Node you can use node-canvas. Something like this:
const { createCanvas, loadImage } = require('canvas');
loadImage('image.jpg').then((img) => {
let result = encoder
.image(img, 300, 300, 'atkinson')
.encode()
})
from escposencoder.
The above example is not working. (TypeError: Image or Canvas expected)
Even i have tried to use canvas and image both are not working
from escposencoder.
This works for me:
let EscPosEncoder = require('esc-pos-encoder');
let Canvas = require('canvas'), Image = Canvas.Image;
let encoder = new EscPosEncoder();
let img = new Image();
img.onload = () => {
let result = encoder
.initialize()
.image(img, 16, 16, 'atkinson')
.encode();
};
img.src= 'image.jpg';
from escposencoder.
it's throwing the same error
TypeError: Image or Canvas expected
at EscPosEncoder.image (D:\public_html\ng-nexo-print-server\node_modules\esc-pos-encoder\src\esc-pos-encoder.js:407:17)
at img.onload (D:\public_html\ng-nexo-print-server\main.js:21:10)
at Image.src (D:\public_html\ng-nexo-print-server\node_modules\canvas\lib\image.js:30:17)
at Object.<anonymous> (D:\public_html\ng-nexo-print-server\main.js:38:9)
at Object.<anonymous> (D:\public_html\ng-nexo-print-server\main.js:117:3)
at Module._compile (module.js:642:30)
at Object.Module._extensions..js (module.js:653:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:504:12)
at Function.Module._load (module.js:496:3)
from escposencoder.
The last example as is works on my machine 😇
In any case, the exception is thrown by node-canvas when the drawImage() function is called on the context of the canvas that is created internally by EscPosEncoder.
The reason you get this error is that the first parameter of the image() function is not a Canvas or Image object. So something is wrong with the way you pass the image into the image() function.
from escposencoder.
I also had the same issue but i fixed it by requiring canvas-browserify instead of canvas. I checked the esc-pos-encoder code, it's using canvas-browserify instead of canvas. This issue seems to be a limitation of native addons (two different builds of node-canvas don't produce compatible objects). Its important to make sure your JS code is using the same node-canvas everywhere.
I hope this fixes the issue for others.😊
from escposencoder.
This works for me:
let EscPosEncoder = require('esc-pos-encoder'); let Canvas = require('canvas'), Image = Canvas.Image; let encoder = new EscPosEncoder(); let img = new Image(); img.onload = () => { let result = encoder .initialize() .image(img, 16, 16, 'atkinson') .encode(); }; img.src= 'image.jpg';
Hi @NielsLeenheer ,
Seems it's very difficult to get the img object or canvas in React Native. If use your above code, then node-canvas will say "Image() is not a constructor", and if use canvas, then there will be 'document is not defined' from the encoder. Could you make it more clear on how to write the code to work with both node-canvas and your encoder? Thanks,
from escposencoder.
This works for me:
let EscPosEncoder = require('esc-pos-encoder'); let Canvas = require('canvas'), Image = Canvas.Image; let encoder = new EscPosEncoder(); let img = new Image(); img.onload = () => { let result = encoder .initialize() .image(img, 16, 16, 'atkinson') .encode(); }; img.src= 'image.jpg';Hi @NielsLeenheer ,
Seems it's very difficult to get the img object or canvas in React Native. If use your above code, then node-canvas will say "Image() is not a constructor", and if use canvas, then there will be 'document is not defined' from the encoder. Could you make it more clear on how to write the code to work with both node-canvas and your encoder? Thanks,
I have exactly the same problems, is there any one managed to make it work on react-native? great thanks
from escposencoder.
Related Issues (20)
- CP874 Mapping Error
- Compatible with JSPrintManager?
- How to add codepage GB18030?
- QRCode not working as expected HOT 2
- New npm package HOT 7
- Cannot install on M1 Mac (ARM64) HOT 2
- Add display text parameter when printing barcode
- Publish a new version with auto encode and with the table functionality HOT 2
- Print image issue ( incomplete printing / encode error ) HOT 6
- Pure Browser JS Example HOT 3
- Failed to execute 'getImageData' HOT 1
- How To Insert Data Array in to Encoder? HOT 1
- cutting HOT 3
- How to print a horizontal line and font size increase
- Hello, how can I print Chinese HOT 1
- esc-pos-encoder error with NODE_MODULE_VERSION HOT 1
- I'm sorry but
- Add multiline support for "newLine" command
- Table with border and table auto layout
- Image printed with empty spaces in between HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from escposencoder.