Printing

  • Revision slug: Printing
  • Revision title: Printing
  • Revision id: 365545
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

There may be times in which your web site or application would like to improve the user's experience when printing content. There are a number of possible scenarios:

  • You wish to adjust layout to take advantage of the size and shape of the paper.
  • You wish to use different styles to enhance the appearance of your content on paper.
  • You wish to use higher resolution images for a better result.
  • You want to adjust the user experience of printing, such as presenting a specially-formatted version of your content before printing begins.

There may be other cases in which you want to manage the printing process, but these are some of the most common scenarios. This article provides tips and techniques for helping your web content print better.

Using a print style sheet

Add the following to your {{HTMLElement("head")}} tag.

<link href="/path/to/print.css" media="print" rel="stylesheet"/>

Using media queries to improve layout

Detecting print requests

Some browsers (including Firefox 6 and later and Internet Explorer) send beforeprint and afterprint events to let content determine when printing may have occurred. You can use this to adjust the user interface presented during printing (such as by displaying or hiding user interface elements during the print process).

Note: You can also use window.onbeforeprint and window.onafterprint to assign handlers for these events, but using {{domxref("element.addEventListener()")}} is preferred.

Examples

Here are some common examples.

Open and automatically close a popup window when finished

If you want to be able to automatically close a popup window (for example, the printer-friendly version of a document) after the user prints its contents, you can use code like this:

<!doctype html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>JavaScript Window Close Example </title>
  <script type="text/javascript">
    function popuponclick() {
      my_window = window.open("", "mywindow","status=1,width=350,height=150");
      my_window.document.write('<html><head><title>Print Me</title></head>');
      my_window.document.write('<body onafterprint="self.close()">');
      my_window.document.write('<p>When you print this window, it will close afterward.</p>');
      my_window.document.write('</body></html>');  
  }
  </script>
</head>
<body>
  <p>To try out the <code>afterprint</code> event, click the link below to open
  the window to print. You can also try changing the code to use <code>beforeprint</code>
  to see the difference.</p>
  <p><a href="javascript: popuponclick()">Open Popup Window</A></p>
</body>
</html>

{{DOMLiveSample("printevents.html")}}

If you want to be able to print an external page without opening it, you can utilize a hidden {{HTMLElement("iframe")}} (see: HTMLIFrameElement), automatically removing it after the user prints its contents. The following is a possible example which will print a file named externalPage.html:

<!doctype html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>MDN Example</title>
  <script type="text/javascript">
    function printPage (sURL) {
      var oHiddFrame = document.createElement("iframe");
      oHiddFrame.src = sURL;
      oHiddFrame.style.visibility = "hidden";
      oHiddFrame.style.position = "fixed";
      oHiddFrame.style.right = "0";
      oHiddFrame.style.bottom = "0";
      document.body.appendChild(oHiddFrame);
      oHiddFrame.contentWindow.onload = oHiddFrame.contentWindow.print;
      oHiddFrame.contentWindow.onafterprint = function () { document.body.removeChild(oHiddFrame); };
    }
  </script>
</head>

<body>
  <p><span onclick="printPage('externalPage.html');" style="cursor:pointer;text-decoration:underline;color:#0000ff;">Print external page!</span></p>
</body>
</html>
Note: Older versions of Internet Explorer can not print the contents of an hidden {{HTMLElement("iframe")}}.

See also

Revision Source

<p>There may be times in which your web site or application would like to improve the user's experience when printing content. There are a number of possible scenarios:</p>
<ul>
  <li>You wish to adjust layout to take advantage of the size and shape of the paper.</li>
  <li>You wish to use different styles to enhance the appearance of your content on paper.</li>
  <li>You wish to use higher resolution images for a better result.</li>
  <li>You want to adjust the user experience of printing, such as presenting a specially-formatted version of your content before printing begins.</li>
