How to display pdf on iOS

Many options, here are 3:

1) The easiest way to load and display a local pdf file is to use a UIWebview like that:

NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

2) You can also use a UIDocumentInteractionController/QLPreviewController to display PDF Files natively.

3) Another way would be to build a custom PDF Viewer, as in apples ZoomingPDFViewer example code. (using UIPageViewController + CATiledLayer + UIScrollView)


I advise you using the QuickLook framework while handling PDF files.


You can use a UIWebView to get the PDF from your bundle directly, no need to call an online web service.

Local File:

NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
[self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];

Remote File:

- (void) loadRemotePdf
    {
       CGRect rect = [[UIScreen mainScreen] bounds];
       CGSize screenSize = rect.size;
        
       UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
        webView.autoresizesSubviews = YES;
            webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);

       NSURL *myUrl = [NSURL URLWithString:@"http://www.mysite.com/test.pdf"];
       NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
            
       [webView loadRequest:myRequest];

       [window addSubview: myWebView];
       [myWebView release];
            
}

Tags:

Ios

Pdf

Uiwebview