Print.js icon indicating copy to clipboard operation
Print.js copied to clipboard

Can not load image with ETag

Open cyd opened this issue 7 years ago • 9 comments

when print an image, trigger an image url request with "ETag", and then print screen shows nothing.

cyd avatar Mar 28 '19 03:03 cyd

Hey @YanDaChen , Please post the code you used (printJS function with parameters).

Also, please post the browsers you have replicated the problem as well the OS and version. Thx.

crabbly avatar Mar 28 '19 03:03 crabbly

@crabbly Thanks for reply. Browser:chrome 73.0.3683.86(64) Frontend-demo: //use this: https://printjs-4de6.kxcdn.com/print.min.js printJS('http://127.0.0.1:8080/test123', 'image')" backend-demo: ` @Controller public class ImgController {

@RequestMapping("/test123")
public void test(HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest) throws Exception {
    httpServletResponse.setHeader("Access-Control-Allow-Origin", "*");
    String etag = httpServletRequest.getHeader("If-None-Match");
    if(etag != null && etag.length() > 0){
        httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        httpServletResponse.setHeader(HttpHeaders.ETAG, etag);
        return;
    }
    FileInputStream fileInputStream = new FileInputStream(new File("/Users/cyd/Downloads/cat.jpg"));
    httpServletResponse.setHeader(HttpHeaders.ETAG, "AAAAAAAA");
    IOUtils.copyLarge(fileInputStream, httpServletResponse.getOutputStream());
    fileInputStream.close();
}

} ` use etag and browser will resolve 304 response.

result: 11111111 click print, pic show pending, screen is empty: 2222222 click cancel print, show 304: 33333

cyd avatar Mar 28 '19 03:03 cyd

Hey @YanDaChen , thank you for the details.

I'm not sure why you see the "pending" status when printing. The library should preload the images first before firing the print job.

Also, if the image is already displayed in the browser, as long as you have your cache enabled, the print job should use the cached image instead, preventing that call. Looking at your screen shot, I see that you have disabled your browser cache. This will block the browser from serving the library the cached version. When you fire the print job, the library will retrieve the image from the server.

The problem here is that the server is responding 304, informing that the file did not change, and is not retransmitting the image data. Therefore, when printing, there is no data to be printed.

crabbly avatar Mar 29 '19 13:03 crabbly

I don't think there is anything we can do in the library to fix this scenario, where you disabled your browser cache and the server is not sending the image again (pretty much because it is expecting you to use the cache).

crabbly avatar Mar 29 '19 13:03 crabbly

@crabbly Thanks for reply. But even if I use cache,the result is the same. You can test it in your localhost. I think maybe it's due to this: https://stackoverflow.com/questions/12354865/image-onload-event-and-browser-cache

https://github.com/crabbly/Print.js/blob/99040414a322f7f3300a58c107a170f3a588151c/src/js/image.js#L30-L45 Here you can change the order.

Edit: I try it but the result is the same..

cyd avatar Apr 01 '19 08:04 cyd

@YanDaChen, could you update the lib to its latest version and try this again. npm update print-js Some changes were made to support raw html image preload, and it may help you with this etag issue. Let us know how it goes. Thank you.

crabbly avatar May 22 '19 20:05 crabbly

@crabbly I try the latest version, image can show, but the image is shrunk ~OZA`{(RKHB3BDWCN L%1PM

wcccode avatar May 31 '19 07:05 wcccode

@YanDaChen mr @crabbly is right i had the same issue reason was that some times image was loaded complete and mostly partial or some part of it so the reason was that image was not coming from server url till that dialogue shows so how can you render that with the plus point that printjs uses cache of image if it has obtained from the server so what you need to do some how load image before calling the printJS function i did this i used sweetalert with response and loaded that image then and when click yes of alert i send the url to the printjs and now its every time i get the full image hope it will make u understand. Dear @crabbly if you can help me increasing the quality of image using print image print js

image image

@

awais2080 avatar Jun 16 '19 21:06 awais2080

Hey guys, please try using the latest version of the library (npm update). The lib will try to preload the images and then leverage the browser's cache when printing. If the browser cache is turned off, the image will be loaded twice, and for large image files, this could be a problem as the print job will be dispatch before the image is completely available. Hope this helps. Please feel free to reopen this with a fiddle or codepen replicating the issue.

crabbly avatar Sep 09 '20 05:09 crabbly