• How To?
  • Tips ‘n Tricks
  • WordPress
  • Snippets
  • Software
    • Browsers
    • Downloads
  • Web
  • Tools
    • Character Counter
    • chmod Calculator
    • Entities Encoder
    • Live HTML Editor
    • My IP
  • Contact
Twitter Facebook Google+ RSS
You are here: SumTips » Blogging » Give Your Site Visitors a Quick Print Preview Option, Ctrl+P Hotkey

Give Your Site Visitors a Quick Print Preview Option, Ctrl+P Hotkey

Posted on May 14, 2012 by Renji | Short URL: http://sumtips.com/?p=6512

jQuery Print Preview plugin provides your site visitors a quick, instant print preview of the active page before actually printing. The preview can be invoked using a button on the page or by pressing Ctrl+P keyboard shortcut. You can see the plugin in action here.

jQuery Print Preview
Implementing Print Preview

Prerequisites:

  • Print stylesheet: If your website already has a print.css stylesheet that would do, if not, create one and link it in your page header using this code:
<link rel="stylesheet" href="/path-to/print.css" type="text/css" media="print" />
  • jQuery: If you have a copy of jQuery on your server, use that, or else use the below code to get an online minifed copy:
<script src="http://cdn.jquerytools.org/1.2.5/full/jquery.tools.min.js"> </script>

Using the plugin:

print-preview.css and jquery.print-preview.js contains the code that generates the preview. These files can be found inside ‘src’ folder in the downloadable tarball.

<link rel="stylesheet" href="print-preview.css" type="text/css" media="screen">
<script src="jquery.print-preview.js" type="text/javascript" charset="utf-8"> </script>

This is the JavaScript that invokes the preview:

<script type="text/javascript">
    $ (function () {

            // Add a print preview link
            $('#aside').prepend('<a class="print-preview">Print this page</a>');

            // Initialize plugin
            $('a.print-preview').printPreview();

            // Trigger when Ctrl+P is pressed
            $(document).bind('keydown', function(e) {
                var code = (e.keyCode ? e.keyCode : e.which);
                if (code == 80 && !$('#print-modal').length) {
                    $.printPreview.loadPrintPreview();
                    return false;
                }            
            });
    });
</script>

Supported Browsers:

This plugin works with pretty much all browsers.

  • Opera
  • Safari
  • Firefox
  • Google Chrome
  • Internet Explorer 6, 7, 8 and 9

Download: jQuery Print Preview plugin


Tweet

Related posts:

  • Fix Stuck Print Jobs in Windows
  • Hide Gmail Preview Pane Sidebar and Extends Page to Full Screen Width
  • Hotkey To Change Explorer View, Show/Hide Navigation Bar, Detail Bar
  • Save or Print Output from Windows Command Prompt to a Text File
  • Add Pages, Custom Post Types in WordPress Site Feed
Categories: Blogging, Tips 'n Tricks | Tags: jQuery
Hotkey To Change Explorer View, Show/Hide Navigation Bar, Detail Bar
Force Refresh CSS, JavaScript & Any Static File in Browser Cache

One Response to “Give Your Site Visitors a Quick Print Preview Option, Ctrl+P Hotkey”

  1. JC Guzman says:
    June 8, 2012 at 1:06 PM

    Hi! Big thanks for this. Its really awesome!
    However I noticed that the below statement was executed multiple times.

    $(‘head’).append(” +
    ‘@media print {‘ +
    ‘/* — Print Preview –*/’ +
    ‘#print-modal-mask,’ +
    ‘#print-modal {‘ +
    ‘display: none !important;’ +
    ‘}’ +
    ‘}’ +
    ”
    );

    As a result, multiple style tags were appended.

    It would be better if you can assign an ID for this.
    So that you can check if this DOM was already appended.

    Keep it up! :cool:

  • Get Updates via Email

  • Recent Posts

    • CMS2CMS: Migrate Site from Drupal or Joomla to WordPress
    • WordPress: Add Preview Button in Distraction Free Writing
    • How to Activate or Deactivate Individual Jetpack Modules
    • Windows 8: Auto Update Defender with Windows Update Disabled
    • Automatically Start and Close Programs at Specific Time
    • How to Copy Code from Notepad++ with Syntax Highlighting
  • Random Posts

    • Times Square Image Created Over 4 Years with 750,000 Layers
    • Auto Sort & Organize Images Based on Resolution, Aspect Ratio
    • Automatically Change Clipboard Text Case with ClipCase
    • ‘You’ve Got Mail’ Audio Notification for Gmail and Google Apps
    • CSS Attribute Selectors
    • Upload MP3, WAV and M4V Files to YouTube
  • Categories

    • Blogging
    • Games
    • Google
    • How To?
    • Linux
    • Microsoft
      • Windows
    • Miscellaneous
    • Phone
    • Snippets
      • AutoHotkey Snippets
      • CSS Snippets
      • htaccess Snippets
      • JavaScript Snippets
      • PowerShell Snippets
      • WordPress Snippets
    • Social
    • Software
      • Browsers
    • Tips 'n Tricks
    • Wallpapers
    • Web
© SumTips. Contact | Sitemap | Privacy Policy