Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Cannot parse (FILE) as an image #164

Closed
shaneguna opened this issue Feb 9, 2017 · 18 comments
Closed

Error: Cannot parse (FILE) as an image #164

shaneguna opened this issue Feb 9, 2017 · 18 comments

Comments

@shaneguna
Copy link

shaneguna commented Feb 9, 2017

Hi guys, i'm experiencing an issue while printing images or pdf.

Im currently using qztray to print a receipt after placing an order. Im running on a windows machine.

Pretty much stuck right here, when running the demo/sample page im able to print images with no issues.

This is my js code.

`
function print(printOrderDetails) {

var orderDetails = JSON.parse(printOrderDetails);
var referenceCode = orderDetails[0].reference_no;


var printer = "HWASUNG HMK-825";
var options = {size: {width: 60, height: 150}, units: "mm",scaleContent: true};
var config = qz.configs.create(printer, options);
var printData = [
  
    {type: 'image', data: '/load/ReceiptLogo.png'},
    '\nPrinted using qz-tray plugin.\n\n\n\n\n\n'
];


// return the promise so we can chain more .then().then().catch(), etc.
return qz.print(config, printData).catch(function (e) {
    //return qz.print(config, ['\n\n\n\n\n' + barcode + '\n\n\n\n\n']).catch(function (e) {
    console.error(e);
});

}`

Im getting this error once this function is called.

Error: Cannot parse (FILE)http://localhost:1234/load/ReceiptLogo.png as an image at WebSocket._qz.websocket.connection.onmessage (qz-tray.js:271)

To add, i get these logs from qz tray.

[INFO] 2017-02-09 18:13:55,352 @ qz.ws.PrintSocketClient:?
	Connection closed: 1001 - null
[INFO] 2017-02-09 18:13:55,354 @ qz.ws.SocketConnection:?
	Closing all communication channels for An anonymous request
[INFO] 2017-02-09 18:14:04,787 @ qz.ws.PrintSocketClient:?
	Connection opened from /0:0:0:0:0:0:0:1:49725 on socket port 8182
[DEBUG] 2017-02-09 18:14:04,855 @ qz.ws.PrintSocketClient:?
	Message: {"certificate":null,"promise":{},"timestamp":1486635244855,"uid":"nhbqw3"}
[INFO] 2017-02-09 18:14:05,795 @ qz.common.TrayManager:?
	Allowed An anonymous request to connect to QZ
[DEBUG] 2017-02-09 18:14:05,800 @ qz.ws.PrintSocketClient:?
	Message: {"call":"print","promise":{},"params":{"printer":{"name":"HWASUNG HMK-825"},"options":{"colorType":"color","copies":1,"density":0,"duplex":false,"fallbackDensity":600,"interpolation":"bicubic","jobName":null,"margins":0,"orientation":null,"paperThickness":null,"printerTray":null,"rasterize":true,"rotation":0,"scaleContent":true,"size":{"width":60,"height":150},"units":"mm","altPrinting":false,"encoding":null,"endOfDoc":null,"perSpool":1},"data":[{"type":"image","data":"http://localhost:1234/load/ReceiptLogo.png"},"\nPrinted using qz-tray plugin.\n\n\n\n\n\n"]},"timestamp":1486635245797,"uid":"8vzepz"}
[INFO] 2017-02-09 18:14:06,227 @ qz.common.TrayManager:?
	Allowed An anonymous request to print to HWASUNG HMK-825
[TRACE] 2017-02-09 18:14:06,227 @ qz.utils.PrintingUtilities:?
	Waiting for processor, 0/3 already in use
[DEBUG] 2017-02-09 18:14:06,228 @ qz.utils.PrintingUtilities:?
	Using qz.printer.action.PrintImage to print
[DEBUG] 2017-02-09 18:14:06,229 @ qz.printer.PrintServiceMatcher:?
	Searching for PrintService matching HWASUNG HMK-825
[DEBUG] 2017-02-09 18:14:06,230 @ qz.printer.PrintServiceMatcher:?
	Found 6 printers
