【问题标题】:Printing Pdf using AirPrint causes cut-off content使用 AirPrint 打印 Pdf 会导致内容被截断
【发布时间】:2014-03-12 19:34:37
【问题描述】:

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

在使用 MAC 打印相同的 pdf 时,它将在选项的帮助下打印整个页面(缩放以适应)。所以任何人都可以帮助我摆脱这个问题.. 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];
}

}

谢谢!

【问题讨论】:

    标签: ios iphone objective-c uiprintpagerenderer uiprintinteractioncntrler


    【解决方案1】:

    听起来很奇怪:

    printController.showsPageRange = NO;
    

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

    【讨论】:

    • 对你的“巫术”评论大笑。
    • 难以置信。设置printController.showsPageRange = YES; 解决了我的问题。谢谢!
    【解决方案2】:

    您只有一个选择,即从 PDF 的所有页面中将scale down 转换为printed formatratio,然后打印。我所做的是得到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;  
    }
    

    编辑:参考printing-uiimage-using-airprint-causes-cut-off-content

    【讨论】:

    • 感谢您的回复。但问题是页面大小应该根据其页面格式化程序(如 A4、A5 等)而有所不同。所以我们无法调整单页类型的图像大小。所以请建议我根据打印页面格式选择自动缩放。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    相关资源
    最近更新 更多