Click here to Skip to main content
Licence 
First Posted 16 Sep 2003
Views 201,399
Bookmarked 105 times

Printing with Style

By | 23 Sep 2003 | Article
Make your web pages printer-friendly.

Introduction

There're a lot of techniques used on the net to create nice looking prints from web pages. Some take a copy of the pages and remove unwanted elements manually, some do it by opening a new print-friendly page using a server side script (ASP, PHP, Perl, ...). I prefer doing it with style :-)

Style

Doing it with style is simple and elegant; you create a separate style sheet with elements suited for print:

  • Set the background to white (background:#ffffff;)
  • Underline links (text-decoration:underline), and maybe color them blue for users with a color printer
  • Hide unwanted parts using display:none;

You attach the style sheet using:

<head>
<link rel="stylesheet" type="text/css" href="..." media="print" />
</head>

media="print" (IE 4+, Netscape 6+ and Opera 3.62+) tells the browser to use this only when you print the page.

This is how I attach my print.css.

<link rel="stylesheet" type="text/css" 
   href="css/print.css" media="print" />

You can test it by printing one of my pages, if you print my Links page you'll also see how I append external links (IE 6+ only):

body { 
    color : #000000; 
    background : #ffffff; 
    font-family : verdana,arial,sans-serif;
    font-size : 12pt;  
}
h2 {
    font-size : 14pt;
}
a { 
    text-decoration : underline; 
    color : #0000ff; 
}

#logo,#head,#menu,#tail,#printicon, {
    display : none;
}

#appendedlinks {
    page-break-before: always 
}

Append external links

A nice feature is to append all external links in the webpage, to the print document. This is implemented on my pages using the following JavaScript:

// 
// IE only: attach eventhandlers.
// They trigger when a user  prints a web page; 
//     From File->Print, File->Print Preview or
//     by executing a window.print() command 
//
window.onbeforeprint=beforePrint
window.onafterprint=afterPrint

function beforePrint() {
    appendlinks(content);
}

function afterPrint() {
    removelinks(appendedlinks);
}


//
// Generic print function, called from the web page
//
function printContent() {
    if (window.print) {
           window.print();
    } else {
        alert("your browser doesn't support this function")
    }
}


// 
// When appending links to the document, do not 
// append links pointing to the following domains / sites
//

var excludedomains=["soderlind.no", "soderlind.org","localhost"]
var excludedomains=excludedomains.join("|")
rexcludedomains=new RegExp(excludedomains, "i")

//
// appendlinks(id)
// id = id of the part of the web page you want to extract links from
//      ex: document.body
//

function appendlinks(id){
    
    var strD = "<p/>";
    var num = 0;
    if (document.getElementById){
        var links=id.getElementsByTagName("A")    
        var total=links.length
        strD += "<dl id=\"appendedlinks\" border=0>"
        strD += "<dt><h2>Links extracted from the document:</h2></dt>"
        for (i=0;i<total;i++){
            if (links[i].hostname.search(rexcludedomains)==-1 
                && links[i].href.indexOf("http:")!=-1) {
                strD += '<dt>'+links[i].innerText+'</dt>'
                strD += '<dd>'+links[i]+'</dd>'
                num++;
            }
        }
        strD += "</dl>"

        if (id.insertAdjacentHTML && num>0)
            id.insertAdjacentHTML("beforeEnd",strD);
    }
}

//
// removelinks(id)
// After the print job is done, remove the appended links from the document
// id = appendedlinks
//
function removelinks(id) {
    if (document.getElementById){
        id.removeNode(true);
    }
}

Have fun coding (and printing).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Per S

Web Developer

Norway Norway

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralIs it possible to define print preferences in window.print() Pinmembercodrakon2:52 17 Mar '06  
GeneralIE 6.0.2900.2180 PinmemberDregor0:36 18 Dec '05  
Generala lot of great css ebooks in http://www.ebook5.com PinsussAnonymous0:36 12 Aug '05  
a lot of great css ebooks in http://www.ebook5.com;P
GeneralPrintig defined area Pinmembertauchert2:45 26 Apr '05  
GeneralIt was very useful PinmemberTanGkaK14:47 7 Apr '05  
Generalprint landscape & page break w/ Style PinmemberCZIU11:34 5 May '04  
GeneralRe: print landscape & page break w/ Style PinsussQuetininha5:27 28 May '04  
GeneralRe: print landscape & page break w/ Style PinsussAnonymous10:35 29 Jul '04  
GeneralRe: print landscape & page break w/ Style Pinmembermjjvol1:29 21 Sep '04  
GeneralPlease help me~ Pinmembercharcoalc0:50 5 May '04  
GeneralRe: Please help me~ PinsussAnonymous7:14 24 Sep '04  
GeneralPrinting with more Style PinsussPer Søderlind4:43 10 Mar '04  
GeneralRe: Printing with more Style PinsussPer Søderlind4:49 10 Mar '04  
GeneralPrinting without page headers Pinmemberkrixerx20:24 1 Oct '03  
GeneralRe: Printing without page headers PinmemberDheth3:42 2 Oct '03  
GeneralRe: Printing without page headers PinmemberStephen Quattlebaum6:39 16 Oct '03  
GeneralRe: Printing without page headers PinmemberDheth20:46 16 Oct '03  
GeneralRe: Printing without page headers PinsussAnonymous3:21 26 Aug '04  
GeneralRe: Printing without page headers PinsussAnonymous9:44 3 Sep '04  
GeneralRe: Printing without page headers PinmemberIn-Active9:09 6 Apr '05  
GeneralVery useful PinmemberJohn Cardinal15:36 23 Sep '03  
GeneralGreate! And, fun to se some one from Norway in here. PinmemberJonas Follesø9:48 18 Sep '03  
GeneralRe: Greate! And, fun to se some one from Norway in here. PinmemberPer Soderlind14:25 18 Sep '03  
GeneralRe: Greate! And, fun to se some one from Norway in here. PinmemberE.Data Studios13:54 24 Sep '03  
GeneralWell Done PinPopularmemberBlake Coverett8:25 18 Sep '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120528.1 | Last Updated 24 Sep 2003
Article Copyright 2003 by Per S
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid