Crooked Spin

Programming

October 13, 2006

WebView printing

Printing a WebView seems simple. It responds to print:. So everything’s almost done for you. Just make sure you have the print menu items correctly hooked up to the print: first responder, right? Wrong. Sending the print: message to the WebView will result in only the visible rect being printed. Anything off screen will not print.

To get the entire page to print you need to send print: to the WebView’s documentView: [[[[theWebView mainFrame] frameView] documentView] print:sender]. The entire page will then be printed.

One other gotcha that I had to figure out was printing images. The images are being displayed inside table cells and are defined in the page’s CSS. All CSS images are displayed as background images and a WebView does not print background images by default. You have to turn on the option before sending print:.

WebPreferences* prefs = [theWebView preferences];
[prefs setShouldPrintBackgrounds: YES];

Now all of the images will display and print correctly for me.

  1. Thank you very much! Very helpful for the programmer (me) who would want to print a WebView! And I thought I was being smart using a NSPrintOperation…

    Comment by Dimitri — December 30, 2006 @ 10:58 am
  2. Wow! Thanks! This is exactly what I was looking for!

    Comment by TG — April 3, 2008 @ 4:53 am

Leave a comment

RSS feed for comments on this post. TrackBack URL

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>