Javascript:从字节数组中在新选项卡中打开PDF [英] Javascript: Open PDF in new tab from byte array
问题描述
我正在使用带有HTTP资源的AngularJS来调用外部API,而我的响应是一个字节数组。我需要在新窗口中将此字节数组转换为PDF。我没有看到任何非常好的解决方案在这里工作跨浏览器或纯javascript。有没有办法做到这一点?
I'm using AngularJS with an HTTP resource to call an external API and my response is a byte array. I need to turn this byte array into a PDF in a new window. I haven't seen any very good solutions on here that work cross browser or that are pure javascript. Is there a way to do this?
这是我的代码:
Javascript
Document.preview({id: $scope.order.id}, function(data){
// Open PDF Here
var file = new Blob([data], {type: 'application/pdf'});
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
});
推荐答案
如果有人还在寻找,那就是我正在做(和工作):
If anyone still looks for that, here is what I'm doing (and working) :
var pdfAsDataUri = "data:application/pdf;base64,"+byteArray;
window.open(pdfAsDataUri);
其中byteArray是您收到的数据。它可能不是一个很好的解决方案(字节数组在URL中可见),但它可以工作......
Where byteArray is the data you receive. It's maybe not a nice solution (byte array is visible in the URL), but it works...
这篇关于Javascript:从字节数组中在新选项卡中打开PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!