如何打印base64 pdf? [英] How to print a base64 pdf?

查看:900
本文介绍了如何打印base64 pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I receive a base64 pdf from the server which I want to print.

我一直在尝试以下方法:

I have been trying the following:

$.ajax({
    type: "POST",
    url: url,
    data: blahblahblah,
    success: function(data) {
        var printWindow = window.open( "data:application/pdf;base64, " + data );
        printWindow.print();
    }
});

遗憾的是,这在Chrome中不起作用。我收到以下错误:

Sadly, this does not work in Chrome. I am receiving the following error:


SecurityError:阻止原始xxx的框架访问带有来源的
框架空值。请求访问的帧具有http的协议
,被访问的帧具有数据协议。
协议必须匹配。

SecurityError: Blocked a frame with origin "xxx" from accessing a frame with origin "null". The frame requesting access has a protocol of "http", the frame being accessed has a protocol of "data". Protocols must match.

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

Suggestions on how to work around this?

推荐答案

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

You can try to open your window and try to insert the pdf data as embed.

这是一块我发现并使用的代码很好(我改为适合您的代码,但未经过测试):

Here is an piece of code I've found and used fine (I changed to fit on your code, but not tested):

    $.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();
    }
});

希望有所帮助。

这篇关于如何打印base64 pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