views:

2949

answers:

5

I'm trying to convert XML data into PDF files from a web page and I was hoping I could do this entirely within Javascript. I need to be able to draw text, images and simple shapes. I would love to be able to do this entirely in the browser.

+1  A: 

This is not possible without a plug-in in the browser or some server side coding.

Daniel A. White
+1  A: 

There's already an Actionscript 3.0 open source library that can generate PDF's 100% client side.

http://alivepdf.bytearray.org/

In theory, it should be possible to do it in Javascript too, I think, but it seems rather complex.

If requiring flash is acceptable, you could probably write some glue AS code to take data from JS (sending it as JSON, for instance), and use the library to generate the PDF.

Juan Pablo Califano
Note, that this will require flash 9/10 and is not able to be ported to javascript.
Daniel A. White
I think it should be possible to generate a PDF in memory using JS. The lack of certain language / environment features such as a ByteArray class means more work on your side, but it should be possible. Whether that's practical or not, is a different thing...
Juan Pablo Califano
+2  A: 

The bigger question here is are you not using a server-side technology? I really can't imagine a situation where it would be more feasible to implement a javascript implementation than just doing it on the server-side. Doing this in javascript would take a lot of work, would be far from perfect, and not very fast on older machines/browsers.

On the server, however, there's numerous open-source libraries and programs that can do the heavy lifting for you and cut down your development time. Like ghostscript for example.

Paul
I'm just trying to get away with HTML+Javascript as I don't have control over the system I deploy on. If doing this through JS is a poor idea, as it is seeming to be, I will just have to go server-side like I wanted to in the first place.
amoeba
A: 

Even if you could generate the PDF in-memory in JavaScript, you would still have the issue of how to transfer that data to the user. It's hard for JavaScript to just push a file at the user.

To get the file to the user, you would want to do a server submit in order to get the browser to bring up the save dialog.

With that said, it really isn't too hard to generate PDFs. Just read the spec.

Frank Krueger
+10  A: 

I've just written a library called jsPDF which generates PDFs using Javascript alone. It's still very young, and I'll be adding features and bug fixes soon. Also got a few ideas for workarounds in browsers that do not support Data URIs. It's licensed under a liberal MIT license.

I came across this question before I started writing it and thought I'd come back and let you know :)

Generate PDFs in Javascript

James Hall
Did I partially inspire jsPDF? I learned about jsPDF the day you announced it. Looking great so far.I ended up going with Prawn as it's easily the best PDF generation library out there for my requirements. I would still love to do all of my work in-browser as I'm not positive I'll get Ruby on the system I'm deploying to.I'm watching jsPDF very closely. I would love to help out but my time is very limited. That might change in the future.
amoeba
You did inspire it somewhat :), I was looking around on the internet to see if it already existed and saw that some people would find it useful. Let me know if you'd like to help out. I'm @MrRio on twitter.
James Hall