Yii 1.1: mprint

Print what you see.

A different approach to print whatever is in your screen.

This is just a wrapper for the javascript code from http://www.bennadel.com, so the features/limitations from the said script will also be found in this extension.




Yii 1.1 or above jQuery


Extract it under your /extensions directory.

Declare the largest element as the 'element', and just add the unneeded DIVs or classes under exceptions.

Edit the mprint.css or/and add css to satisfy your taste.

Debug mode is added so that you can play with the the page that you will be printing.If you enable this feature, you can set the dimensions of the mPrint section (which appears at the bottom of the page) by overriding dbgWidth and dbgHeight, which defaults are set to 100%. Printing will not proceed if debug mode is enabled.


     $this->widget('ext.mPrint.mPrint', array(
          'title' => 'title',          //the title of the document. Defaults to the HTML title
          'tooltip' => 'Print',        //tooltip message of the print icon. Defaults to 'print'
          'text' => 'Print Results',   //text which will appear beside the print icon. Defaults to NULL
          'element' => '#page',        //the element to be printed.
          'exceptions' => array(       //the element/s which will be ignored
          'publishCss' => true,       //publish the CSS for the whole page?
          'visible' => Yii::app()->user->checkAccess('print'),  //should this be visible to the current user?
          'alt' => 'print',       //text which will appear if image can't be loaded
          'debug' => true,            //enable the debugger to see what you will get
          'id' => 'print-div'         //id of the print link



  • appended the id in registerScript so as to work on multiple print links on the page (thanks tommo)


  • used the element declared by the user instead of adding the class mprint (thanks Trejder)
  • added the property id, which will serve as the id attribute of the print link


  • added the property cssFile which is intended to replace css so as to avoid confusion for those who are used to in cssFile variable (besides, it is more definitive since it is a property for a css file)
  • added property dbgHeight and dbgWidth, which will be the size of the debug portion
  • added the property timeOut which will tell how many seconds before the iframe disappears. This will only be usable if debug is set to false
  • added the method mCssFile which will manage what value to use between css and cssFile property
  • made some adjustments with the js
  • fixed the code so as to make use of $cssFile variable more effectively (thanks tydeas_dr)


  • added the variable 'debug' so that the result can be seen and checked first before sending it out to the printer
  • enhanced the passsing of parameters to the js by using json_encode function
  • enhanced the js code (added $.fn.print.defaults)
  • fixed some bugs found when using FF


  • added the property 'alt' (string), which is the text that will appear if the image is not available
  • modified the way the property is being checked from isset() to strlen().
  • enhanced the way in producing the image tag and used the Yii-way instead of manually creating it.
  • enhanced (somehow) the documentation.


  • added the property 'showIcon' (bool), which tells whether to include the image in the link or not
  • added the property 'image' (string), which tells the widget what icon to use. With this, different icons can be used in different widget calls (for example, one grid icon to print the datagrid results, and one calendar icon to print the calendar div). It will only be rendered if the properties 'visible' and 'showIcon' are set to 'true'. Default value is 'printer.png'


  • added the property 'visible' (bool), which dicates the visibility of the printing link
  • modified some lines of codes to make it more clearer


  • fixed the way the JS file creates the iFrame (where <head> is inside the <body>)
  • added the execCommand in mPrint.js to fix IE7 and IE8 issue on printing banners


  • fixed the bug 'Missing argument 2 for CClientScript::registerCss()' by replacing registerCss with registerCssFile (thanks to joblo)

  • modified the property 'exemptions' to 'exceptions' for a more definitive term (thanks Gustavo)

  • added the property 'publishCss' (bool), which will dictate whether to register the CSS file for the whole page (for the benefit of CTRL+P).


  • initial release


JQuery print code came from: http://www.bennadel.com

Total 18 comments

#17052 report it
Aneesh Asokan at 2014/04/29 03:02am
Print the same data for two print events

When i try to print two divisions using this widget, it shows the same document for printing when i click the print button.

<div id="idcard">
    idcard data
<?php $this->widget('application.extensions.print.printWidget', array(
<div id="test">
    test data
<?php $this->widget('application.extensions.print.printWidget', array(

How it can solve?

#16541 report it
AhmedK at 2014/03/04 11:14am
this is what i need !!

im new in yii and i really appreciate if someone help me to integrate this extension in my code !! thx

#15456 report it
beks at 2013/11/12 07:26am
Guide me on how to implement Mprint where exactly in model,view or controller

Am just inquiring help from the MPrint To be able to print some forms ..Where do i implement it.

#11061 report it
Anthony Gough at 2012/12/13 01:45am
Blank page in Chrome

@MaNiShGaIkWaD - I was getting a blank page in Chrome however when I turned off debug ('debug'=>false) the page rendered correctly - good extension however may appear not to work unless you turn debug off.

#10346 report it
MaNiShGaIkWaD at 2012/10/22 12:20am
mPrint does not work with chrome , safari

Is there a solution for Chrome / Safari ? the print option opens a blank page.

#5986 report it
Trejder at 2011/12/02 11:05am
Two problems

Hi again,

I've run into two problems. Smaller and bigger one.

  1. You named graphic and css file with all letters lowercase, but you named main file (mPrint.js) with a capital letter. This can potentially make some problems on servers that differentiate letters in filenames. My Firefox got wako even on Windows and was claiming that mPrint.js file does not exist in assets folder, even thought it was there, and forced to load script until I renamed it to all lowercase. But that could be FF-wako.

  2. I have a big problem with number of print dialogs opened (print jobs created). They are often much more then actual print calls. On one page I have four different dives to be printed and after a serie o quick refreshes of that page I'm ending up with sixteen (!) print dialogs and Firefox asking me, if I want to prevent current page from opening some more dialogs.

On page, where I print only one div I often end up with 2-4 print dialogs.

That is a really strange thing. I was trying to investigate, what can cause it? But I ended up with nothing. I thought, it is caused by that IFRAME not being destroyed immediately, but I set timeOut to 1 second (debug mode of course disabled) and the problem remains. Either timeOut gets ignored and or IFRAME is not being destroyed or it is not the source of this problem.

Do you have any ideas, what can cause such strange behaviour of multiplying print jobs / print dialogs?

BTW: Can you explain, what is the whole stuff with this timeout and iframes not being destroyed immediately after print, but kept for one minute or so (with default settings)? Why do you need this?

#4497 report it
macinville at 2011/07/13 12:58pm
Updated at last!

Thanks tommo for your comment, I have updated the source at last. :)

And thanks to la-ci for reminding me to update this extension.

Apologies to everyone for the inconvenience and the delay.

#4249 report it
Tommo at 2011/06/20 04:17am
If you want to have multiple print links on the page...

It won't work with the current setup because the javascripts will overwrite each other.

This fixes it...I edited line 284 in mPrint.php - I added $this->id to the script identifier so that the new javascripts are all uniquely identified.

Yii::app()->clientScript->registerScript('processPrint'.$this->id, '
#3858 report it
Trejder at 2011/05/14 03:17pm
It is me, who should thanks! :]

Hi macinville,

Thank you for your reply and update of the extension. Even from description of version 1.1 I see that changes are looking good! Can't wait to get my hands on it! :] I'll test your changes, when I return to work - hopefully on Monday! :]

Thank you again and have a nice weekend.

#3855 report it
macinville at 2011/05/14 08:27am

Hi Trejder,

I appreciate the comments and suggestions. I have been so busy lately with my new projects, my apologies if I was not able to update mprint.

I will update the extension just like what you suggested. Thanks so much for your input.

#3744 report it
Trejder at 2011/05/04 10:03am
Adding "by-pass" class?

Hi again,

Just a small suggestion of change to base code.

What is the reason, why you add a dummy class to div that will be printed, in one place of the code:


where tab-to-be-printed is ID of div to be printed out (selected in widget configuration).

And in another place of your code you print it with:

$( ".mprint" ).print({"documentName":"Title","cssFile":"\/app\/assets\/172d6395\/mprint.css","debug":false,"dbgHeight":"100%","dbgWidth":"100%","timeOut":60});

Wouldn't it be just easier to use user div directly, instead of defining some by-pass class? For example like this:

$( "#tab-to-be-printed" ).print({"documentName":"Title","cssFile":"\/app\/assets\/172d6395\/mprint.css","debug":false,"dbgHeight":"100%","dbgWidth":"100%","timeOut":60});

Your current implementation makes it a little bit problem to print out div, which contents are being generated by AJAX call. Function you use to add mprint class to div to be printed out (first piece of code in this comment) is fired upon page load (as mprint widged is placed in main page body), but during firing it by browser, selected div (#tab-to-be-printed in this example) does not exitst. As it will be generated as a result of AJAX call.

In current implementation after firing AJAX call and putting its results to a proper place, user code must manually again call $("#tab-to-be-printed").addClass("mprint");, as without it - printing will not work.

Doing just a little change (third piece of code in this comment) would avoid user from doing so.

#3705 report it
Trejder at 2011/04/29 08:58am
Printing with background colors

This extension looks wonderful and has everything I need. Thank you so much for it!

I was wondering if there is any way to print contents of some element including background colours of elements inside it? If I'm not mistaken, this is being blocked by default by a browser (i.e. you can't print page with background colours) therefore I wouldn't be surprised if it would turn out to be impossible.

Anyway I would like to ask if someone also run into this issue and possibly found any solution?

#3354 report it
macinville at 2011/04/05 12:27pm
Thanks a bunch!

Thank you tydeas for your comments. I have re-bundled this extension according to your suggestion regarding the use of $cssFile. I will upload the fix for Opera and Google browsers within the week.

I really appreciate it. Thanks again

#3349 report it
tydeas_dr at 2011/04/05 07:34am
FIX: $this->mCssFile()

Line 322-324 must be added so you use the $cssFile property.

315     public function mCssFile() {
316         if (!isset($this->cssFile)) {   //no cssFile
317             if (isset($this->css)) {
318                 $this->css = $this->css;
319             } else {                  //no cssFile and css
320                 $this->css = 'mprint.css';
321             }
322         } else {
323             $this->css = $this->cssFile;
324         }
325         return $this->css;
326     }
#3348 report it
tydeas_dr at 2011/04/05 07:32am
FIX: $this->mCssFile()

Line 322-324 must be added so you use the $cssFile property. 315 public function mCssFile() { 316 if (!isset($this->cssFile)) { //no cssFile 317 if (isset($this->css)) { 318 $this->css = $this->css; 319 } else { //no cssFile and css 320 $this->css = 'mprint.css'; 321 } 322 } else { 323 $this->css = $this->cssFile; 324 } 325 return $this->css; 326 }

#3347 report it
tydeas_dr at 2011/04/05 06:47am
browser compatibility

Fedora 14 Linux - Opera Version 11.00: Clicking the print button does nothing. - Google chrome 7.0.517.44: Prompt fro print but get a blank page. - Firefox 3.6.15: Works as expected. Windows xp - IExplorer 7: Works as expected

#3124 report it
macinville at 2011/03/17 09:35pm

Hi joblo,

Thanks for that one, I totally missed that. :)

#3122 report it
Joblo at 2011/03/17 06:00pm
Nice extension

Only a small bug: Missing argument 2 for CClientScript::registerCss() mPrint.php on line 89

You have to add an 'id' as first parameter, for example

$asset = Yii::app()->getClientScript()->registerCss('mprintcss',$this->assetsPath. '/'.$this->css);

Thanks, it's very useful for my upcoming 'mongocms'.

Leave a comment

Please to leave your comment.

Create extension