1

Hi I am using jsPdf for making pdf of html content, it is going fine for short content and creating pdf , but when I am trying to use it on large content with html2canvas.js (for rendering css), it is not creating pdf. Any suggestions or sample code for this would be helpful.Thank you.

Ankit Dubey
  • 19
  • 1
  • 1
  • 5

2 Answers2

5

It is possible to create pdf for large files. There are primarily two ways to do this :

1. html -> canvas -> image -> pdf (I assume you are trying this approach)

2. html -> pdf (does not work if html contains svg)

I would suggest you to go for (2) unless you have a good reason to go for (1) (like if you are have svg content)-- it is quite expensive operation for the browser and there is possibility of the browser crashing too.

1. html -> canvas -> image -> pdf

This is very neatly described here - https://github.com/MrRio/jsPDF/issues/339#issuecomment-53327389

My experience when using this method - crashes when the pdf generated contains more than 2-3 pages. (tested on latest chrome and firefox)

2. html -> pdf

var pdf = new jsPDF('l', 'pt', 'a4');
 var options = {
    pagesplit: true
};

pdf.addHTML($('body'), 0, 0, options, function(){
    pdf.save("test.pdf");
});

This is way faster compared to above approach. Generates pdf containing 5-6 pages in 1-2 seconds!

Hope this helps!

prad
  • 1,086
  • 1
  • 11
  • 19
  • This works like a charm. My only question would be, is there a way to target a specific div inside the body, instead of the hole body? – Backer Jun 01 '15 at 15:18
  • yes, use the second approach from the answer and replace $('body') with an appropriate jquery selector of your div. eg. $('#id_of_your_div') – prad Jun 03 '15 at 07:04
  • greetings! can you help me getting all the content from the div and not only what is visible on the viewport. – Terence Oct 27 '17 at 09:39
  • How about a 53 page pdf? – Starchand Dec 01 '17 at 11:43
1

PDFPY

https://www.npmjs.com/package/pdfpy

var data = {
    //the key as to be same as below
    input: "./test.html",
    output: "./output.pdf"
}

pdfpy.file(data, function(err, res) {
    if(err) throw err

    if(res) console.log("success")
});