[DEBUG] 2017-02-09 18:14:06,230 @ qz.printer.PrintServiceMatcher:?
	Found match: HWASUNG HMK-825
[DEBUG] 2017-02-09 18:14:06,230 @ qz.utils.PrintingUtilities:?
	Found Resolution: 20300x20300 dphi
[ERROR] 2017-02-09 18:14:07,242 @ qz.utils.PrintingUtilities:?
	Failed to print
java.lang.UnsupportedOperationException: Cannot parse (FILE)http://localhost:1234/load/ReceiptLogo.png as an image
	at qz.printer.action.PrintImage.parseData(Unknown Source)
	at qz.utils.PrintingUtilities.processPrintRequest(Unknown Source)
	at qz.ws.PrintSocketClient.processMessage(Unknown Source)
	at qz.ws.PrintSocketClient.onMessage(Unknown Source)
	at sun.reflect.GeneratedMethodAccessor10.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.jetty.websocket.common.events.annotated.CallableMethod.call(CallableMethod.java:70)
	at org.eclipse.jetty.websocket.common.events.annotated.OptionalSessionCallableMethod.call(OptionalSessionCallableMethod.java:68)
	at org.eclipse.jetty.websocket.common.events.JettyAnnotatedEventDriver$2.run(JettyAnnotatedEventDriver.java:210)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
	at java.lang.Thread.run(Unknown Source)
Caused by: javax.imageio.IIOException: Can't get input stream from URL!
	at javax.imageio.ImageIO.read(Unknown Source)
	... 13 more
