如何打印动态生成的pdf dataUrl?

我正在尝试从网页上打印动态生成的PDF。

var $iframe = $(''); $iframe.appendTo('body'); $iframe.load(function() { var iframe = $iframe[0]; var result = iframe.contentWindow.document.execCommand("print", false, null); if (!result) iframe.contentWindow.print(); $.remove($iframe); }); $iframe.attr('src', dataUrl); 

execCommand()给出错误消息:

未捕获的SecurityError:阻止具有原点“ http:// localhost:2520 ”的帧访问具有原点“null”的帧。 请求访问的帧具有“http”协议,被访问的帧具有“数据”协议。 协议必须匹配。

另外,设置src attr会发出警告:

资源解释为Document但使用MIME类型application / pdf传输:

dataUrl看起来像这样:

 data:application/pdf;base64,JVBERi0xLjQKJdP... 

编辑:@Mike C

我可以创建iframe并显示pdf,但是当我打印时,它是空白的。

  body * { display:none } iframe#theframe { display:block }  var $iframe = $(''); $iframe.appendTo('body'); $iframe.load(function() { setTimeout(function() { window.print(); }, 1000); }); 

尝试使用window.open()document.write()setTimeout()

 var popup = window.open("", "w"); var html = '' + '' + '' + ''; popup.document.write(html);