How to open generated pdf using jspdf in new window

后端 未结 14 2374
遇见更好的自我
遇见更好的自我 2020-12-04 15:50

I am using jspdf to generate a pdf file. Every thing is working fine. But how to open generated pdf in new tab or new window.

I am using



        
相关标签:
14条回答
  • 2020-12-04 16:19

    Step I: include the file and plugin

    ../jspdf.plugin.addimage.js
    

    Step II: build PDF content var doc = new jsPDF();

    doc.setFontSize(12);
    doc.text(35, 25, "Welcome to JsPDF");
    doc.addImage(imgData, 'JPEG', 15, 40, 386, 386);
    

    Step III: display image in new window

    doc.output('dataurlnewwindow');
    

    Stepv IV: save data

    var output = doc.output();
    return btoa( output);
    
    0 讨论(0)
  • 2020-12-04 16:20

    Javascript code

    // IE doesn't allow using a blob object directly as link href
    // instead it is necessary to use msSaveOrOpenBlob
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
      window.navigator.msSaveOrOpenBlob(doc.output("blob"), "Name.pdf");
    } else {
    
      // For other browsers:
      // Create a link pointing to the ObjectURL containing the blob.
      doc.autoPrint();
      window.open(
        URL.createObjectURL(doc.output("blob")),
        "_blank",
        "height=650,width=500,scrollbars=yes,location=yes"
      );
    
      // For Firefox it is necessary to delay revoking the ObjectURL
      setTimeout(() => {    
        window.URL.revokeObjectURL(doc.output("bloburl"));
      }, 100);
    }
    
    0 讨论(0)
提交回复
热议问题