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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17739816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 09:26:04  来源:igfitidea点击:

How to open generated pdf using jspdf in new window

javascriptjspdf

提问by Satendra Jindal

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

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

I am using

我在用

doc.output('datauri');

Which is opening the pdf in same tab.

这是在同一选项卡中打开 pdf。

采纳答案by Juan Capello

  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 ;)
  1. 在 jspdf.js 中搜索:

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

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

回答by kardave

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

根据来源,您可以为 output() 使用“dataurlnewwindow”参数:

doc.output('dataurlnewwindow');

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

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

回答by William Entriken

I have to use this to load the PDF directly. Using doc.output('dataurlnewwindow');produces an ugly iframe for me. Mac/Chrome.

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

  var string = doc.output('datauristring');
  var x = window.open();
  x.document.open();
  x.document.location=string;

回答by ilter

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

或者...您可以使用 Blob 来实现这一点。

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.

该代码允许您在浏览器中创建一个 Blob 对象并将其显示在新选项卡中。

回答by sol404

This solution working for me

这个解决方案对我有用

window.open(doc.output('bloburl'))

回答by Alocus

This is how I handle it.

我就是这样处理的。

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

回答by Neetz

this code will help you to open generated pdf in new tab with required title

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

 let pdf = new jsPDF();
 pdf.setProperties({
          title: "Report"
      });
      pdf.output('dataurlnewwindow');

回答by rod

using javascript you can send the generated pdf to a new window using the following code.

使用 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();

回答by Aswathy

This works for me!!!

这对我有用!!!

When you specify window features, it will open in a new window

当您指定窗口功能时,它将在新窗口中打开

Just like :

就像 :

window.open(url,"_blank","top=100,left=200,width=1000,height=500");

回答by Ismael Soschinski

Javascript code

Javascript代码

// 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);
}