如何在新窗口中使用jspdf打开生成的pdf文件

Sat*_*dal 40 javascript jspdf

我正在使用jspdf生成pdf文件.一切都很好.但是如何在新选项卡或新窗口中打开生成的pdf.

我在用

doc.output('datauri');
Run Code Online (Sandbox Code Playgroud)

这是在同一个标​​签中打开pdf.

小智 97

根据源代码,您可以使用'dataurlnewwindow'参数输出():

doc.output('dataurlnewwindow');
Run Code Online (Sandbox Code Playgroud)

来自github:https: //github.com/MrRio/jsPDF/blob/master/jspdf.js#L914

所有可能的情况:

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
Run Code Online (Sandbox Code Playgroud)

  • @YaBasha您还需要FileSaver.js. (5认同)
  • `doc.output('dataurlnewwindow');`返回一个空白页面.在打电话之前我还需要做些什么吗? (3认同)

Wil*_*ken 16

我必须使用它来直接加载PDF.使用doc.output('dataurlnewwindow');为我生成一个丑陋的iframe.MAC /铬.

  var string = doc.output('datauristring');
  var x = window.open();
  x.document.open();
  x.document.location=string;
Run Code Online (Sandbox Code Playgroud)

  • 不需要使用iframe只需使用doc.output('dataurlnewwindow',{}) (3认同)
  • @FullDecent 4 年后,但现在它打开了一个空白页。 (2认同)

小智 10

  1. 在jspdf.js中搜索:

    if(type == 'datauri') {
        document.location.href ='data:application/pdf;base64,' + Base64.encode(buffer);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 添加:

    if(type == 'datauriNew') {   
        window.open('data:application/pdf;base64,' + Base64.encode(buffer));
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将此选项称为"datauriNew"Saludos;)


ilt*_*ter 9

或者......你可以使用Blob来实现这个目标.

喜欢:

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

该代码允许您在浏览器中创建Blob对象并在新选项卡中显示它.


sol*_*404 7

此解决方案为我工作

window.open(doc.output('bloburl'))
Run Code Online (Sandbox Code Playgroud)

  • window.open(pdf.output('bloburl'),'_blank'); 在Chrome中为我工作 (2认同)

小智 7

此代码将帮助您在具有所需标题的新选项卡中打开生成的 pdf

 let pdf = new jsPDF();
 pdf.setProperties({
          title: "Report"
      });
      pdf.output('dataurlnewwindow');
Run Code Online (Sandbox Code Playgroud)


小智 5

使用javascript,您可以使用以下代码将生成的pdf发送到新窗口.

var string = doc.output('datauristring');

var iframe = "<iframe width='100%' height='100%' src='" + string + "'></iframe>"

var x = window.open();
x.document.open();
x.document.write(iframe);
x.document.close();
Run Code Online (Sandbox Code Playgroud)

  • 下载按钮似乎不适用于此方法。 (2认同)

Alo*_*cus 5

这就是我处理它的方式.

window.open(doc.output('bloburl'), '_blank');
Run Code Online (Sandbox Code Playgroud)

  • @NorCalKnockOut您是否已打开任何广告或弹出窗口阻止程序? (2认同)