首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Mailer中预览PDF

在Mailer中预览PDF
EN

Stack Overflow用户
提问于 2014-02-13 05:15:42
回答 2查看 65关注 0票数 0

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

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

我在文档中找不到任何关于这方面的东西。有没有办法显示我正在发送的PDF的预览?

我不想在web视图中预览PDF,预览应该在邮件视图控制器中显示。我可以在邮件视图控制器中预览PDF文件,但该功能(由于某种原因)无法使用我编写的代码块。

代码语言:javascript
复制
    //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];
    }
EN

回答 2

Stack Overflow用户

发布于 2014-02-13 06:40:47

显示PDF的预览是非常容易的。您只需要添加一个UIWebView到您的视图,并让它为您加载PDF预览。

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

代码语言:javascript
复制
NSURLRequest *request = [NSURLRequest requestWithURL:oldFileUrl];
[self.previewerWebview loadRequest:request];

这应该会在适当的位置显示生成的PDF的预览。

票数 0
EN

Stack Overflow用户

发布于 2014-02-14 02:46:37

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21739814

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档