</ul>
<p>There may be other cases in which you want to manage the printing process, but these are some of the most common scenarios. This article provides tips and techniques for helping your web content print better.</p>
<h2 id="Using_a_print_style_sheet">Using a print style sheet</h2>
<p>Add the following to your {{HTMLElement("head")}} tag.</p>
<pre>
&lt;link href="/path/to/print.css" media="print" rel="stylesheet"/&gt;
</pre>
<h2 id="Using_media_queries_to_improve_layout">Using media queries to improve layout</h2>
<h2 id="Detecting_print_requests">Detecting print requests</h2>
<p>Some browsers (including Firefox 6 and later and Internet Explorer) send <code>beforeprint</code> and <code>afterprint</code> events to let content determine when printing may have occurred. You can use this to adjust the user interface presented during printing (such as by displaying or hiding user interface elements during the print process).</p>
<div class="note">
  <strong>Note:</strong> You can also use <a href="/en-US/docs/DOM/window.onbeforeprint" title="DOM/window.onbeforeprint"><code>window.onbeforeprint</code></a> and <a href="/en-US/docs/DOM/window.onafterprint" title="DOM/window.onafterprint"><code>window.onafterprint</code></a> to assign handlers for these events, but using {{domxref("element.addEventListener()")}} is preferred.</div>
<h2 id="Examples">Examples</h2>
<p>Here are some common examples.</p>
<h4 id="Open_and_automatically_close_a_popup_window_when_finished">Open and automatically close a popup window when finished</h4>
<p>If you want to be able to automatically close a <a href="/en-US/docs/DOM/window.open" title="DOM/window.open">popup window</a> (for example, the printer-friendly version of a document) after the user prints its contents, you can use code like this:</p>
<pre class="brush: html">
&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;title&gt;JavaScript Window Close Example &lt;/title&gt;
  &lt;script type="text/javascript"&gt;
    function popuponclick() {
      my_window = window.open("", "mywindow","status=1,width=350,height=150");
      my_window.document.write('&lt;html&gt;&lt;head&gt;&lt;title&gt;Print Me&lt;/title&gt;&lt;/head&gt;');
      my_window.document.write('&lt;body onafterprint="self.close()"&gt;');
      my_window.document.write('&lt;p&gt;When you print this window, it will close afterward.&lt;/p&gt;');
      my_window.document.write('&lt;/body&gt;&lt;/html&gt;');  
  }
  &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;p&gt;To try out the &lt;code&gt;afterprint&lt;/code&gt; event, click the link below to open
  the window to print. You can also try changing the code to use &lt;code&gt;beforeprint&lt;/code&gt;
  to see the difference.&lt;/p&gt;
  &lt;p&gt;&lt;a href="javascript: popuponclick()"&gt;Open Popup Window&lt;/A&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>{{DOMLiveSample("printevents.html")}}</p>
<h3 id="Print_an_external_page_without_opening_it">Print an external page without opening it</h3>
<p>If you want to be able to print an external page without opening it, you can utilize a hidden {{HTMLElement("iframe")}} (see: <a href="/en-US/docs/DOM/HTMLIFrameElement" title="DOM/HTMLIFrameElement">HTMLIFrameElement</a>), automatically removing it after the user prints its contents. The following is a possible example which will print a file named <code>externalPage.html</code>:</p>
<pre class="brush: html">
&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
  &lt;title&gt;MDN Example&lt;/title&gt;
  &lt;script type="text/javascript"&gt;
    function printPage (sURL) {
      var oHiddFrame = document.createElement("iframe");
      oHiddFrame.src = sURL;
      oHiddFrame.style.visibility = "hidden";
      oHiddFrame.style.position = "fixed";
      oHiddFrame.style.right = "0";
      oHiddFrame.style.bottom = "0";
      document.body.appendChild(oHiddFrame);
      oHiddFrame.contentWindow.onload = oHiddFrame.contentWindow.print;
      oHiddFrame.contentWindow.onafterprint = function () { document.body.removeChild(oHiddFrame); };
    }
  &lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;p&gt;&lt;span onclick="printPage('externalPage.html');" style="cursor:pointer;text-decoration:underline;color:#0000ff;"&gt;Print external page!&lt;/span&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<div class="note">
  <strong>Note:</strong> Older versions of Internet Explorer can not print the contents of an hidden {{HTMLElement("iframe")}}.</div>
<h2 id="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/DOM/window.print" title="DOM/window.print"><code>window.print</code></a></li>
  <li><a href="/en-US/docs/DOM/window.onbeforeprint" title="DOM/window.onbeforeprint"><code>window.onbeforeprint</code></a></li>
  <li><a href="/en-US/docs/DOM/window.onafterprint" title="DOM/window.onafterprint"><code>window.onafterprint</code></a></li>
  <li><a href="/en-US/docs/CSS/Media_queries" title="CSS/Media queries">Media queries</a></li>
  <li>{{cssxref("@media")}}</li>
</ul>
Revert to this revision