问题
I'm trying to open a PDF file in the Microsoft Edge browser. The PDF file is generated using jsPDF library in Angular 8. The code I've written is given below:
import { Component, OnInit } from '@angular/core';
import * as data from '../dummy.json';
import * as jsPDF from 'jspdf';
import 'jspdf-autotable';
@Component({
selector: 'app-create-pdf',
templateUrl: './create-pdf.component.html',
styleUrls: ['./create-pdf.component.css']
})
export class CreatePDFComponent implements OnInit {
constructor() { }
ngOnInit() {
debugger;
var columns = [
{title: "ID", dataKey: "id"},
{title: "Name", dataKey: "name"},
{title: "Country", dataKey: "country"},
];
var rows = [
{"id": 1, "name": "Shaw", "country": "Tanzania"},
{"id": 2, "name": "Nelson", "country": "Kazakhstan"},
{"id": 3, "name": "Garcia", "country": "Madagascar"},
];
var doc = new jsPDF('p', 'pt');
doc.autoTable(columns, rows, {
styles: {fillColor: [100, 255, 255]},
columnStyles: {
id: {fillColor: 255}
},
margin: {top: 60},
beforePageContent: function(data) {
doc.text("Header", 40, 30);
}
});
let string = doc.output("datauristring");
let file = this.dataURLtoFile(string,"Report.pdf");
var url = URL.createObjectURL(file);
var newWindow = window.open();
newWindow.document.open();
newWindow.document.write('<html><body><object data = "'+url+'" width = "100%" height = "100%" type = "application/pdf"></object></body></html>');
}
dataURLtoFile(dataurl, filename) {
var arr = dataurl.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type:mime});
}
}
When I run the code in Google Chrome, the file opens. But when I try it in Microsoft Edge, the screen shows "Couldn't open PDF, something's keeping this PDF from opening" error. I tried clearing the cache of the browser, but still, the error shows. Can anybody please help me by giving a solution?
N.B. I'm attaching a screenshot of the error message for your reference.
Screenshot of Error
Thanks in advance.
回答1:
Not sure whether this is the same issue you had posted 4 days ago.
PDF File generated using jsPDF doesn't show on Microsoft Edge
I again try to make research based on new error and I found that this error may occur if you had installed any other pdf viewer for viewing the pdf. It can interrupt the Edge and can cause this error.
You can try to Refer following points may help to fix this issue.
(1) Set Microsoft Edge as a default PDF viewer.
(2) Delete Cache in Microsoft Edge.
(3) Repair Microsoft Edge.
For detailed steps, you can refer link below.
Couldn’t open PDF in Edge, Something’s keeping this PDF from the opening
来源:https://stackoverflow.com/questions/59025698/trying-to-open-pdf-file-in-edge-shows-somethings-keeping-this-pdf-file-from-op