I have an iPad application that uses WebViews to display a list of URL's. I would like to be able to print all of the WebViews in one go, without prompting the user multiple times with the PrintInteractionController. The problem is that the PrintInteractionController does not appear to have the ability to do this. You cannot assign multiple viewFormatters, and the WebViews are not recognized as printItems. There is also no method that I can find to just print the items and not show the PrintInteractionController.
Does anyone know of a way to do this?
Cheers.
This is an old question, but I spent several days searching for an answer and following some misleading comments in the Apple docs and sample code. So, I'm sticking this here to save the next guy the days I wasted.
The specific problem is: How does one write a
UIPrintPageRenderer
that can print multipleUIWebViews
in a single print job given to a UIPrintInteractionController?You can get pretty far along to a solution that doesn't involve converting everything to PDFs first using this Apple sample code: PrintWebView. Also this documentation will help some: UIPrintPageRenderer. The problem is that the sample App and the documentation for
UPPrintPageRenderer
suggest that if you add multipleUIPrintFormatter
via this property:The sample code Apple provided for the method to override
-(NSInteger)numberOfPages
in PrintWebView will just work and figure out the correct page counts.... Nope!What you have to do is add the
printFormatters
in a non-obvious way via the method shown below, and then correct their respective startPage properties and use these collectively to work out the correct pageCount for UIPrintPageRenderer to return. Instead, use this:Why does this works and the other doesn't? No idea, probably a bug.... I should file a radar :) But Thanks to @Mustafa and this other Stackoverflow answer for the hint:
Here are the steps to follow to print multiple UIWebViews in one print job:
UIPrintPageRenderer
to set the properties you want on the webView.viewPrintFormatters you give to it.[addPrintFormatter:startingAtPageAtIndex:]
UIPrintPageRenderer
-(NSInteger)numberOfPages
method get the pageCount from each formatter, and use that to update the startPage and total page count values.Here is the basic code outline to follow (FYI: this solution is working for an App built with a deployment target of iOS 9.0):
Define your UIPrintPageRenderer class like so (you don't really need the
init
method, but in my use case I set values in there I always want that way):In the Controller that handles the print action for the UIWebViews you want to print, do something like this:
And voila! Now... it just works ;)
Use
printPageRenderer
property of your UIPrintIterationController object. You can set multiple UIPrintFormatter subclasses in a UIPrintPageRenderer subclass object.