1

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);
});
smalinux
  • 1,132
  • 2
  • 13
  • 28
zer0
  • 4,657
  • 7
  • 28
  • 49
  • http://stackoverflow.com/questions/391005/convert-html-css-to-pdf-with-php – Benjamin Sep 15 '16 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: http://wkhtmltopdf.org/ . – Michas Sep 15 '16 at 20:13

1 Answers1

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');
});
cstopher
  • 261
  • 1
  • 7
  • Ok, I am tried this out - a combination of html2canvas and jsPDF over here: http://jsfiddle.net/5ud8jkvf/6320/ Any idea why the PDF returns an empty document? – zer0 Sep 15 '16 at 20:45