使用AirPrint打印PDF会导致截断内容

在这里,我打印大小为'pageSize = CGSizeMake(640,832);'的pdf。 这个尺寸是A4大小的页面大小。 所以我会切断一些文本(意味着它不会打印整个页面)。

同时使用MAC打印相同的PDF,它将打印整个页面的选项(规模适合)的帮助。 所以任何一个可以帮助我从这个问题出来..是否有任何select在IOS SDK的规模适合。

这是我的代码

-(void)printItem { NSArray *aArrPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ; NSString *aStr = [[aArrPaths objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"PropertyReport_%d.pdf",self.propertyId]]; // NSString *aStr = [[NSBundle mainBundle] pathForResource:@"TRADUZIONE HELP SECTIONS REV2" ofType:@"pdf"]; NSURL *url=[[NSURL alloc] initFileURLWithPath:aStr]; NSData *data=[[NSData alloc] initWithContentsOfURL:url]; printController = [UIPrintInteractionController sharedPrintController]; if(printController && [UIPrintInteractionController canPrintData:data]) { printController.delegate = self; UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; //printInfo.jobName = [NSString stringWithFormat:@"New Image"]; printInfo.duplex = UIPrintInfoDuplexLongEdge; printController.printInfo = printInfo; printController.showsPageRange = YES; printController.printingItem = data; void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) { if (!completed && error) { //NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code); } }; // aWebViewPDF.hidden=FALSE; [printController presentAnimated:YES completionHandler:completionHandler]; } } 

谢谢!

听起来很奇怪,尝试:

 printController.showsPageRange = NO; 

这似乎启用了自动缩放,但有时会在作业结束时打印一个额外的空白页面。 AirPrint基本上是巫术。

您只有一个选项是scale down到所有PDF页面的printed formatratio ,然后打印出来。 我所做的是将pdf into images所有页面pdf into images并缩放到要求:

 NSString *strFilePath = [[NSBundle mainBundle] pathForResource:@"pdftouch" ofType:@"pdf"]; pic.printingItems = [[self allPagesImageFromPDF:strFilePath] copy]; 

添加所有三种方法:

Convert all PDF pages into UIImages

 -(NSMutableArray *)allPagesImageFromPDF:(NSString *)strPath { NSMutableArray *arrImages = [NSMutableArray array]; CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:strPath]); NSInteger numberOfPages = CGPDFDocumentGetNumberOfPages(pdf); for (NSInteger pageIndex = 1; pageIndex <= numberOfPages; pageIndex++) { UIImage *img = [self getThumbForPage:pdf pageIndex:pageIndex]; [arrImages addObject:[self resizedImage:img]]; } return arrImages; } 

Resize images

 -(UIImage *)resizedImage:(UIImage *)image { UIGraphicsBeginImageContext(CGSizeMake(612, 792)); //A4 size [image drawInRect:CGRectMake(0,0,612, 792)]; //A4 size UIImage *resizedImg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resizedImg; } 

Get UIImage from CGPDFPageRef

 -(UIImage *)getThumbForPage:(CGPDFDocumentRef)pdfRef pageIndex:(int)page_number{ // Get the page CGPDFPageRef myPageRef = CGPDFDocumentGetPage(pdfRef, page_number); // Changed this line for the line above which is a generic line CGRect pageRect = CGPDFPageGetBoxRect(myPageRef, kCGPDFMediaBox); //CGFloat pdfScale = width/pageRect.size.width; //pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale); pageRect.origin = CGPointZero; UIGraphicsBeginImageContext(pageRect.size); CGContextRef context = UIGraphicsGetCurrentContext(); // White BG CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); CGContextFillRect(context,pageRect); CGContextSaveGState(context); // Next 3 lines makes the rotations so that the page look in the right direction CGContextTranslateCTM(context, 0.0, pageRect.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFMediaBox, pageRect, 0, true)); CGContextDrawPDFPage(context, myPageRef); CGContextRestoreGState(context); UIImage *thm = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return thm; } 

编辑 :请参阅打印 – 使用airprint-causes-cut-off-content