我有一个格式化的PDF字符串,看起来像
%PDF-1.73 0 obj<<< /Type /Group /S /Transparency /CS /DeviceRGB >> /Resources 2 0 R/Contents 4 0 R>> endobj4 0 obj<> streamx??R=o?0??+??=|vL?R???l?-???,???Ge?JK????{???Y5?????Z?k?vf?a??`G????Asf?z????`%??aI#?!;?t???GD?!???<?????B?b??
...
00000 n 0000000703 00000 n 0000000820 00000 n 0000000926 00000 n 0000001206 00000 n 0000001649 00000 n trailer << /Size 11 /Root 10 0 R /Info 9 0 R >>startxref2015%%EOF
Run Code Online (Sandbox Code Playgroud)
我试图在一个新窗口中打开这个字符串作为PDF文件.每当我使用window.open()并将字符串写入新选项卡时,它认为文本应该是HTML文档的内容.我希望它能够识别出这是一个PDF文件.
任何帮助深表感谢
Nob*_*oka 50
仅供参考,以下内容
window.open("data:application/pdf," + encodeURI(pdfString));
Run Code Online (Sandbox Code Playgroud)
在Chrome中不再有效.昨天,我遇到了同样的问题并尝试了这个解决方案,但没有用(它是'不允许将顶部框架导航到数据URL').您无法再在新窗口中直接打开数据URL.但是,您可以将其包装在iframe中,并在下面的新窗口中打开它.=)
let pdfWindow = window.open("")
pdfWindow.document.write("<iframe width='100%' height='100%' src='data:application/pdf;base64, " + encodeURI(yourDocumentBase64VarHere)+"'></iframe>")
Run Code Online (Sandbox Code Playgroud)
小智 20
window.open("data:application/pdf," + escape(pdfString));
Run Code Online (Sandbox Code Playgroud)
上面的一个粘贴了URL中的编码内容.这限制了URL中的内容长度,因此PDF文件加载失败(因为内容不完整).
Mor*_*len 19
您可能想要使用数据URI进行探索.它看起来像.
window.open("data:application/pdf," + escape(pdfString));
Run Code Online (Sandbox Code Playgroud)
我无法立即将其工作,可能是因为提供了二进制字符串的格式化.我在使用数据URI时通常也使用base64编码数据.如果您能够从后端编码传递内容,您可以使用..
window.open("data:application/pdf;base64, " + base64EncodedPDF);
Run Code Online (Sandbox Code Playgroud)
希望这是您需要的正确方向.另请注意,这在IE6/7中根本不起作用,因为它们不支持数据URI.
Fel*_*ano 15
这个对我有用.
window.open("data:application/octet-stream;charset=utf-16le;base64,"+data64);
Run Code Online (Sandbox Code Playgroud)
这个也有用
let a = document.createElement("a");
a.href = "data:application/octet-stream;base64,"+data64;
a.download = "documentName.pdf"
a.click();
Run Code Online (Sandbox Code Playgroud)
Chr*_*ell 13
我意识到这是一个非常古老的问题,但我今天遇到了同样的问题,并提出了以下解决方案:
doSomethingToRequestData().then(function(downloadedFile) {
// create a download anchor tag
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.data], { 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.appendChild(downloadLink);
// fire a click event on the anchor
downloadLink.click();
// cleanup: remove element and revoke object URL
document.body.removeChild(downloadLink);
URL.revokeObjectURL(downloadUrl);
});
Run Code Online (Sandbox Code Playgroud)
@Noby Fujioka的更新版本答案:
function showPdfInNewTab(base64Data, fileName) {
let pdfWindow = window.open("");
pdfWindow.document.write("<html<head><title>"+fileName+"</title><style>body{margin: 0px;}iframe{border-width: 0px;}</style></head>");
pdfWindow.document.write("<body><embed width='100%' height='100%' src='data:application/pdf;base64, " + encodeURI(base64Data)+"#toolbar=0&navpanes=0&scrollbar=0'></embed></body></html>");
}
Run Code Online (Sandbox Code Playgroud)
小智 7
var byteCharacters = atob(response.data);
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
var byteArray = new Uint8Array(byteNumbers);
var file = new Blob([byteArray], { type: 'application/pdf;base64' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
Run Code Online (Sandbox Code Playgroud)
您从API或其他来源返回base64字符串。您也可以下载它。
| 归档时间: |
|
| 查看次数: |
147866 次 |
| 最近记录: |