2

我使用Apple打印文档示例从iPhone打印PDF到Airprint兼容打印机。但是我没有从iPhone上获得完整的页面打印,请查看我所得到的图片,我需要pdf以全纸长度,宽度打印。UIPrintInteractionController不能打印到iPhone中的整个页面

enter image description here

我使用打印功能的代码如下,其相同的代码,如苹果的印刷样本应用程序,

UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController]; 
    if(!controller){ 
     NSLog(@"Couldn't get shared UIPrintInteractionController!"); 
     return; 
    } 

    UIPrintInteractionCompletionHandler completionHandler = 
    ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) { 
     if(!completed && error){ 
      NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code); 
     } 
    }; 

    controller.delegate = self; 

    // Obtain a printInfo so that we can set our printing defaults. 
    UIPrintInfo *printInfo = [UIPrintInfo printInfo]; 
    // This application produces General content that contains color. 
    printInfo.outputType = UIPrintInfoOutputGeneral; 
    // We'll use the URL as the job name. 
    printInfo.jobName = [_filePath lastPathComponent]; 
    // Set duplex so that it is available if the printer supports it. We are 
    // performing portrait printing so we want to duplex along the long edge. 
    printInfo.duplex = UIPrintInfoDuplexLongEdge; 
    // Use this printInfo for this print job. 
    controller.printInfo = printInfo; 
    controller.printingItem = myData; 
    // Be sure the page range controls are present for documents of > 1 page. 
    controller.showsPageRange = YES; 

    // This code uses a custom UIPrintPageRenderer so that it can draw a header and footer. 
    APLPrintPageRenderer *myRenderer = [[APLPrintPageRenderer alloc] init]; 
// UIPrintPageRenderer *myRenderer = [[UIPrintPageRenderer alloc] init]; 
    // The APLPrintPageRenderer class provides a jobtitle that it will label each page with. 
// myRenderer.jobTitle = printInfo.jobName; 
    // To draw the content of each page, a UIViewPrintFormatter is used. 
    UIViewPrintFormatter *viewFormatter = [docWebView viewPrintFormatter]; 

#if SIMPLE_LAYOUT 

    UIFont *font = [UIFont fontWithName:@"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT]; 
    CGSize titleSize = [myRenderer.jobTitle sizeWithFont:font]; 
    myRenderer.headerHeight = myRenderer.footerHeight = titleSize.height + HEADER_FOOTER_MARGIN_PADDING; 
#endif 
    [myRenderer addPrintFormatter:viewFormatter startingAtPageAtIndex:0]; 
    // Set our custom renderer as the printPageRenderer for the print job. 
    controller.printPageRenderer = myRenderer; 

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 
     [controller presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler]; // iPad 
    } 
    else 
    { 
     docWebView.hidden = YES; 
     [controller presentAnimated:YES completionHandler:completionHandler]; // iPhone 
    } 
+0

请问您可以发布一个PDF文件的链接吗? –

+0

Pdf文件在本地保存在手机中的文档目录中。 –

+0

你可以把它上传吗?可能是文档创建方式的问题? –

回答

4

我生成PDF通过绘制到打印完整的网页PDFcontext。我设置了这样一个电话:

NSMutableData *pdfData = [NSMutableData data]; 
UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil); 

注意,因为我在CGRectZero通过了规模的背景下,在其默认大小

/* 
    default pdf.. 
    8.5 X 11 inch 
    612 x 792 
    */ 

**这仅仅是为72dpi,但它的默认值。我有一招,我拉来提高分辨率前缩小:

for (int page = 0 : page < numberPages : page ++){ 
UIGraphicsBeginPDFPage(); 
CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 
CGFloat scaleFactor = 3.0; 
CGContextSaveGState(pdfContext); 
CGContextScaleCTM(pdfContext, 1.0/scaleFactor, 1.0/scaleFactor); 


///draw stuff 
/// context size is now (612.0 * 3.0 , 792.0 * 3.0) , i.e. resolution 72.0 * 3.0 dpi.. 
// 


CGContextRestoreGState(pdfContext); 

}//page drawing loop 


NSString *filePath = [[NSFileManager defaultManager] //etcetc make a path]; 
BOOL success = [pdfData writeToFile:filePath atomically:YES]; 

确定,所以现在我有我的PDF我直接打印到UIPrintInteractionController

NSData *pdfData = [NSData dataWithContentsOfFile:[[self pdfView]filePath]]; 
    if ([UIPrintInteractionController canPrintData:pdfData]) { 

    UIPrintInteractionController *pr = [UIPrintInteractionController sharedPrintController]; 

    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = 
    ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) { 
     if (!completed && error) NSLog(@"Print error: %@", error); 
    }; 

    pr.printingItem = pdfData; 
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { 
     [pr presentFromRect:tool.frame inView:self.view animated:YES completionHandler:completionHandler]; 
    } else { 
     [pr presentAnimated:YES completionHandler:completionHandler]; 
    } 
    } 

请注意,我不做手脚UIPrintPageRenderer。我没有这门课的经验,我从来没有必要去研究它。但在我看来,如果它为一个页眉和一个页脚腾出空间,那么这些就必然会缩小你的文档以腾出空间。尝试将您的页眉和页脚合并到文档本身中。祝你好运

1
bestPaperForPageSize:withPapersFromArray: 

返回UIKit的决定是最好的基于给定的页面大小和特定于打印机的纸张尺寸,成像区域组合的打印作业的打印纸对象。

+ (UIPrintPaper *)bestPaperForPageSize:(CGSize)pageSize withPapersFromArray:(NSArray *)paperList 

UIPrintInteractionController的代表可以在其实施在UIPrintInteractionControllerDelegate协议中声明的方法printInteractionController:choosePaper:的调用此方法。

0

而不是 UIViewPrintFormatter * viewFormatter = [docWebView viewPrintFormatter]; 使用 viewPrintFormatter * viewFormatter = [[UIPrintFormatter alloc] init];

不需要pdf和图片的特定格式化程序