Caused by: java.net.ConnectException: Connection refused: connect
	at java.net.DualStackPlainSocketImpl.connect0(Native Method)
	at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
	at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
	at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
	at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
	at java.net.PlainSocketImpl.connect(Unknown Source)
	at java.net.SocksSocketImpl.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at sun.net.NetworkClient.doConnect(Unknown Source)
	at sun.net.www.http.HttpClient.openServer(Unknown Source)
	at sun.net.www.http.HttpClient.openServer(Unknown Source)
	at sun.net.www.http.HttpClient.<init>(Unknown Source)
	at sun.net.www.http.HttpClient.New(Unknown Source)
	at sun.net.www.http.HttpClient.New(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at java.net.URL.openStream(Unknown Source)
	... 14 more
[TRACE] 2017-02-09 18:14:07,252 @ qz.utils.PrintingUtilities:?
	Returning processor back to pool

Hoping for your assistance. Thank you.

@xewl
Copy link

xewl commented Feb 9, 2017

I have the same problem, and a problem as a result of this one too.

Research told me that the given file url must be accessible by the java applet, eg. without authentication. (Check! but it stil couldn't "parse" it)

Then I tried to turn it into base64 data; which comes in via an ajax-request.
Checking the request log, I receive the correct dataset, but when debugging via console it prepended the current base_url before the base64-string...!?

Something's up with QZ2+ and 1.9 is deprecated by now. This should be solved asap.

@shaneguna
Copy link
Author

Hey Xewl, good to know im not the only one facing this issue. I also crossed out the authentication part since im able to access the files with no issue through a different browser, port or server.

May i know if your files are saved in any framework? Currently developing this on laravel.

@tresf
Copy link
Contributor

tresf commented Feb 9, 2017

var printData = [
  
    {type: 'image', data: '/load/ReceiptLogo.png'},
    '\nPrinted using qz-tray plugin.\n\n\n\n\n\n'
];

@shaneguna can you please clarify something? What I'm reading is that you are appending an image along with raw text, however I do not see a raw image format specified.

Here is an example from our wiki which has the image format specified.

var printData = [
    { type: 'raw', format: 'image', data: '/load/ReceiptLogo.png', options: { language: "escp", dotDensity: 'double' } },
    '\nPrinted using qz-tray plugin.\n\n\n\n\n\n'
];

If that does not work, I would suggest double-checking your image URL and security access, such as opening a new private browsing session (CTRL + SHIFT + N) to http://localhost:1234/load/ReceiptLogo.png and see what type of access an anonymous browser will have.

Then I tried to turn it into base64 data; which comes in via an ajax-request.
Checking the request log, I receive the correct dataset, but when debugging via console it prepended the current base_url before the base64-string...!?

Base64 Images aren't currently compatible with raw. You're right, this is a regression from 1.x versions. We've fixed it for 2.1 but it required an API change, so we decided not to backport it to 2.0 See #70.

@shaneguna
Copy link
Author

Hi @tresf,

Thank you for responding. The issue seems to be with the served port not being accessible by the java applet. I added my application through the vhosts list of the server and it worked. So, the suggestion with regard to the URL access is the reason why the files are not being parsed.

Right now, i'm still facing issues with printing image format as raw type. Its returning same error that its not able to parse, this is not happening for image type. I thought of a workaround to chain the printing by calling another function that will print raw texts unfortunately after calling the function that prints {type:'image', data: '/load/ReceiptLogo.png' ,options: { dotDensity: 'double' }}, but it cuts the paper after completing. Is there a way to avoid cutting before completing the chained function?

Thanks

PS. As for future reference of other laravel dev's, i havent tested if this is the same case in Ubuntu.

@shaneguna
Copy link
Author

Hi @tresf,

Sorry, found a possible solution in the docs. Let me try this out first.

https://qz.io/wiki/2.0-Raw-Printing#chaining-requests

Regards.

@shaneguna
Copy link
Author

shaneguna commented Feb 10, 2017

Hi @tresf,

To update, chaining requests work but the problem is i'm not able to follow my format. Is there a way to override cutting of paper after printing for image types or, if you could aid me with the error faced when printing image as raw this would be better as i will be able to print everything in raw.

 { type: 'raw', format: 'image', data: '/load/ReceiptLogo.png', options: { language: "escp", dotDensity: 'double' } }
Error: Cannot parse (IMAGE)http://localhost/load/ReceiptLogo.png as a raw command at WebSocket._qz.websocket.connection.onmessage (qz-tray.js:271)

Best Regards.

@shaneguna
Copy link
Author

Hi @tresf,

The last mentioned issue seems to be compatibility of the printer to recognize esc/p commands.
I'll be closing the issue since i have proceeded to generating pdf and printing from this instead.

Thank you very much for your time.

@tresf
Copy link
Contributor

tresf commented Feb 10, 2017

{  
   type:'raw',
   format:'image',
   data:'/load/ReceiptLogo.png',
   options: {  
      language:"escp",
      dotDensity:'double'
   }
}

That looks fine. I assume you have it wrapped into an array? [ ].

Is there a way to override cutting of paper after printing for image types or, if you could aid me with the error faced when printing image as raw this would be better as i will be able to print everything in raw.

It shouldn't be cutting automatically, you should be sending a cut command, which a lot of our clients do. (e.g. '\x1B' + '\x69', // cut paper), etc. If it is cutting automatically, that is a driver setting.

https://qz.io/wiki/2.0-Raw-Printing#escp

The last mentioned issue seems to be compatibility of the printer to recognize esc/p commands.

If that is the case, you can try QPCPrint on Windows 7 to see if you can get some valid ESC/P output.

https://qz.io/wiki/Printer-Emulators#raw

@thiagoemanoel1992
Copy link

Hi , how u make to enable the application throught vhosts ? My server its a RHRL 7.

Thanks .

@dogrocker
Copy link

dogrocker commented Aug 22, 2018

Got same error with @shaneguna .

Error: Cannot parse (IMAGE)http://localhost/load/ReceiptLogo.png as a raw command at WebSocket._qz.websocket.connection.onmessage (qz-tray.js:271)

in qztray 2.0.5, 2.0.7

This's my setting


             var rawdata = [
                    '\x1B' + '\x40',          // init
                    '\x1B' + '\x61' + '\x31', // center align
                    {
                        type: 'raw', format: 'image', data: '{{ request.session.logo_receipt }}',
                        options: {language: "ESC/POS", dotDensity: 'double'}
                    }];
            }

Don't know how to fix this.
I try another language language: "ESC/POS", language: "ESCPOS", language: "escp"
Not work at all.

@tresf
Copy link
Contributor

tresf commented Aug 22, 2018

Error: Cannot parse (IMAGE)http://localhost/load/ReceiptLogo.png as a raw command at WebSocket._qz.websocket.connection.onmessage (qz-tray.js:271)

That's just a generic error that's thrown if the ImageWrapper fails. The cause could be many things. I recommend you close QZ Tray and restart it from console to find out what the underlying cause is and address the exact error that appears in the Java backtrace.

@shehzadosama
Copy link

@shaneguna @tresf facing same error:

Uncaught (in promise) Error: Cannot parse (IMAGE)http://localhost:3000/image_sample_bw.png as a raw command
    at WebSocket._qz.websocket.connection.onmessage (:3000/static/js/bundle.js:87654)

Have you guys resolved this issue?

@klabarge
Copy link
Member

klabarge commented Jul 9, 2019

@shaneguna as @tresf mentioned before, this is a generic error. Can you please launch the software from console and share the full stack trace with us?

A code snippet would also assist us.

This could be as simple as an incorrect url for image_sample_bw.png

@shehzadosama
Copy link

@klabarge Below is the code snippet that i am using for image printing:
{ type: 'raw', format: 'image', data: 'image_sample_bw.png', options: { language: 'ESCPOS', dotDensity: 'double' } }

Error stack trace:
[DEBUG] 2019-07-09 15:51:13,880 @ qz.utils.ShellUtilities:? Parsed default density from CUPS EPSON-TM-T20-2: 361dpi [INFO] 2019-07-09 15:51:13,921 @ qz.printer.ImageWrapper:? Loading BufferedImage [ERROR] 2019-07-09 15:51:13,922 @ qz.utils.PrintingUtilities:? Failed to print java.lang.UnsupportedOperationException: Cannot parse (IMAGE)http://localhost:3000/image_sample_bw.png as a raw command at qz.printer.action.PrintRaw.parseData(Unknown Source) at qz.utils.PrintingUtilities.processPrintRequest(Unknown Source) at qz.ws.PrintSocketClient.processMessage(Unknown Source) at qz.ws.PrintSocketClient.onMessage(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.eclipse.jetty.websocket.common.events.annotated.CallableMethod.call(CallableMethod.java:70) at org.eclipse.jetty.websocket.common.events.annotated.OptionalSessionCallableMethod.call(OptionalSessionCallableMethod.java:68) at org.eclipse.jetty.websocket.common.events.JettyAnnotatedEventDriver$2.run(JettyAnnotatedEventDriver.java:210) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.NullPointerException at qz.printer.ImageWrapper.<init>(Unknown Source) at qz.printer.action.PrintRaw.getImageWrapper(Unknown Source) ... 14 more [TRACE] 2019-07-09 15:51:13,928 @ qz.utils.PrintingUtilities:? Returning processor back to pool

Note: Image printing is working fine while using sample.html (From QZ demo) and clicking on Print ESC/POS

@tresf
Copy link
Contributor

tresf commented Jul 9, 2019

{ type: 'raw', format: 'image', data: 'image_sample_bw.png', options: { language: 'ESCPOS', dotDensity: 'double' } }

You're probably running authentication on port :3000. You can use AJAX to fetch and base64 encode the image to circumvent this, or lower the security for the image URL. In a future version, we'll provide an embedded technique for the base64 logic, but at this time, it would be manual to the programmer.

@shehzadosama
Copy link

@tresf Thanks for your kind response. I have used your suggestion and now it's working for me.

Imported image like this:
import * as image from './image_sample_bw.png';

And I used in this way:
{ type: 'raw', format: 'image', flavor: 'base64', data: image, options: { language: "escp", dotDensity: 'double' } }

@tresf
Copy link
Contributor

tresf commented Jul 10, 2019

@shehzadosama wow, I was unaware JS could automagically import a base64 image. Are you using Angular? This is a slick technique, I wish the browser had such a function. :)

@shehzadosama
Copy link

@tresf I am using ReactJS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants