Preview a PDF from remote URL?

I'm developing an app that amongst other things, will have a library of PDF documents to view.


The PDF's are stored remotely and I use SwiftJSON/Alamofire to call the API and receive the filepath as a string. Currently, a user clicks an "Open" button and they are directed to the PDF in Safari on their device.


What I'd like to do on the view controller offering the details of the PDF, is to display the first page (or specify a page) to be used as almost like a UIImage on the screen, giving the user a snapshot of the PDF.


No PDF's are stored in the bundle to prevent application updates to change a simple PDF etc.


Any help is appreciated!

You could use PDFKit (iOS 11 only), WebKit, or DocumentInteractionController to display the document in your app. I have limited experience with DocumentInteractionController, no experience with the others.


I did the following in an older app to get a UIImage of a page from a local PDF file. It was originally done in Xcode 6 or 7 with Objective C, now in Xcode 8.3.2 with Swift, so it may need tweaks for Xcode 9 / Swift 4. It still works, but there might be a much simpler way to do the same thing now.


if let pdfDocument = CGPDFDocument(fileURL as CFURL) {
    if let pdfPageForThumbnail = pdfDocument.page(at: 1) {
        if let pdfPageDict = pdfPageForThumbnail.dictionary {
            var pdfThumbStream : CGPDFStreamRef? = nil
            var thumbnailExistsInPDF = CGPDFDictionaryGetStream(pdfPageDict, "Thumb", &pdfThumbStream)
            if thumbnailExistsInPDF {
                var pdfThumbFormat = CGPDFDataFormat.raw
                if let pdfThumbData = CGPDFStreamCopyData(pdfThumbStream!, &pdfThumbFormat) {
                    thumbImage = UIImage(data: pdfThumbData as Data)
                    if thumbImage == nil {
                        thumbnailExistsInPDF = false
                    }
                }
            }
            if !thumbnailExistsInPDF {
                let pdfPageRect = pdfPageForThumbnail.getBoxRect(.cropBox)
                UIGraphicsBeginImageContext(pdfPageRect.size)
                if let pdfContext = UIGraphicsGetCurrentContext() {
                    pdfContext.translateBy(x: pdfPageRect.minX, y: pdfPageRect.maxY)
                    pdfContext.scaleBy(x: 1.0, y: -1.0)
                    pdfContext.translateBy(x: -(pdfPageRect.origin.x), y: -(pdfPageRect.origin.y))
                    let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
                    let rgbaComponentsArray: [CGFloat] = [255.0, 255.0, 255.0, 1.0]
                    if let whiteColor = CGColor(colorSpace: rgbColorSpace, components: rgbaComponentsArray) {
                        pdfContext.setFillColor(whiteColor);
                        pdfContext.fill(pdfPageRect);
                        pdfContext.drawPDFPage(pdfPageForThumbnail);
                        thumbImage = UIGraphicsGetImageFromCurrentImageContext();
                        UIGraphicsEndImageContext();
                    }
                }
            }
        }
    }
}
                 

You could also store (or build on the fly) a thumbnail on your server and add a function to your API to retrieve the thumbnail.

Preview a PDF from remote URL?
 
 
Q