jQuery printMe2 plugin


In some case, we cant use plugin printMe to print because of popup blocker, so how to use it, i create a new version call: printMe2

Homepage : printMe
http://www.daniarlandis.es/printMe/
https://github.com/menusal/printMe

And here the new code

jQuery.fn.printMe2 = function(options){

	// Setup options
	var settings = $.extend({
		// Defaults options.
		path: [],
		title: "",
		head: false,
        type: 'iframe'
	}, options );
	
	// Set the properties and run the plugin
	return this.each(function(){
		
		// Store the object
		var $this = $(this);
        
        if(settings.type=='popup'){

    		var w = window.open();
    		w.document.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
    		w.document.write( "<html>" );
    		w.document.write( "<head>" );
    		w.document.write( "<meta charset='utf-8'>" );
    
    		// Add the style sheets
    		for(i in settings.path){
    			w.document.write('<link rel="stylesheet" href="'+settings.path[i]+'">');
    		}
    		
    		// Close the head
    		w.document.write('</head><body>');
    
    		// Add a header when the title not is empty
    		if (settings.title != "")
    			w.document.write( "<h1>" + settings.title + "<\/h1>" );
    
    		// Add a content to print
    		w.document.write( $this.html() );
    		w.document.write('<script type="text/javascript">function closeme(){window.close();}setTimeout(closeme,50);window.print();</script></body></html>');
    		w.document.close();
        }else{
            var w = document.createElement('iframe');
            document.body.append(w)
    		w.contentDocument.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
    		w.contentDocument.write( "<html>" );
    		w.contentDocument.write( "<head>" );
    		w.contentDocument.write( "<meta charset='utf-8'>" );
    
    		// Add the style sheets
    		for(i in settings.path){
    			w.contentDocument.write('<link rel="stylesheet" href="'+settings.path[i]+'">');
    		}
    		
    		// Close the head
    		w.contentDocument.write('</head><body>');
    
    		// Add a header when the title not is empty
    		if (settings.title != "")
    			w.contentDocument.write( "<h1>" + settings.title + "<\/h1>" );
    
    		// Add a content to print
    		w.contentDocument.write( $this.html() );
    		w.contentDocument.write('<script type="text/javascript">window.print();</script></body></html>');
    		setTimeout(function(){w.remove()},1000)
        }
	});
}    

We use type=iframe, so can print in the case below.

Example usage:

$('#printinvoice').printMe2()

Related Post

,

No comments yet.

Leave a Reply