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.
December 30th, 2006 at 10:58 am
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…
April 3rd, 2008 at 4:53 am
Wow! Thanks! This is exactly what I was looking for!