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.
19.1k 8 8 gold badges 60 60 silver badges 83 83 bronze badges asked Apr 12, 2009 at 19:27 4,225 3 3 gold badges 22 22 silver badges 14 14 bronze badgesI'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 :)
Example create a "Hello World" PDF file.
// Default export is a4 paper, portrait, using milimeters for units var doc = new jsPDF() doc.text('Hello world!', 10, 10) doc.save('a4.pdf')
33.9k 12 12 gold badges 72 72 silver badges 104 104 bronze badges
answered Apr 22, 2009 at 19:49
James Hall James Hall
7,665 2 2 gold badges 23 23 silver badges 15 15 bronze badges
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.
Commented May 4, 2009 at 19:40You 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.
Commented May 7, 2009 at 9:22 It's still very much alive. 0.9.0rc2 Released today. Commented Apr 7, 2013 at 22:00@JamesHall, I'd like to thank you for all the work it must be taking to write this, and for MIT licensing it to make the world a better place when you could have commercialized it for your own gain.
Commented Sep 26, 2013 at 14:58 Can downvoters offer us their "better" ideas or solutions? This is an excellent effort. Commented Apr 28, 2015 at 21:20Another javascript library worth mentioning is pdfmake.
The browser support does not appear to be as strong as jsPDF, nor does there seem to be an option for shapes, but the options for formatting text are more advanced then the options currently available in jsPDF.
answered May 7, 2014 at 12:13 18.1k 8 8 gold badges 61 61 silver badges 102 102 bronze badgesThis answer should be upvoted a lot more, pdfmake is so much more robust than jspdf. However just a side note, it is wrapper around PDFKit library.
Commented Apr 19, 2015 at 19:27another side note, jspdf doesn't have utf-8 support and pdfmake has utf-8 support only for default fonts.
Commented Apr 5, 2016 at 7:21 "pdfmake.min.js - 0.1.22 - 9 hours ago" It is not dead, updated this day. Commented Dec 23, 2016 at 15:43From comparing the two playgrounds, to me it seems that jsPDF is more targeted at "graphic" PDF creation, whereas pdfmake is targeted more at generating well-formatted "plain" documents, it seems to have a lot more options for generic text formatting.
Commented Dec 17, 2017 at 10:31 Is this IE friendly? Commented Jun 27, 2019 at 18:33I maintain PDFKit, which also powers pdfmake (already mentioned here). It works in both Node and the browser, and supports a bunch of stuff that other libraries do not:
Check out http://pdfkit.org/ for a full tutorial to see for yourself what PDFKit can do. And for an example of what kinds of documents can be produced, check out the docs as a PDF generated from some Markdown files using PDFKit itself: http://pdfkit.org/docs/guide.pdf.
You can also try it out interactively in the browser here: http://pdfkit.org/demo/browser.html.
answered Feb 5, 2015 at 3:35 devongovett devongovett 4,880 5 5 gold badges 36 36 silver badges 35 35 bronze badges Isn't this a server side library? I think the discussion is on the client side js library. Commented Mar 16, 2015 at 15:53PDFKit works in both Node and the browser as mentioned in the answer. See the link to a browser demo.
Commented Mar 17, 2015 at 10:06Does this compress images? I am working on a web app that functions similar to camscanner and I am looking for PDF creation through javascript to leverage the compression and multi page format.
Commented Jul 4, 2017 at 14:47 @devongovett can you look at my question about your library? Commented Mar 15, 2019 at 20:41I've been using PDFKit recently and although it has a few glitches it does work well and is very solid. I would recommend it.
Commented Jul 6, 2021 at 9:09Another interesting project is texlive.js.
It allows you to compile (La)TeX to PDF in the browser.
answered May 30, 2016 at 13:39 3,923 1 1 gold badge 23 23 silver badges 18 18 bronze badges The hello world example renders nicely but takes around 30s to complete. Commented Jul 1, 2017 at 5:01For react fans there is another great resource for PDF generation: React-PDF
It is great for creating PDF files in React and even let the user download them from the client side itself with no server required!
this is a small example snippet of React-PDF to create a 2 section PDF file
import React from 'react'; import < Page, Text, View, Document, StyleSheet >from '@react-pdf/renderer'; // Create styles const styles = StyleSheet.create(< page: < flexDirection: 'row', backgroundColor: '#E4E4E4' >, section: < margin: 10, padding: 10, flexGrow: 1 >>); // Create Document Component const MyDocument = () => ( > > Section #1 > Section #2 );
This will produce a PDF document with a single page. Inside, two different blocks, each of them rendering a different text. These are not the only valid primitives you can use. you can refer to the Components or Examples sections for more information.
answered Oct 25, 2021 at 4:55 Dev Sebastian Dev Sebastian 667 7 7 silver badges 11 11 bronze badges Does it support tables? Commented May 8 at 10:00It is worth mentioning PDF-LIB which is an awesome library:
See how pretty the code is