How to print PDF with actual size in iOS

Here is the problem.

I am using UIPrintInteractionController to print a PDF file. This PDF file size is A4, 595x842 in pt, but it turned out that the printed content was reduced.

The results even vary depending on whether I use the iPhone simulator. When I test the following code in the iPhone simulator, the file I printed has the actual size. But when I test this code on the device (iPad), a problem arose and appeared with the magazine " Deskjet \ 0323520 \ 032series \ 032 [5B73DB] ._ ipp._tcp.local .: Success of the print job with a warning: Job attributes did not match the print document. "

The code I wrote is as follows:

 - (IBAction)printButtonTouched:(UIButton *)sender { NSURL *testFileURL = [self makeTestPDF]; if ([UIPrintInteractionController canPrintURL:testFileURL]) { UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.jobName = testFileURL.path; printInfo.orientation = UIPrintInfoOrientationPortrait; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.duplex = UIPrintInfoDuplexNone; self.myPrinter.printInfo =printInfo; self.myPrinter.printingItem = testFileURL; if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { [self.myPrinter presentFromRect:sender.frame inView:self.view animated:YES completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) { }]; } else { [self.myPrinter presentAnimated:YES completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) { }]; } } } - (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray *)paperList { CGRect mediaBox = CGRectMake(0, 0, 595.276, 841.89); UIPrintPaper *bestPaper = [UIPrintPaper bestPaperForPageSize:mediaBox.size withPapersFromArray:paperList]; return bestPaper; } 

When I start a PDF page in the context of the PDF, my code is as follows:

  CGFloat a4Width = 595.276f; CGFloat a4Height = 841.89f; CGRect mediaBox = CGRectMake(0, 0, a4Width, a4Height); UIGraphicsBeginPDFContextToFile(testPDFURL.path, mediaBox, nil); UIGraphicsBeginPDFPageWithInfo(mediaBox, nil); 

The printer I'm testing on is the HP Deskjet Ink Advantage 3525 .
The same thing happened when I tested another printer that also supports AirPrint , the only difference is the scaling factor.

I am looking for an answer on how to make a PDF with its actual size. And I tried for almost a week to find the answer, but I did not find useful information. Please help me.

+4
source share

Source: https://habr.com/ru/post/1499782/


All Articles