How to display pdf on iOS

后端 未结 12 1110
半阙折子戏
半阙折子戏 2020-12-12 18:30

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 vi

相关标签:
12条回答
  • 2020-12-12 18:46

    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];
                
    }
    
    0 讨论(0)
  • 2020-12-12 18:49

    I found this site and is a very easy method to open pdf's with any available pdf capable app that is loaded on the iPhone. (I prefer iBooks)

    simple way to present and open a pdf file

    0 讨论(0)
  • 2020-12-12 18:49

    In my case, I have a tableView that segues to a PDFView based on either a selected row or detail indicator (there are two different ViewControllers for each in different parts of my app). I have an @IBOutlet for the PDFView and use this code to open the desired PDF, which is saved in the app:

    @IBOutlet weak var myPDFView: PDFView!
    
    var mySelection = String()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        if let path = Bundle.main.path(forResource: mySelection, ofType: "pdf") {
            let url = URL(fileURLWithPath: path)
    
            if let myDocument = PDFDocument(url: url) {
                myPDFView.document = myDocument
                myPDFView.autoScales = true
                myPDFView.displayMode = .singlePageContinuous
                myPDFView.displayDirection = .vertical
                myPDFView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            }
        }
    }
    

    This all works great. The correct PDFs load easily and fill the screen like I want. However, the PDF is always scrolled up just a little, causing the top portion to be hidden under the navigation bar. Notice in the screen shot below that the top of the document is not visible but the bottom is, just above the tab bar at the bottom. I think this has to do with PDF coordinates starting at the bottom of the page but cannot figure out how to get it to load the very top of the document, so the background at the top is visible.

    Simulator Screen Shot Link

    Perhaps this has to do with autoScales?

    0 讨论(0)
  • 2020-12-12 18:55

    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)

    0 讨论(0)
  • 2020-12-12 18:55

    Apple’s PDFKit framework provides a huge range of code to help us work with PDFs, and one of the most useful is PDFView – it renders PDFs to the screen and lets users interact with them.

    To try it out, start by importing the PDFKit framework:

    import PDFKit
    

    Next, add this code to your viewDidLoad() method to create a PDFView and make it fill all available space:

    let pdfView = PDFView()
    
    pdfView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(pdfView)
    
    pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
    pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    

    Finally, create a URL pointing to a PDF you have in your bundle somewhere (or one in your documents directory), then create a PDFDocument object from that and pass it to the PDF view:

    guard let path = Bundle.main.url(forResource: "example", withExtension: "pdf") else { return }
    
    if let document = PDFDocument(url: path) {
        pdfView.document = document
    }
    

    Done!

    0 讨论(0)
  • 2020-12-12 18:59

    It's late but there is some limitation with UIWebview on loading pdf files, It won't load editable pdf properly. so load any type of pdf files use following code

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"pdfFileName" ofType:@"pdf"]];
    PDFView *view = [[PDFView alloc] initWithFrame:self.view.frame];
    view.document = [[PDFDocument alloc] initWithURL:url];
    [self.view addSubview:view];
    
    0 讨论(0)
提交回复
热议问题