0

I have looked at jsPDF but the plugin strips CSS in the exported PDF. I have looked at html2canvas also, but that takes a screenshot of the current window. My HTML content is hidden from the user and I am looking for a solution that would create a PDF on the fly from a HTML block. Any suggestions?

EDIT: I just tried creating html2canvas to jsPDF but I get an empty pdf document. Can someone please tell me why? http://jsfiddle.net/5ud8jkvf/6320/

$('.download').on('click', function(){
    html2canvas($('.print'), {
    onrendered: function(canvas) {
        $('#canvas').replaceWith(canvas);
    },
    //width: 200,
    //height: 200
    });
    setTimeout(function(){
        var doc = new jsPDF();
        var specialElementHandlers = {
      '#editor': function (element, renderer) {
          return true;
      }
        };

    doc.fromHTML($('canvas').html(), 15, 15, {
        'width': '200',
            'elementHandlers': specialElementHandlers
    });
    doc.save('sample-file.pdf');

    }, 500);
});
2
  • stackoverflow.com/questions/391005/… Commented Sep 15, 2016 at 19:58
  • There is no simple solution. You would need the implementation of half of the full web browser in JavaScript. I would generate PDF on server using this program: wkhtmltopdf.org . Commented Sep 15, 2016 at 20:13

1 Answer 1

3

Could you use html2canvas as follows, it shouldn't matter that your html content is hidden you should be able to access it like so:

var pdf = new jsPDF('p', 'pt', 'letter');
pdf.addHTML($('#hiddenElement').html(), function () {
    pdf.save('Test.pdf');
});
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I am tried this out - a combination of html2canvas and jsPDF over here: jsfiddle.net/5ud8jkvf/6320 Any idea why the PDF returns an empty document?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.