簡體   English   中英

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

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

我正在使用jspdf生成一個 pdf 文件。 一切正常。 但是如何在新選項卡或新窗口中打開生成的 pdf。

我在用

doc.output('datauri');

這是在同一選項卡中打開 pdf。

根據來源,您可以為 output() 使用“dataurlnewwindow”參數:

doc.output('dataurlnewwindow');

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

我必須使用它來直接加載 PDF。 使用doc.output('dataurlnewwindow'); 為我生成了一個丑陋的 iframe。 蘋果/鉻。

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

這個解決方案對我有用

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

或者...您可以使用 Blob 來實現這一點。

像:

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

該代碼允許您在瀏覽器中創建一個 Blob 對象並在新選項卡中顯示它。

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

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

此代碼將幫助您在具有所需標題的新選項卡中打開生成的 pdf

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

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

這對我有用!!!

當您指定窗口功能時,它將在新窗口中打開

就像 :

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

第 I 步:包含文件和插件

../jspdf.plugin.addimage.js

第二步:構建PDF內容 var doc = new jsPDF();

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

第三步:在新窗口中顯示圖像

doc.output('dataurlnewwindow');

第四步:保存數據

var output = doc.output();
return btoa( output);

第 1 步
關閉添加塊

第 2 步
添加

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

或者試試

doc.output('dataurlnewwindow')

通常,您可以下載、顯示或獲取 blob 字符串

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

Javascript 代碼:在結束行中添加

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

HTML 代碼:插入正文

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

在 iframe 內的同一窗口內預覽以及其他內容。

此外,重要的是要記住輸出方法有其他值,測試所有這些值以選擇適合您的情況的理想值可能會很有趣。

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

一次測試一行:

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