How to open generated pdf using jspdf in new window

后端 未结 14 2373
遇见更好的自我
遇见更好的自我 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 15:56

    STEP 1
    Turn off addblock

    STEP 2
    Add

    window.open(doc.output('bloburl'), '_blank');
    

    Or try

    doc.output('dataurlnewwindow')
    
    0 讨论(0)
  • 2020-12-04 15:57

    Based on the source you can use the 'dataurlnewwindow' parameter for output():

    doc.output('dataurlnewwindow');
    

    Source in github: https://github.com/MrRio/jsPDF/blob/master/jspdf.js#L914

    All possible cases:

    doc.output('save', 'filename.pdf'); //Try to save PDF as a file (not works on ie before 10, and some mobile devices)
    doc.output('datauristring');        //returns the data uri string
    doc.output('datauri');              //opens the data uri in current window
    doc.output('dataurlnewwindow');     //opens the data uri in new window
    
    0 讨论(0)
  • 2020-12-04 15:57

    This solution working for me

    window.open(doc.output('bloburl'))
    
    0 讨论(0)
  • 2020-12-04 15:58
    1. Search in jspdf.js this:

      if(type == 'datauri') {
          document.location.href ='data:application/pdf;base64,' + Base64.encode(buffer);
      }
      
    2. Add :

      if(type == 'datauriNew') {   
          window.open('data:application/pdf;base64,' + Base64.encode(buffer));
      }
      
    3. call this option 'datauriNew' Saludos ;)
    0 讨论(0)
  • 2020-12-04 16:02

    Or... You can use Blob to achive this.

    Like:

    pdf.addHTML($('#content'), y, x, options, function () {
        var blob = pdf.output("blob");
        window.open(URL.createObjectURL(blob));
    });
    

    That code let you create a Blob object inside the browser and show it in the new tab.

    0 讨论(0)
  • 2020-12-04 16:02

    Javascript code: Add in end line

    $("#pdf_preview").attr("src", pdf.output('datauristring'));
    

    HTML Code: Insert in body

    <head>
    </head>
    <body>
    <H1>Testing</h1>
    <iframe id="pdf_preview" type="application/pdf" src="" width="800" height="400"></iframe>
    </body>
    </html>
    

    preview within same window inside iframe along with with other contents.

    0 讨论(0)
提交回复
热议问题