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.
Version ¶
1.1r1
Requirements ¶
Yii 1.1 or above jQuery
Usage ¶
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.
Example ¶
<?php
$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
'.summary',
'.search-form'
),
'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
));
?>
Changelogs ¶
1.1r1
- appended the id in registerScript so as to work on multiple print links on the page (thanks tommo)
1.1
- 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
1.0
- 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)
0.1.0
- 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
0.0.6
- 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.
0.0.5
- 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'
0.0.4
- added the property 'visible' (bool), which dicates the visibility of the printing link
- modified some lines of codes to make it more clearer
0.0.3
- 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
0.0.2
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).
0.0.1
- initial release
Resources ¶
JQuery print code came from: http://www.bennadel.com
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'.
Thanks
Hi joblo,
Thanks for that one, I totally missed that. :)
browser compatibility
Fedora 14 Linux
Windows xp
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 }
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 }
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
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?
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:
$("#tab-to-be-printed").addClass("mprint");
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.
Thanks!
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.
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.
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, '
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.
Two problems
Hi again,
I've run into two problems. Smaller and bigger one.
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.
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?
mPrint does not work with chrome , safari
Is there a solution for Chrome / Safari ? the print option opens a blank page.
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.
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.
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
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 </div> <?php $this->widget('application.extensions.print.printWidget', array( 'printedElement'=>'#idcard',)); ?> <div id="test"> test data </div> <?php $this->widget('application.extensions.print.printWidget', array( 'printedElement'=>'#test',)); ?>
How it can solve?
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.