【问题标题】:pdf show in simulator but not in iPhonepdf 在模拟器中显示,但在 iPhone 中不显示
【发布时间】:2012-08-25 12:29:01
【问题描述】:

我一直在努力解决过去几个小时的问题。我以编程方式制作了一个 pdf 并将其保存在 ~/library/Application Support/iPhone Simulator/... 并且这些程序在模拟器中对我来说工作正常。我可以在 webView 中保存和检索 pdf。

但是当我在 iPhone4 设备上测试我的应用程序时,我在检索时看不到 pdf 文件。我是新手,所以我不知道手动检查是否在设备中创建了 pdf。

请帮助我。

谢谢

我从这段代码中保存了 pdf:

- (void)generatePdfButtonPressed{

pageSize = CGSizeMake(612, 792);
NSString *fileName = @"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

[self generatePdfWithFilePath:pdfFileName];
}


- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);

NSInteger currentPage = 0;
BOOL done = NO;
do
{
    // Mark the beginning of a new page.
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

    // Draw a page number at the bottom of each page.
    currentPage++;
    //[self drawPageNumber:currentPage];

    //Draw a border for each page.
  //  [self drawBorder];

    //Draw text fo our header.
    [self drawHeader];

    //Draw a line below the header.
    [self drawLine];


    //Draw personel details
    [self drawPersonelDetail];

    //Draw Education
    [self drawEducation];




    //Draw work Experiences
    [self drawWorkExperiences];

    //Draw work Activities
    [self drawActivities];


    //Draw Skills
    [self drawSkils];


    //Draw some text for the page.
 //   [self drawText];

    yCord=550;
//    [self drawText];

    //Draw an image
//    [self drawImage];
    done = YES;
}
while (!done);

// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}

【问题讨论】:

  • 随处添加日志消息,并检查并记录返回码。显然,您在电话上所做的某些事情不起作用,这就是您可以找到那个地方的方法。另外,我不确定您手机上的应用程序中是否预先存在“应用程序支持” - 您可能需要以编程方式创建它(正如我所说,我不确定)
  • @DavidH 感谢您的回复。我想知道。我正在为在我的设备中存储 pdf 进行正确的编码? (在模拟器中运行良好)
  • 另外,您没有展示的是如何存储您生成的文件路径,以及您如何尝试将 PDF 加载到 Web 视图中。
  • 如果您需要帮助,请按照我的建议进行操作,然后发布失败的步骤,然后您将获得帮助。

标签: iphone ios pdf


【解决方案1】:

经过长时间的努力,我找到了自己问题的解决方案:我在下面提供的解决方案可能对其他任何人都有帮助。

- (void)generatePdfButtonPressed{

pageSize = CGSizeMake(612, 792);
NSString *fileName = @"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

[self generatePdfWithFilePath:pdfFileName];
}


- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);

NSInteger currentPage = 0;
BOOL done = NO;
do
{
// Mark the beginning of a new page.
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

// Draw a page number at the bottom of each page.
currentPage++;
[self drawPageNumber:currentPage];

//Draw a border for each page.
 [self drawBorder];

//Draw text  our header.
[self drawHeader];

//Draw a line below the header.
[self drawLine];


//Draw personel details
[self drawPersonelDetail];

//Draw Education
[self drawEducation];


//Draw an image
[self drawImage];

done = YES;
}
while (!done);

// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}



And for Display the pdf file the method is :

- (void)showPdfClicked
{

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

NSLog(@"%@",documentsDirectory);

NSString *path1 =[documentsDirectory stringByAppendingPathComponent:@"Demo.pdf"];

NSLog(@"Retruve Save path is %@: ",path1);
// NSString *path1 = [[NSBundle mainBundle] pathForResource:@"pdf1" ofType:@"pdf"];
NSURL *targetURL1 = [NSURL fileURLWithPath:path1];
NSURLRequest *request1 = [NSURLRequest requestWithURL:targetURL1];
[pdf1View loadRequest:request1];

}

【讨论】:

  • 那么,这段代码和你原来的代码有什么不同呢?什么解决了你的问题?
  • @SixtenOtto 我以前犯过愚蠢的错误,当我创建 PDF 时,我命名为“Demo.pdf”,但是当我访问 pdf 文件时,我使用了“demo.pdf”,所以它没有显示。
【解决方案2】:

我以前遇到过这个问题。我的猜测是内存问题——您尝试加载的 PDF 文件有多大? UIWebView 似乎在缓冲(如果有的话)大文件以供显示时遇到了一些问题。

【讨论】:

    【解决方案3】:

    我通过此代码成功通过电子邮件发送了我创建的 pdf。也许这对你有帮助(dottorfeelgood)。

    -(IBAction)emailButtonClicked
    {
    
    
    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    picker.mailComposeDelegate = self;
    [picker setSubject:@"Subject"];
    
    
    NSString *fileName = @"Demo.pdf";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
    
    
    
    NSMutableData *pdfData = [NSMutableData data];
    
    
    pdfData = [NSData dataWithContentsOfFile:pdfFileName];
    
    CGRect bounds=CGRectMake(0, 0, 612, 792);
    [picker addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"SomeFile.pdf"];
    
    
    
    
    // Fill out the email body text
    NSString *emailBody = @"Image is attached";
    [picker setMessageBody:emailBody isHTML:NO];
    [self presentModalViewController:picker animated:YES];
    
    //[picker release];
    
    
    }
    

    编码愉快!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 2010-11-07
      • 2013-05-13
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多