2013-02-15 30 views
0

我正嘗試使用xcode 4.6從iPad應用創建PDF報告。我知道在模擬器上運行時正在創建一個有效的pdf文件,因爲我可以將其挖出並預覽它。註釋掉的代碼執行此操作。問題是,我無法將它寫在我可以在iPad上看到的地方。存儲在IOS6.1上爲iPad生成的PDF

我試過使用UIGraphicsBeginPDFContextToData來代替,並試圖將圖像寫入PhotoAlbum。這裏的問題是,當我將NSMutableData轉換成圖像時,它返回nil。

這是代碼。感謝你給與我的幫助。

- (IBAction)makePDF:(UIButton *)sender 
{ 
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, (CFStringRef)self.labelCopyright.text, NULL); 

    if (currentText) 
    { 
     CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); 
     if (framesetter) 
     { 
//   NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, //NSUserDomainMask, YES) objectAtIndex:0]; 
//   NSString *pdfPath = [rootPath stringByAppendingPathComponent:@"Nick.pdf"]; 
//   NSLog(@"pdf is at %@",pdfPath); 
//   UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil); 
      NSMutableData *data = [[NSMutableData alloc] initWithCapacity:100000]; 
      UIGraphicsBeginPDFContextToData(data, CGRectZero, nil); 
      CFRange currentRange = CFRangeMake(0, 0); 
      NSInteger currentPage = 0; 
      BOOL done = NO; 

      do 
      { 
       UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil); 
       currentPage++; 
//    [self drawPageNumber:currentPage]; 

       currentRange = [self renderPage:currentPage withTextRange:currentRange andFramesetter:framesetter];    
       if (currentRange.location == CFAttributedStringGetLength((CFAttributedStringRef)currentText)) done = YES; 
      } 
      while (!done); 
      UIGraphicsEndPDFContext(); 
      UIImage* image = [UIImage imageWithData:data]; 
      assert(image); 
      UIImageWriteToSavedPhotosAlbum(image, self, nil, nil); 
      CFRelease(framesetter); 
     } 
     else NSLog(@"Could not create the framesetter needed to lay out the atrributed string."); 
     CFRelease(currentText); 
    } 
    else NSLog(@"Could not create the attributed string for the framesetter"); 
} 

- (CFRange)renderPage:(NSInteger)pageNum withTextRange:(CFRange)currentRange andFramesetter:(CTFramesetterRef)framesetter 
{ 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); 
    CGRect frameRect = CGRectMake(72, 72, 468, 648); 
    CGMutablePathRef framePath = CGPathCreateMutable(); 

    CGPathAddRect(framePath, NULL, frameRect); 
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); 

    CGPathRelease(framePath); 
    CGContextTranslateCTM(currentContext, 0, 792); 
    CGContextScaleCTM(currentContext, 1.0, -1.0); 
    CTFrameDraw(frameRef, currentContext); 
    currentRange = CTFrameGetVisibleStringRange(frameRef); 
    currentRange.location += currentRange.length; 
    currentRange.length = 0; 
    CFRelease(frameRef); 

    return currentRange; 
} 
+0

好吧,排序的更新。看來UIMage不支持PDF,因此這解釋了我的示例代碼的倒數第二行中未能轉換的問題。我仍然希望創建一個可以在iPad上使用並在其他地方使用的pdf,而不必涉及任何服務器端的東西,如果poss。 – Nick 2013-02-16 12:42:36

回答

0

的可變數據保存到文件目錄

[data writeToFile:filePath atomically:YES]

下面是一個例子:

+(void) saveData: (NSData*) data ToFileName: (NSString*) filename { 

    // Retrieves the document directories from the iOS device 
    NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); 
    NSString* documentDirectory = [documentDirectories objectAtIndex:0]; 
    NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent: filename]; 

    // instructs the mutable data object to write its context to a file on disk 
    [data writeToFile:documentDirectoryFilename atomically:YES]; 
    //NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); 
} 

至於設備上顯示生成的PDF中,UIWebView對象支持加載PDF來自NSData的文件。這裏有一個例子:

[self.webView loadData:pdfData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil]; 

是可能的一個NSData對象附加到電子郵件以及。這裏是一個例子:

//Check if we can send e-mails 
if ([MFMailComposeViewController canSendMail]) { 

    //Create the Email view controller 
    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init]; 
    controller.mailComposeDelegate = self; 

    //Set the subject and body 
    [controller setSubject:@"Email Subject"]; 
    [controller setMessageBody:@"Email body" isHTML:NO]; 

    //Set the email address 
    [controller setToRecipients:@"[email protected]"]; 

    //Add the current PDF as an attachment 
    NSString *fileName = @"file.pdf"; 

    [controller addAttachmentData:self.retrievedPDF mimeType:@"application/pdf" fileName:fileName]; 

    // show the email controller modally 
    [self.navigationController presentModalViewController: controller animated: YES]; 

} 
0

取而代之的是PDF寫入一個NSMutableData對象,把它寫使用UIGraphicsBeginPDFContextToFile文件。

第一個參數是文件路徑。最好的地方是Documents目錄。有那麼多種不同的方式來獲取文件退出應用:

  1. iTunes的文件共享
  2. 電子郵件
  3. 的iCloud
  4. 發送到第三方服務器(Dropbox的,箱,谷歌驅動器等)
  5. 使用UIDocumentInteractionController在另一個iOS應用程序中打開。