Automatically print a webpage to pdf - javascript

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.

Related

Is there any way to open file in its default application using JavaScript?

I want to open a file in its Windows default application.
Actually, the scenario is as below:
A file is saved in Database and it is shown as a preview in CKEditor
When I double click on that file, it should open in its default application. The file can be a Word or Excel or PDF file
Then the user will edit the file in that default application
And when the user saves that file or closes the document, it should be updated on the server also it should refresh the file preview in CKEditor
I just want to know if there is any way to open the file in its default application.
Things I have tried:
I tried to use the ActiveX object to open the file, but it is not supported in Chrome and Firefox. And anyway, many forums are saying that it is the end of ActiveX era.
Tried to find if there is any CKEditor plugin, but did not find any.
Thank you in advance for your answer.
Short answer: no. Can you imagine the security nightmare if JavaScript was allowed to execute arbitrary binary files on a user's machine?
Your solution is going to be to find a JavaScript library that allows for handling those files, similar to how PDF.js was made for PDFs in-browser.

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

find pdf text in IE9 browser using selenium

In our application while clicking the link it will open the new window with secure pdf file. We want to validate that using selenium ruby. but We unable to validate this in IE9 [because there is no html/dom element]
We can perform this validation using firefox and chrome browser because there html/DOM element present for that pdf page.
Is another way to validate pdf text from browser URL?
I'm not sure this is possible. Chrome and Firefox, do, indeed render more that is actually there, usually for more powerful embedding, etc.
I can offer a solution however,
Instead of navigating to the PDF, you can validate the href of the link.
here is some pseudo-code from Java on how to accomplish the task...
String href = driver.findElement(By.cssSelector("a[href$='.pdf']")).getAttribute("href");
assertTrue(href.contains("FileName.pdf"));
From these two statements, we'll know two things:
The link is indeed taking you to a PDF file.
The PDF file, is called FileName
If the test fails in any way, that means that the link doesn't exist, or points to the wrong pdf.

Save HTML5 page as PDF

On click of a button, without doing a round trip to the server, can we save a HTML5 page on clients machine as PDF.
Check out PDF.js. This lib can create pdf in the browser (or serverside on node.js).
Using chrome you can print the page to a pdf file.
You need something like this, maybe server-side you can start a browser and print the content to a pdf file and send this to the client.
Disclaimer: i work for ByteScout
If you have simple HTML formatting and want to generate PDF on client side and if you have non-commercial project, check BytescoutPDF.js - it supports simple HTML formatting for text (font name, size, color) plus simple drawings and images, should be enough for simple reports

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