...

View Full Version : Pdf generator



ira19
06-17-2009, 01:36 PM
Hi, is it possible to generate pdf file using javascript?

Philip M
06-17-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
06-17-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...

digital-ether
06-17-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:

eg:
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.


window.open('data:application/pdf;charset=utf-8,pdf-source-code','_blank','height=300,width=400');

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:
http://apps.jgate.de/platform/source?jspdf

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum