4

I'm implementing printing in an iOS app. I have a print proxy app on a Mac for a physical test printer, which is a US Letter printer. The Printer Simulator that ships with the iOS simulator also appears to default to US Letter.

Anyone have suggestions on how I can test for other default paper sizes?

Thanks!

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204

3 Answers3

6

If I've understood the iOS printing features aright -- and that's a big if - then the Print Simulator will adjust to US Letter or A4 depending on the dimensions of what you send. So, if you send it something A4 sized it will select A4 and if you send it something US Letter sized, it will select US Letter.

However, I found with actual printing, this didn't always work. To get that right, I found it made a different to explicitly encourage iOS to select an A4 paper size. This is done in the printInteractionController:choosePaper: method in the UIPrintInteractionController delegate (see code below). If anyone out there understands this better, please post.

More generally (and going off question a bit...) the approach I've taken to printing is to have an app setting for US Letter or A4. When the app first starts up it checks whether it is the US (by testing [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode]) and if so, chooses US Letter. Otherwise it's A4. The user can change this. (I let the user export PDFs, so, say, if she's a UK user sending PDFs to US pals, she can make them look right.)

To print, I first create an A4 or US Letter PDF. Apple's sample code walks you through this for the US Letter size. For A4 you need to set the dimensions of the PDF to around 595.4 x 841.7. (For working with other sizes, note that these numbers are in points, where point density is 72dpi, i.e. you take the size in inches and multiply by 72.) I then use setPrintingItem: on the UIPrintInteractionController with an NSURL for the PDF.

I have found the iOS printing documentation quite challenging, so it's not unlikely that a simpler/better/more robust approach to tackling A4/US Letter etc. is available. But hopefully, some of what I've written here is of help.

Delegate code

// code in the UIPrintInteractionController delegate
// The [Shared settings] object returns paperWidth and paperHeight depending on the app wide A4 or US Letter setting
- (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray *)paperList
{
    CGSize pageSize = CGSizeMake([[Settings shared] paperWidth], [[Settings shared] paperHeight]);
    return [UIPrintPaper bestPaperForPageSize:pageSize withPapersFromArray:paperList];
}

Creating PDF code

UIGraphicsBeginPDFContextToFile(CGRectMake(0.0, 0.0, [[Settings shared] paperWidth], [[Settings shared] paperHeight]), nil);
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0.0, 0.0, [[Settings shared] paperWidth], [[Settings shared] paperHeight]), nil);

// create the PDF

UIGraphicsEndPDFContext();
Obliquely
  • 7,002
  • 2
  • 32
  • 51
1

It is quite easy! On the printer simulator window on your Mac, you'll find a "Load paper" icon. Thus you can choose the default paper size for any of the simulated printer. You can define an alternate size if the printing format of your app document is not available!

Denis
  • 775
  • 7
  • 22
0

As per Apple's documentation:

If the output type is UIPrintInfoOutputPhoto, the default paper size is 4x6 or A6 or some other standard size, depending on locale; if the output type is UIPrintInfoOutputGeneral or UIPrintInfoOutputGrayscale, the default paper size is US Letter (8 1/2 by 11 inches) or A4 or some other standard size, depending on locale.

So, i'm guessing that if you change the locale, you should be able to test the print for A4.

Mustafa
  • 20,504
  • 42
  • 146
  • 209