简体   繁体   English

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

[英]How to open generated pdf using jspdf in new window

I am using jspdf to generate a pdf file.我正在使用jspdf生成一个 pdf 文件。 Every thing is working fine.一切正常。 But how to open generated pdf in new tab or new window.但是如何在新选项卡或新窗口中打开生成的 pdf。

I am using我在用

doc.output('datauri');

Which is opening the pdf in same tab.这是在同一选项卡中打开 pdf。

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

I have to use this to load the PDF directly.我必须使用它来直接加载 PDF。 Using doc.output('dataurlnewwindow');使用doc.output('dataurlnewwindow'); produces an ugly iframe for me.为我生成了一个丑陋的 iframe。 Mac/Chrome.苹果/铬。

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

这个解决方案对我有用

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

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 对象并在新选项卡中显示它。

  1. Search in jspdf.js this:在 jspdf.js 中搜索:

     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 ;)将此选项称为'datauriNew' Saludos ;)

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

我就是这样处理的。

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

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');

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

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");

Step I: include the file and plugin第 I 步:包含文件和插件

../jspdf.plugin.addimage.js

Step II: build PDF content var doc = new jsPDF();第二步:构建PDF内容 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);

STEP 1第 1 步
Turn off addblock关闭添加块

STEP 2第 2 步
Add添加

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

Or try或者试试

doc.output('dataurlnewwindow')

Generally you can download it, show, or get a blob string :通常,您可以下载、显示或获取 blob 字符串

const pdfActions = {
    save: () => doc.save(filename),
    getBlob: () => {
      const blob = doc.output('datauristring');
      console.log(blob)
      return blob
    },
    show: () => doc.output('dataurlnewwindow')
  }

Javascript code: Add in end line Javascript 代码:在结束行中添加

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

HTML Code: Insert in body HTML 代码:插入正文

<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.在 iframe 内的同一窗口内预览以及其他内容。

Additionally, it is important to remember that the output method has other values and it might be interesting to test all of them to choose the ideal one for your case.此外,重要的是要记住输出方法有其他值,测试所有这些值以选择适合您的情况的理想值可能会很有趣。

https://artskydj.github.io/jsPDF/docs/jsPDF.html#output https://artskydj.github.io/jsPDF/docs/jsPDF.html#output

test one line at a time:一次测试一行:

doc.output('arraybuffer');
doc.output('blob');
doc.output('bloburi');
doc.output('bloburl');
doc.output('datauristring');
doc.output('dataurlstring');
doc.output('datauri');
doc.output('dataurl');
doc.output('dataurlnewwindow');
doc.output('pdfobjectnewwindow');
doc.output('pdfjsnewwindow');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM