force window.print() to download a PDF - javascript

I made a feature for my website to let my users download the content of the site using these instructions: Printing with Javascript || Mozilla Dev. Basically the user is printing a new formated site stored in a hidden iframe. Now I want to let the user download that same PDF by clicking a button. Is there any easy way? I'm very happy with the result of window.print().
I've taken a look a jsPDF but it doesn't seem to be rendering my CSS correctly. Aditinally this library is pretty messy and hardly documented, I had to digg into the code, and I can't get it to work properly.

There's no way to do it reliably client-side. You could use PhantomJS or similar server-side to handle PDF generation.
If it were me, I'd just let the users do it themselves. Chrome, for example, has a virtual printer driver which exports as a PDF. For everyone else, there are print drivers available that do the same thing, such as CutePDF.

Related

Saving web page as pdf looking exactly the way it looks in browser

I have a web page that is dynamically generated by php. Sometimes I need to save that page to share processed data. I usually save it in Opera browser and it works perfectly fine. However some people may not have Opera or virtual printer installed. Is there a simple way to save page as pdf by JavaScript? Or maybe on the server side by PHP? I've been trying jsPDF, but it is terrible. It doesn't see fonts, it doesn't work with utf-8, it doesn't display css styles properly and generally you need to re-create that page from the scratch if you want decent result. I just need magical button Save that will do the same thing Opera does. Does such thing exist? If yes, where can I find it? If not, why?
You could use Phantom JS for that. http://phantomjs.org/screen-capture.html

Makes PDF readonly

I want to display PDF in Read-only format so website visitors can read PDF but not allowing them to download or print or any other operation.
Anybody have such reference code?
It is possible to disable the printing option in a PDF, this can be done by setting a password on the printing funtion and ofcourse not sharing the password.
If you google this you will find how to do this. This Website might already be a good start.
Note: you will need Acrobat Pro, wich is not free.
Keep in mind, like #Stephen already mentioned, people can make a screenshot or something similar and print it anyway.
That's not possible, and not very practical as well:
If you can view it in your browser, you already have downloaded the file.
If you have it in your browser, there's nothing that can stop you from printing a screen capture of the file, so even while it is possible to create a PDF that isn't printable, it would be easily defeated.

How can I simulate "CTRL +S" keypress in Javascript

I have a page with HTML/JavaScript code I want to export to an HTM file when the user presses the export button on the page. I basically just need to find a way to trigger Ctrl+S to execute the Save Page dialog window. I have searched all over and can't seem to find any thing for this that allows JavaScript to simulate that key press sequence.
TL:DR - Does anyone know how to simulate CTRL+S key press in JavaScript/jQuery
Use the saveDocument() method. Docs for it here.
Edit: That only works in Firefox.
I don't think Javascript can do that. There is something for printing but not for saving.
What you can do you create a hint for the browser that the file is an attachment.
You need to send some HTTP headers. You can for example do that with PHP:
header('Content-disposition: attachment');
Maybe .htaccess works also if you don't want to use PHP. You can look that up.
If you want the browser to save the user's page preserving changes in the DOM, this might be beyond the scope of JavaScript, which aims to provide interaction with the page itself, not the environment it's working in.
On some devices this might even be inapplicable - saving pages in Android browsers it not that straightforward and not always possible.
Still, if you're looking just for a working solution for several desktop browsers, you could look at TiddlyWiki, which is a kind of a "local wiki", content on which are kept client-side and saved with the page. Saving is implemented in Java (not JavaScript!) applet distributed with the page. Kind of a web-based browser-based application.

In-browser conversion of MS Word document to PDF

I would like to implement an in-browser Microsoft Word document merge feature that will convert the merged document into PDF and offer it to the user for download. I would like to this process to be supported in Google Chrome and Firefox. Here is how I would like it to work:
Client-side JavaScript obtains the Word template document in docx format, either from a server, or by asking the user for a file upload (which it can then read using the FileReader API)
The JavaScript uses its local data structures (e.g., data lists it has obtained via Ajax) to expand the template into a document. It can do this either directly, by unzipping the docx file and processing its contents, or using DOCx.js. The template expansion is just a matter of substituting template variables with values obtained from the local data structures.
The JavaScript then converts the expanded template into PDF.
The JavaScript offers the PDF file to the user for download, e.g., using Downloadify.
The difficulty I am having is in step 3. My understanding (based on all the Googling I have done so far) is that I have the following options:
Require that the local machine is a Windows machine, and invoke Word on it, to convert to PDF. This can be done using a little bit of scripting using WScript.shell, and it looks doable with Internet Explorer. But based on what I have read, it doesn't look like I can call WScript.shell from within either Chrome or Firefox, because of their security constraints.
I am open to trying Silverlight to do the conversion, but I have not found enough documentation on how to do this. Ideally, if I used Silverlight, I would like to write the Silverlight code in JavaScript, because (a) I don't know much CSharp, and (b) I think it would be much easier in JavaScript.
Create a web service that will convert a given docx file to a pdf file, and invoke that service via Ajax. I would rather not do this, if possible, for a few reasons: (a) I tried using docx4java (I am a reasonably skilled Java programmer) but the conversion process is far too slow, and it does not preserve document content very well; and (b) I would like to avoid a call out to the network, to avoid security issues. It does seem possible to write a little service on a Windows server for doing the conversion, and if there is no other good option, I might go that route.
If I have been unclear about anything, please let me know. I would appreciate your ideas and feedback.
I love command line tools.
Load the doc to your server and use LibreOffice to convert it to PDF via the command line
soffice.exe --headless --convert-to pdf --outdir E:\Docs\Out E:\Docs\In\a.doc
You can display a progress bar to the user and when complete give them the option to download the doc.
More info on LibreOffice's command line parameters go here
Done.
Old old question now, but for anyone who stumbles across this, web assembly (wasm) now makes this sort of approach possible.
We've just released https://www.npmjs.com/package/#nativedocuments/docx-wasm which can perform the conversion locally.

Adobe Reader plugin interaction through Javascript

I'm displaying PDF file that are generated on-the-fly within a ASP page. The PDF generation (and download to the client) can take some time and I'd like to provide the user with some feedback (a loading message or something).
AFAIK there's no way to know when the PDF is viewed because the DOM events get triggered when the Adobe Reader plugin gets loaded (even though it's not displaying anything yet).
I noticed that there's a Javascript API for the plugin object that I could potentially use. I notivced a LoadFile method on it but unfortunately it doesn't seem to do much.
Adobe's documentation is really useless. It talks of Javascript only as a plug-in writting language or as OLE interaction (and poorly).
Is there any documentation for the API and is it possible to know when the PDF has been loaded? It would be perfect if I could pass on a PDF stream to a PDF viewer of some sort.
I've wanted to the same, found a page that may help you out.
http://www.adobe.com/devnet/acrobat/javascript.html

Categories