Skip to content Skip to sidebar Skip to footer

Send A Pdf Url In A Browser To The Printer Via Iframe

For current non-IE browsers (Chrome, Firefox, Opera, Safari), I would like to send a PDF document to the printer given a URL to that PDF. To avoid superfluous windows popping up, I

Solution 1:

-- NOTE whit this approach you will never see the popup blocker --

I run in a similar problem wiht an ajax application a couple month ago, the problem was I faced was I need to create the pdf file and store before to send it to print, what I did is the following:

I didnt use iframes. This application works with php TCPDF for creating the pdf and jquery and underscore for the templating system.

you can see the demo video at http://www.screenr.com/Ur07 (2:18 min)

  1. Send the information via JSON (ajax) To achieve this because I faced the problem that everything was in ajax, so I couldn't make post with the information, so what I did is to append a hidden form to the DOM and then with the target="_blank" make the post to a new window (which you will close a the end of the process).

HTML hidden virtual form

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

Javascript

functionmakePost(){
  var _t = _.template(_JS_Templates["print.form.hidden"]); //this the html of the formvar 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 domvar 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.- when you receive the json in my case was using php you create whatever you have to create, I did it in this way you can see the php code in this example (is my answer)

Generate PDF using TCPDF on ajax call

3.- And finally in this is where the cool parts come is the response, in this part, I use the php file to write a javascript code saying that have to close the parent windows and report the json answer to an specific function, is kind of callback.

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.- and for the end with the control in you window browser you can do.

var someobject = (function(name){
  //privatevar finish(data){
    //do whatever you want with the json data
  }

   //publicvar$r = {};
   $r.finish = function(data){ 
     finish(data); //you will pass the data to the finish function and that is..!! voila!!
   }

   return$r;
})("someobject");

I know is not exactly what you ask but is another approach to the same problem while I think this is little more complex, I can guarantee works in a lot of browser and the users will love the way you do it. They will never see what is happening and they will download the file just how the expect to do it, clicking a link and the saving the file.

Just my two cents and happy coding.

Post a Comment for "Send A Pdf Url In A Browser To The Printer Via Iframe"