如何使用canvas Jquery下载图像为pdf

时间:2017-02-02 17:14:41

标签: javascript jquery pdf canvas

如何在此代码中设置将要下载的文件是.pdf而不是.jpeg? 我知道怎么做,问题是我无法在我的代码中实现它......

  function genScreenshot() {
    document.getElementById('coglione').style.height = '3580px';
    document.getElementById('coglione').style.width = '2480px';
    $('#boxes').show();
    html2canvas(document.getElementById('boxes'), {
      onrendered: function(canvas) {
        $('#container').html("");
        $('#container').append(canvas);

        if (navigator.userAgent.indexOf("MSIE ") > 0 || 
          navigator.userAgent.match(/Trident.*rv\:11\./)) 
        {
          var blob = canvas.msToBlob();
          window.navigator.msSaveBlob(blob,'yuppy.jpeg');
        }
        else {
          $('#test').attr('href', canvas.toDataURL("image/png"));
          $('#test').attr('download','yuppy.jpeg');
          $('#test')[0].click();
        }
      }
    });
    document.getElementById('coglione').style.height = '720px';
    document.getElementById('coglione').style.width = '';
  }

有效的示例代码:

download.addEventListener("click", function() {
  // only jpeg is supported by jsPDF
  var imgData = canvas.toDataURL("image/jpeg", 1.0);
  var pdf = new jsPDF();

  pdf.addImage(imgData, 'JPEG', 0, 0);
  var download = document.getElementById('download');

  pdf.save("download.pdf");
}, false);

0 个答案:

没有答案