View Full Version : Pdf generator

Jun 17th, 2009, 01:36 PM
Hi, is it possible to generate pdf file using javascript?

Philip M
Jun 17th, 2009, 01:59 PM
No. JavaScript is purely a client-side language, and unless used with an Ajax call has no capability to read from or write to a file (except a cookie), communicate with the server, access a database or the client's operating system, or alter the default behaviour of the browser.

rnd me
Jun 17th, 2009, 08:04 PM
Hi, is it possible to generate pdf file using javascript?

yes it is, in some browsers.

You can use XSL FO to create PDFs from HTML/XML data.

it would be available to all users if you did it on the server though...

Jun 17th, 2009, 11:12 PM
You can definitely generate PDF with client side JavaScript.

Displaying it as a PDF file to the user may be an issue.

Because PDF is just a file format, if you know how to generate this file format, you can do it with JavaScript.

But because it is a file format separate from HTML, the browser views it is through a URL, or as an embedded object.

Taking the PDF source you've generated and passing it to the embed, or linking it to a URL is what you have to figure out.

You cannot do this via sending the browser to a URL:

window.loaction = 'mypdf_file_url';

mainly because you cannot generate that URL from the client side.

You can however use a data URI: http://en.wikipedia.org/wiki/Data_URI_scheme
Write this to a PDF embed with the PDF source you generated in JavaScript. Most browsers now support it.

You could use window.open() or window.write() to write the data to the window.


You can also embed it in a url, so it is like a normal link. But you generated the actual pdf data with JavaScript.

I'm sure you can find other ways of doing it.

Here is an example of PDF generation in pure JavaScript:

Though this is server side, it will work on the client side with modifications. On the server side, it is easy to display the PDF, or intiate a download, since you can do it through the HTTP Specifications.

On the client side, you cannot initiate a HTTP request to yourself, so you'll have to use other protocols described above.