在Mailer中预览PDF

时间:2014-02-12 21:15:42

标签: ios objective-c pdf

我有一些旧代码,我从.xib创建PDF文件。当我通过电子邮件发送从xib生成的PDF文件时,我可以在邮件窗口中预览PDF。

使用下面的代码,我可以从PDF组生成单个PDF文件,我可以通过电子邮件发送,但不会显示PDF页面的预览,这是我想要实现的。

我在文档中找不到任何关于此的内容。有没有办法显示我发送的PDF的预览?

我不想在Web视图中预览PDF,预览应该在邮件视图控制器出现后显示。我可以在邮件视图控制器中预览PDF文件,但这些功能(由于某种原因)停止使用我编写的代码块。

    //wrap all PDF files together into one PDF file
    //*************************************************************


    NSArray     *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString    *documentsDirectory = [paths objectAtIndex:0];

    NSString    *oldFilePath=[documentsDirectory stringByAppendingPathComponent:@"myPDF.pdf"];
    NSURL       *oldFileUrl = [NSURL fileURLWithPath:oldFilePath];

    CGContextRef context = CGPDFContextCreateWithURL((__bridge_retained CFURLRef)oldFileUrl, NULL, NULL);


    for (OBJreport in pages)
    {

        // Get the first page from each source document
        NSString            *pdfPath        = OBJreport.rep_filePath;
        NSURL               *pdfUrl         = [NSURL fileURLWithPath:pdfPath];
        CGPDFDocumentRef    pdfDoc          = CGPDFDocumentCreateWithURL((__bridge_retained CFURLRef)pdfUrl);
        CGPDFPageRef        pdfPage         = CGPDFDocumentGetPage(pdfDoc, 1);
        CGRect              pdfCropBoxRect  = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);

        // Copy the page to the new document
        CGContextBeginPage(context, &pdfCropBoxRect);
        CGContextDrawPDFPage(context, pdfPage);

        // Close the source files
        CGContextEndPage(context);
        CGPDFDocumentRelease(pdfDoc);
    }


    // Cleanup
    //*************************************************************
    CGContextRelease(context);


    //add the final output file to the pages array for cleanup
    //*************************************************************
    OBJ_report *cleanUpOBJreport = [OBJ_report new];


    cleanUpOBJreport.rep_filePath   = [oldFileUrl path];
    cleanUpOBJreport.rep_mimiType   = @"application/pdf";
    cleanUpOBJreport.rep_data       = [NSData dataWithContentsOfURL:oldFileUrl];
    cleanUpOBJreport.rep_fileName   = @"myPDF.pdf";

    [pages addObject:cleanUpOBJreport];




    //mail
    //*************************************************************

    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

    [mailer setMailComposeDelegate:self];
    [mailer setSubject:@"File Attached"];
    [mailer setMessageBody:defaultEmailCoverLetter isHTML:NO];

    //add all pages
    [mailer addAttachmentData:cleanUpOBJreport.rep_data mimeType:@"application/pdf" fileName:@"myPDF.pdf"];
    [self presentViewController:mailer animated:YES completion:nil];


    //cleanup files
    //*************************************************************
    OBJ_report *clean_OBJreport = [OBJ_report new];
    for (clean_OBJreport in pages)
    {
        NSError *error = nil;
        [[NSFileManager defaultManager] removeItemAtPath:clean_OBJreport.rep_filePath error:&error];
    }

2 个答案:

答案 0 :(得分:0)

显示PDF的预览非常容易。您只需要在视图中添加UIWebView,然后让它为您加载PDF预览。

在您的情况下,我们假设您已将UIWebView添加到mailer视图控制器的视图中(或最好添加到任何其他视图中),并将其连接到出口属性previewerWebview中查看控制器。从您的代码中可以看出,您的PDF文件是在类中oldFileUrl变量指向的url处生成的。因此,您可以使用两行简单的代码加载PDF的预览:

NSURLRequest *request = [NSURLRequest requestWithURL:oldFileUrl];
[self.previewerWebview loadRequest:request];

这应该会显示生成的PDF的预览。

答案 1 :(得分:0)

PDF,当超过一页时,将显示为图标而不是预览整个文档。这是默认行为。