Rails 3/JavaScript: Print PDFs to default printer - javascript

I have an app that generates PDFs and I want it to be able to print the PDF to the client default printer. How can I accomplish that?
Thanks
Edit: Javascript link could also work.

You could embed javascript in your pdf.
http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/Acro6JSGuide.pdf
So when you generate the PDF, use this.print() when the document is opened.

Rails only sends the PDF to the browser, and the browser/user handles printing. You really can't control what printer it gets sent to...

Related

Directly printing PDF from PHP or JS

So, I have this application in Symfony, I use a printer for some stickers, which is fine using JS window.print(), but now, I need to print PDFs and I cannot ask the user to do anything.
Is it possible to directly send the PDF to the printer either from PHP or JS?
I can setup a local server which will be connected on the same network with the printer.
I cannot find nothing about directly printing except some PHP 4.0.4 version extension.
I'm using Symfony 4 with PHP 7.3 and JS for the front end stuff.
Can you please advise if there is a library for PHP or I can skip the print button on window.print() in JS.
Thanks in advance
Directly this shouldn´t be possible, but what if your Printer has got a smb-hotfolder functionality? Then you could write out files into this hotfolder. Regards

Javascript to print with wifi printer

I´m making a simple webapp, I want that when it is open in mobile phones print some data using the user´s wifi printer which most of cases will be an Epson L355.
Can anybody give me a clue or sample to achieve this?
Thank you all.
You won't be able to do this because you don't have access to the user's printer. Instead, you can just generate a pdf and redirect user on it.
The user will then be able to download / print the pdf via his inline PDF viewer.
You can do it by following this :
How to force files to open in browser instead of download (pdf)?

Download the PDF generated by window.print()

Is there any JS solution to download the PDF generated by window.print() ? I need not to open the print window, but simply download the PDF.
I have tried other plugins but the PDF generated by window.print() is the exact thing I needed.
Any ideas how to achieve the same?
i think window.print() function gives pop-up to print your html page..noting more, one alternative solution is you can use jsPDF library to generate pdf.
https://parall.ax/products/jspdf
window.print() invokes the print dialog or printing process of the browser. Depending on the device, it either physically print a paper or write the output to a PDF printer.
Both these actions happen in the client side and the printout or the PDF is already with the client as of the end of printing.
There is nothing to be "downloaded" as a result of this.
-
And to select the PDF printer directly without seeing the print dialog, mostly a user action is needed. JS has no method to replace this user action.
--
There are some libraries which can turn your web page in to a PDF and let you download it. But none of them are as perfect as the browser default printing.
There are some server side solutions available. Those can generate the PDF with 99.9% perfection and send the result to the front end for you to downlaod.
Please see DOMPDF

Automatically print a webpage to pdf

While using browsers like Chrome and Firefox, there is an option to print the page to PDF (that is saving the webpage as a PDF file).
Print this page
This code opens up the print page from where the user should manually choose print to PDF. Can this be done automatically? If so please help.
You can not print page to pdf with Javascript. You have to use server side script to print page to pdf. In PHP, you can use 3rd party PDF library like http://www.tcpdf.org .
There is not much available to print to PDF in Javascript.
The most supported option is indeed to generate it server-side using a library such as tcpdf (as shown by Thein Hla Maw).
I don't think Mark's solution is the correct one, since it requires the users to have some software installed.
For pure javascript PDF generation, there is this.
NB : thanks SO :-)
Yes... provided that your users have a few things installed.
They need a PDF print driver, such as PrimoPDF
If you want to automatically print to this driver, they need to have a plugin installed, such as JS Print Setup (for Firefox) or MeadCo ScriptX for IE
Then, using the API of said plugin, you can choose which driver to print to. You can even skip the print dialog.
A better option, however, would be the generate the PDF server-side, stream it to them, and then use JavaScript inside the PDF to automatically open the print dialog as soon as they view it. This way they don't need any plugins.

Need a print button in a browser that will print a PDF file

I need to set up a print button in HTML that will print a certain PDF file, hopefully without having to download the file first. I've tried a few things but some only work in IE. and some require downloading the file as an embedded object which also is not acceptable. Embedding javascript in the PDF sounds the most promising, but I'd rather avoid that if possible for other reasons.
Any other ideas?
Unfortunately it really can't be done... PDFs are not handled naively by any browser, and as is such they can't be printed using the browser's print dialog.
Your users will have to download the pdf and print it with whatever PDF software they use (Even if it's just a plugin for the browser) unless you convert it to boring old html or an image or something.
You cannot print something without the user downloading it first.
The printer is located on the user end of the connection. The PDF is, at first, located on the server end.
The PDF must thus travel across the webs to reach the user end before the printer will know which ink to put on paper. In other words, it must be downloaded.
I think the best you can do is download the PDF into an iframe element, and advise the user to click the "print" button in there. To my knowledge, no tighter integration than that is possible. The PDF viewer's print button can not be accessed via JavaScript.

Categories