无法加载PDF文档javascript

时间:2018-01-24 11:11:36

标签: javascript

我正在尝试使用以下代码生成简单的PDF文件,但失败并显示错误:“无法加载PDF文档”:

var downloadLink      = document.createElement('a');
downloadLink.target   = '_blank';
downloadLink.download = 'name_to_give_saved_file.pdf';

// convert downloaded data to a Blob
var blob = new Blob(['downloadedFile'], { type: 'application/pdf' });

// create an object URL from the Blob
var URL = window.URL || window.webkitURL;
var downloadUrl = URL.createObjectURL(blob);

// set object URL as the anchor's href
downloadLink.href = downloadUrl;

// append the anchor to document body
document.body.append(downloadLink);

// fire a click event on the anchor
downloadLink.click();

试图遵循许多现有答案(例如How to build PDF file from binary string returned from a web-service using javascript)但尚未得到任何解决方案。请帮忙。 在此先感谢:)

1 个答案:

答案 0 :(得分:0)

想出来了。问题是我们需要传递有效的

pdf(datatype/contenttype - binary) to URL.createObjectURL(blob).

由于