通过iframe将浏览器中的PDF URL发送到打印机

Send a PDF URL in a browser to the printer via iframe

本文关键字:URL 打印机 PDF iframe 浏览器 通过      更新时间:2023-09-26

对于当前的非IE浏览器(Chrome、Firefox、Opera、Safari),我希望在给定PDF URL的情况下将PDF文档发送到打印机。

为了避免弹出多余的窗口,我目前正在使用<iframe>进行此操作,但我希望在打印完成后关闭iframe,否则一些浏览器在试图离开页面时会弹出对话框。

以下是我到目前为止所想到的(为了简单起见,使用lodash和jQuery):

var _print_url, _remove_iframe, _set_print;
_print_url = function(src_url) {
  $("<iframe src='" + src_url + "' type='application/pdf'>").css({
    visibility: 'hidden',
    position: 'fixed',
    right: '0',
    bottom: '0'
  }).on('load', _set_print).appendTo(document.body);
};
_remove_iframe = function(iframe) {
  return $(iframe).parent().find(iframe).detach();
};
_set_print = function() {
  this.contentWindow.print();
/* 
 * Where we could do @contentWindow.close() with a window, we must remove the
 * print iframe from the DOM. We have to engage in some async madness
 * it seems, for no apparent reason other than this won't work
 * synchronously (@cw.print above must be async, it seems) - even though 
 * window.close() appears to work synchronously.
 */
  _.delay(_.partial(_remove_iframe, this), 100);
};

有时,谷歌Chrome的打印对话框似乎会正确显示PDF,但当选择打印机并确认打印意图时,它实际上会将框架父页的内容发送到打印机,而不是PDF本身。

Mozilla页面上有一个链接建议,但这份文档目前似乎已经过时了。我能找到的最好的例子是对发票的AmazonWebServices打印对话框进行逆向工程,但这打开了一个窗口。

我考虑过的一个替代方案是谷歌云打印,但很明显,这需要安装额外的软件或配置谷歌帐户,除非必要,否则我都不想强加给用户。

有没有其他例子可以说明如何在给定URL的情况下打印PDF,特别是使用Javascript,而不需要额外的浏览器插件或人工制品(如弹出窗口)?

注意,使用这种方法,您永远不会看到弹出窗口阻止程序--

几个月前,我在一个ajax应用程序中遇到了类似的问题,我面临的问题是,我需要创建pdf文件并在发送打印之前进行存储,我所做的是:

我没有用iframe。该应用程序与php TCPDF一起创建pdf,并与jqueryunderscore一起创建模板系统。

您可以在上查看演示视频http://www.screenr.com/Ur07(2:18分钟)

  1. 通过JSON(ajax)发送信息为了实现这一点,因为我面临的问题是,所有的东西都在ajax中,所以我无法用信息发布,所以我所做的是将一个隐藏的form附加到DOM,然后用target="_blank"将帖子发布到一个新窗口(在过程结束时,您将关闭该窗口)

HTML隐藏虚拟表单

<form method='<%= method %>' 
      action="<%= action %>" 
      name="<%= name %>" 
      id="<%= id %>" 
      target="_blank">
    <input type='hidden' name='json' id='<%= valueId %>' />
</form>

Javascript

 function makePost(){
  var _t = _.template(_JS_Templates["print.form.hidden"]); //this the html of the form
  var o = {
    method : "POST",
    action : js_WEB_ + "/print.php",
    name : "print_virtual_form",
    id : "print_virtual_form_id",
    valueId : "print_virtual_value"
  }
 var form = _t(o);
 $(".warp").append(form); //appending the form to the dom
  var json = {
    data : data // some data
  }     
 $("#print_virtual_value").val(JSON.stringify(json)); //assing to the hidden input the value and stringify the json
 $("#print_virtual_form_id").submit(); //make the post submmitting the form
 $("#print_virtual_form_id").remove(); //removing the "virtual hidden form"
}

2.-当你收到json时,在我的例子中,你使用php创建了你必须创建的任何东西,我是这样做的,你可以在这个例子中看到php代码(这是我的答案)

在ajax调用上使用TCPDF生成PDF

3.-最后,这是最酷的部分是响应,在这一部分中,我使用php文件编写了一个javascript代码,说必须关闭父窗口并向特定函数报告json答案,这是一种回调。

switch($_SERVER['REQUEST_METHOD']){
    case "GET":
        echo "Denied";
    break;
    case "POST":
        if(isset($_POST["json"])){
            if(!empty($_POST["json"])){
                $json = $_POST["json"];             
                $hash = PDF::makePDF($json);
                echo "<script>
                  function init() {
                        //calling callback
                                        //returning control to the previous browser
                                 window.opener.someclass.finish('".$hash."');
                                 window.close();
                    }
                window.onload = init;                       
             </script>";
            }else{
                echo "not json detected";
            }
        }else{
            echo "not json detected";
        }
    break;

4.-最后在你的窗口浏览器中控制你可以做。

var someobject = (function(name){
  //private
  var finish(data){
    //do whatever you want with the json data
  }
   //public
   var $r = {};
   $r.finish = function(data){ 
     finish(data); //you will pass the data to the finish function and that is..!! voila!!
   }
   return $r;
})("someobject");

我知道这并不完全是你所要求的,但这是解决同一问题的另一种方法,虽然我认为这稍微复杂一点,但我可以保证在很多浏览器中都能工作,用户会喜欢你这样做的方式。他们永远不会看到发生了什么,他们会下载文件,就像他们期望的那样,点击链接并保存文件。

只有我的两美分和快乐的编码。