Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何打印base64 pdf?_Javascript_Jquery_Pdf - Fatal编程技术网

Javascript 如何打印base64 pdf?

Javascript 如何打印base64 pdf?,javascript,jquery,pdf,Javascript,Jquery,Pdf,我从要打印的服务器收到base64 pdf 我一直在尝试以下方法: $.ajax({ type: "POST", url: url, data: blahblahblah, success: function(data) { var printWindow = window.open( "data:application/pdf;base64, " + data ); printWindow.print(); } });

我从要打印的服务器收到base64 pdf

我一直在尝试以下方法:

$.ajax({
    type: "POST",
    url: url,
    data: blahblahblah,
    success: function(data) {
        var printWindow = window.open( "data:application/pdf;base64, " + data );
        printWindow.print();
    }
});
遗憾的是,这在Chrome中不起作用。我收到以下错误:

SecurityError:阻止原点为“xxx”的帧访问 原点为“空”的帧。请求访问的帧具有协议 对于“http”,正在访问的帧具有“数据”协议。 协议必须匹配


关于如何解决此问题的建议?

您可以尝试打开窗口并尝试将pdf数据作为嵌入文件插入

下面是一段我发现并使用得很好的代码(我更改为适合您的代码,但未经测试):

$.ajax({
类型:“POST”,
url:url,
资料来源:布拉布拉赫布拉赫,
成功:功能(数据){
var winparams='dependent=yes,locationbar=no,scrollbars=yes,menubar=yes,'+
'可调整大小,屏幕X=50,屏幕Y=50,宽度=850,高度=1050';
var htmlop='';
var printWindow=window.open(“,”PDF“,winparams);
printWindow.document.write(htmlPop);
printWindow.print();
}
});

希望有帮助。

您可以使用jspdf这样做

var p=newjspdf();
p、 添加图像(imgData,'JPEG',0,0);//其中imageDate包含base64字符串
//在新选项卡中打开作为Base64编码字符串接收的PDF
const openPdf=(basePdf)=>{
let byteCharacters=atob(basePdf);
让byteNumbers=新数组(byteCharacters.length);
for(设i=0;i
谢谢,但不幸的是,这不起作用。我相信它也遇到了同样的x域问题。从我的在线阅读来看,这可能是不可能的…你的数据是二进制的吗?在我的代码中,它是以字符串的形式返回的,所以我不得不使用escape(数据),也许这可能是这里的问题。我就是没法把它打印出来。我将在今晚晚些时候检查这个问题,也许是时间问题。这个页面是MeNoT的空白,先生,我在BASE中通过我的BASE 64字符串,但是它只打开空白页,上面有左上角的日期,这个链接我已经回答了。在这里,PTCH的答案在Chrome 49以上的任何东西上都是非常有用的,即10 +,Edgage,Safari 7 +,Firefox 8 +。只需将数据uri移动到blob url(浏览器中的所有本地url)中,并从iframe打印blob。
    $.ajax({
    type: "POST",
    url: url,
    data: blahblahblah,
    success: function(data) {

        var winparams = 'dependent=yes,locationbar=no,scrollbars=yes,menubar=yes,'+
            'resizable,screenX=50,screenY=50,width=850,height=1050';

        var htmlPop = '<embed width=100% height=100%'
                         + ' type="application/pdf"'
                         + ' src="data:application/pdf;base64,'
                         + escape(data)
                         + '"></embed>'; 

        var printWindow = window.open ("", "PDF", winparams);
        printWindow.document.write (htmlPop);
        printWindow.print();
    }
});
// open PDF received as Base64 encoded string in new tab
const openPdf = (basePdf) => {
  let byteCharacters = atob(basePdf);
  let byteNumbers = new Array(byteCharacters.length);
  for (let i = 0; i < byteCharacters.length; i++) {
    byteNumbers[i] = byteCharacters.charCodeAt(i);
  }
  let byteArray = new Uint8Array(byteNumbers);
  let file = new Blob([byteArray], {type: 'application/pdf;base64'});
  let fileURL = URL.createObjectURL(file);
  window.open(fileURL);
}