How to display pdf on iOS

87,021

Solution 1

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];
            
}

Solution 2

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)

Solution 3

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

Solution 4

SWIFT 4.x

UIWebview approach in swift:

//let url = URL.init(string: "https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf")! // for file in web
let url = Bundle.main.url(forResource: "filename", withExtension: ".pdf")! // for file in bundle
let webView = UIWebView.init(frame: view.frame)
webView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
webView.loadRequest(URLRequest.init(url: url))
view.addSubview(webView)

Solution 5

There is a new toolkit IOs has introduced. It has many dedicated features related to displaying PDFs. You can use PdfView class in it to display a PDF either from a local file or via a URL.

PDFKit > PDFView

Share:
87,021
Yang Jie Domodomo
Author by

Yang Jie Domodomo

Newbie to everything, please guide me ^.^

Updated on December 17, 2020

Comments

  • Yang Jie Domodomo
    Yang Jie Domodomo over 3 years

    I want to know if there is any way for a offline build for xcode iOS such that we can display pdf file from local file.

    The method I'm using now is via UIWebView to display a custom HTML which contain the PDF URL (online) , but I want to make an offline version if possible.

    But from online research, it seems to me the only way to display PDF is via UIWebView, if that is really the case, is it possible to pull the file from local resource (e.g. add files to my xcode resource folder) rather than using the PDF URL.

    OFF-TOPIC: Which would be a better choice? To pull local file or to use the online URL for PDF file?

    As UIWebView require connection, it would seem to be a better choice to just pull the online URL as it will be the most current and updated.

    Feedback is much appreciated.

    EDIT: quick question, if I use the UIDocumentInteractionController method or the QuickLook framework to make it offline, this would mean I have to release update patch each time there is new PDF article added in (correct me if I'm wrong)

    Taking this point into consideration, I've come up with a counter-argument to my own question on the spot (Sorry if any of you feel you are wasting time on me >.<, I am a person who like to question possibilities and the different approach one could take to solve a problem) In the event to save time from the constant updating and providing a real time base solution, which is to use UIWebView (I know this is contradicting my purpose of this SO thread), as this proj is base on a online website (getting my content and sources from there), if I were to load their website for the article section, would it be bold of me to say that by adding a NSString variable, I can add in the NSRange function to the viewDidLoad together with the UIWebView such that the URL will never leave the news section and PDF URL. e.g. using combination of if and NSRange function to counter check that the URL only contain the news section and PDF URL)

    I did once before a NSRange function for point on calculator so I thought it would be a good time to put it in use for this framework app.

    P.S - This app is a organization base project and I'm a intern providing them with the base skeleton framework.