angular 9 pdf.output(';dataurlnewwindow';)不适用于chrome
在第9章中,如何以pdf格式在新选项卡中打开html内容。我已经使用jspdf完成了代码。但它只对mozilla和IE有效。Chrome阻止了新标签。因此尝试了使用Iframe的另一种方法。然后它也在为chrome工作。但是在我的html内容中需要包含更多的设计代码。之后,当我尝试空白页是在新的标签。我的代码是这样的 ngx-table-popup.component.htmlangular 9 pdf.output(';dataurlnewwindow';)不适用于chrome,angular,pdf,jspdf,Angular,Pdf,Jspdf,在第9章中,如何以pdf格式在新选项卡中打开html内容。我已经使用jspdf完成了代码。但它只对mozilla和IE有效。Chrome阻止了新标签。因此尝试了使用Iframe的另一种方法。然后它也在为chrome工作。但是在我的html内容中需要包含更多的设计代码。之后,当我尝试空白页是在新的标签。我的代码是这样的 ngx-table-popup.component.html 查看PDF格式 ngx-table-popup.component.ts ``` import
查看PDF格式
ngx-table-popup.component.ts
``` import jsPDF from 'jspdf';
import html2canvas from 'html2canvas';
/***********tried with pdf.output('dataurlnewwindow') ,it is only working with IE and mozilla***********************/ public
captureScreen() {
var data = document.getElementById('contentToConvert');
html2canvas(data).then(canvas => {
var imgWidth = 208;
var pageHeight = 295;
var imgHeight = canvas.height * imgWidth / canvas.width;
var heightLeft = imgHeight;
const contentDataURL = canvas.toDataURL('image/png')
let pdf = new jsPDF('p', 'mm', 'a4'); // A4 size page of PDF
var position = 0;
pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight)
pdf.output('dataurlnewwindow');
}); } /*****************tried with Iframe.Working only for less content.More html content like invoice report is not
displaying with iframe ********************************/
dataString:string;
public captureScreen() {
var data = document.getElementById('contentToConvert');
html2canvas(data).then(canvas => {
var imgWidth = 208;
var pageHeight = 295;
var imgHeight = canvas.height * imgWidth / canvas.width;
var heightLeft = imgHeight;
const contentDataURL = canvas.toDataURL('image/png')
let pdf = new jsPDF('p', 'mm', 'a4'); // A4 size page of PDF
var position = 0;
pdf.addImage(contentDataURL, 'PNG', 0, position, imgWidth, imgHeight)
//pdf.output('dataurlnewwindow');
this.dataString=pdf.output('datauristring'); this.debugBase64(this.dataString);
}); } debugBase64(base64URL){
var win = window.open();
win.document.write('<iframe type="application/pdf" src="' + base64URL + '" frameborder="0" style="border:0; top:0px; left:0px;
bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen
></iframe>'); }
``从“jsPDF”导入jsPDF;
从“html2canvas”导入html2canvas;
/***********尝试使用pdf.output('dataurlnewwindow'),它仅适用于IE和mozilla***************************/public
captureScreen(){
var data=document.getElementById('contentToConvert');
html2canvas(数据)。然后(画布=>{
var imgWidth=208;
var pageHeight=295;
var imgHeight=canvas.height*imgWidth/canvas.width;
var heightLeft=imgHeight;
const contentDataURL=canvas.toDataURL('image/png')
让pdf=newJSPDF('p','mm','a4');//a4大小的pdf页面
var位置=0;
addImage(contentDataURL,'PNG',0,位置,imgWidth,imgHeight)
输出('dataurlnewwindow');
});}/*************尝试使用Iframe。仅适用于较少的内容。不适用于更多html内容,如发票报告
用iframe显示********************************/
数据字符串:字符串;
公共captureScreen(){
var data=document.getElementById('contentToConvert');
html2canvas(数据)。然后(画布=>{
var imgWidth=208;
var pageHeight=295;
var imgHeight=canvas.height*imgWidth/canvas.width;
var heightLeft=imgHeight;
const contentDataURL=canvas.toDataURL('image/png')
让pdf=newJSPDF('p','mm','a4');//a4大小的pdf页面
var位置=0;
addImage(contentDataURL,'PNG',0,位置,imgWidth,imgHeight)
//输出('dataurlnewwindow');
this.dataString=pdf.output('datauristring');this.debugBase64(this.dataString);
});}debugBase64(base64URL){
var win=window.open();
win.document.write(“”);}
要在新窗口中打开报告,可以使用窗口对象。
.//这里您只需要将pdf转换为base64字符串。
const reportpdf=btoa(pdf.output());
//将其传递到createObjectURL方法中
如果(PDF){
const blobURL=URL.createObjectURL(this.pdfBlobConversion(reportpdf,'application/pdf');
窗口打开(blobURL);
}
//将base64转换为windows的blob类型
pdfBlobConversion(B64数据:字符串,内容类型:字符串){
contentType=contentType | |“”;
常量切片大小=512;
b64Data=b64Data.replace(/^[^,]+,/,'');
b64Data=b64Data.replace(/\s/g');
const byteCharacters=window.atob(b64Data);
常量字节数组=[];
对于(让偏移量=0;偏移量
To open a report in the new window you can use the window object.
.// Here you just need to convert pdf to base64 string.
const reportpdf = btoa(pdf.output());
// pass it into the createObjectURL method
if (reportpdf) {
const blobURL = URL.createObjectURL(this.pdfBlobConversion(reportpdf, 'application/pdf'));
window.open(blobURL);
}
// converts base64 to blob type for windows
pdfBlobConversion(b64Data: string, contentType: string) {
contentType = contentType || '';
const sliceSize = 512;
b64Data = b64Data.replace(/^[^,]+,/, '');
b64Data = b64Data.replace(/\s/g, '');
const byteCharacters = window.atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset = offset + sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, { type: contentType });
return blob;
}