Fixing sIFR Printing with CSS and MooTools

By  on  

While I'm not a huge sIFR advocate I can understand its allure. A customer recently asked us to implement sIFR on their website but I ran into a problem: the sIFR headings wouldn't print because they were Flash objects. Here's how to fix the sIFR printing issue.

Sample XHTML

<h2>Sample Heading 1</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ut lectus fermentum augue bibendum tincidunt. In hac habitasse platea dictumst. Nullam ornare nunc ac massa. Nam volutpat tempor tortor. Maecenas sapien. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum a odio ut libero facilisis tincidunt.</p>

<h2>Sample Heading 2</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ut lectus fermentum augue bibendum tincidunt. In hac habitasse platea dictumst. Nullam ornare nunc ac massa. Nam volutpat tempor tortor. Maecenas sapien. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Vestibulum a odio ut libero facilisis tincidunt.</p>

Just some sample XHTML.

The CSS

@media screen {
	.print-only	{ display:none; }
}
@media print {
	.print-only { display:block; }
	.no-print	{ display:none; }
}

Setting a couple of standard print-related CSS styles.

The MooTools JavaScript

$$('h2').each(function(el,i) {
		//new print-only h2
		 new Element('h2',{
			  text: el.get('text')
		 }).addClass('print-only').inject(el,'before');
		 
		//inject swiff into current h2
		el.addClass('no-print');
		var swiff = new Swiff('sifr450x23.swf',{
			 id: 'sifr-' + i,
			 width: 450,
			 height: 23,
			 container: el,
			 params: {
				  wMode: 'transparent'
			 },
			 vars: {
				  titleText:el.get('text')
			 }
		});
  });
});

We first inject a new H2 element that will be used for print. Then we inject the Swiff into the original H2. Of course you can see where I've added the print-only/no-print CSS classes.

The only browser I found that printed the Flash sIFR objects was Internet Explorer. This solution will work across all browsers.

Recent Features

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    Write Simple, Elegant and Maintainable Media Queries with Sass

    I spent a few months experimenting with different approaches for writing simple, elegant and maintainable media queries with Sass. Each solution had something that I really liked, but I couldn't find one that covered everything I needed to do, so I ventured into creating my...

Incredible Demos

  • By
    Chris Coyier’s Favorite CodePen Demos IV

    Did you know you can triple-heart things on CodePen? We’ve had that little not-so-hidden feature forever. You can click that little heart button on any Pen (or Project, Collection, or Post) on CodePen to show the creator a little love, but you can click it again...

  • By
    Using MooTools to Instruct Google Analytics to Track Outbound Links

    Google Analytics provides a wealth of information about who's coming to your website. One of the most important statistics the service provides is the referrer statistic -- you've gotta know who's sending people to your website, right? What about where you send others though?

Discussion

  1. sIFR itself actually does contain a print stylesheet which hides the Flash movie and shows the alternative text.

  2. I am a little confused too Mark. I have not had a problem using sIFR3 and printing, prints out just fine for me. I know many have the resize and load issues but it is all down to proper use of the styles and setting it up correctly covering everything like line heights etc. Not quite sure Davids Problem here, never run into it not printing before.

  3. My issues were on Firefox 3.1 / Windows XP. The sIFR headings don’t show up which is why I created this script.

  4. Marc

    david, i’m new to sIFR too and i am looking for a better text-replacement alternative.. did you include the line:

    <style type="text/css" media="print">
    @import url("sIFR-print.css");
    </style>
    

    in your heading?

  5. @Marc: Nope, just used the CSS code above.

  6. kst

    I didn’t work on FF 3.0.12 for me. Maybe better to use base64 technique for Firefox?

  7. Antonio

    Another great technique as usual. To be honest, your method is far greater than needing all those unnecessary http requests that comes with the original sIFR implementation, CSS & Javascript files.

    Having said that, that’s why I prefer using sIFR Lite (By Dave @ AllCrunchy.com) and tweaked your script slightly. Would love to turn it into a Moo-ish class but still don’t know how to do it.

    Keep up the great work.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!