I am using the last version .25 and if I call the printJS() function, the pdf file will be downloaded and not shown in the print preview ?
Using latest Chrome.
Does it check specific response headers ?! What are the requirements of the respone ?!
Facing similar issue. If filename is sent from the sever, it downloads the file.
Otherwise print preview is opened with some some string and not the pdf file contents.
Am I missing something?
@sadevio The library should print fine in Chrome. It doesn't check the response headers, you should make sure that you are passing a valid url.
Also, please try running npm update print-js. The latest version is 1.0.53 as of today.
@ajay-bal What is the "some string" that you are getting in the print preview?
Also, if the PDF document is on another server, try passing showModal as true to the printJS function. This will preload the PDF before printing, preventing a cross-domain error.
@crabbly I do use the latest version. What is a valid URL for you ? The URL must be valid in my opinion, because the pdf will be downloaded.
Does it has to look like
https://mydomain.com/something/myfile.pdf
Could it be
Sorry. What I meant by valid is to check if the response headers are not telling the browser to download the file.
The library does not force the download of the PDF document.
When you open the document directly, can you view it in the browser or does it asks you to download the file?
Also, take a look at this #219
Ok we do set actually
response.header("Content-Disposition", "attachment; filename=""+attachment.getFileName()+ """);
Let me check if I take it out, if that works then.
This was the issue. If we dont set the header it will work fine.
@crabbly thank you for your help !
Cool. I'm glad to hear :)
Same issue to me
Just pass a downloadable url to print method and then it will download that file instead of printing it.
print({ printable: url, showModal:true, type });
And the backend pseudo code is :
const target = path.join(this.config.baseDir, 'app/public/upload', filename);
ctx.attachment(filename);
ctx.set('Content-Type', 'application/octet-stream');
ctx.body = fs.createReadStream(target);
Am i missing something or should I change the response' header?
Hi @kennylbj,
You have to change the response header.
The browser is downloading the document because your response header is telling it to do so. Which would be the expected behavior.
Thanks @crabbly
Now I serve those files as static resources and everything works fine.
@crabbly Not passing content-disposition as application/pdf did the trick. So if we pass the content-disposition, download happens else normal print preview of pdf document. I have used onPrintDialogClose also successfully.
Thanks a lot @crabbly!
Thanks for the feedback @ajay-bal
I think you meant content-disposition as attachment.
I use Content-Disposition: attachment; filename=my_document_name.pdf whenever I want to force a document download.
However, we can still pass Content-Type: application/pdf to indicate the media type, without forcing the download.
Hi I also need to Download as PDF insted of taking to Print Preview Page
In which Js i need to change those response header?
Pls explain in detail
@crabbly Not passing content-disposition as application/pdf did the trick. So if we pass the content-disposition, download happens else normal print preview of pdf document. I have used onPrintDialogClose also successfully.
Thanks a lot @crabbly!
Hi ajay
Where do I change those response headers?