Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

PDF Preview in Swift iOS

import UIKit
import PDFKit

class ViewController: UIViewController {

    @IBOutlet var pdfView: PDFView!

    override func viewDidLoad() {
        super.viewDidLoad()

        if let path = Bundle.main.path(forResource: "sample", ofType: "pdf") {
            if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
                pdfView.displayMode = .singlePageContinuous
                pdfView.autoScales = true
                pdfView.displayDirection = .vertical
                pdfView.document = pdfDocument
            }
        }
    }
}
Comment

swift pdf preview image

import PDFKit

func pdfThumbnail(url: URL, width: CGFloat = 240) -> UIImage? {
  guard let data = try? Data(contentsOf: url),
  let page = PDFDocument(data: data)?.page(at: 0) else {
    return nil
  }

  let pageSize = page.bounds(for: .mediaBox)
  let pdfScale = width / pageSize.width

  // Apply if you're displaying the thumbnail on screen
  let scale = UIScreen.main.scale * pdfScale
  let screenSize = CGSize(width: pageSize.width * scale, 
                          height: pageSize.height * scale)

  return page.thumbnail(of: screenSize, for: .mediaBox)
} 
Comment

PREVIOUS NEXT
Code Example
Swift :: ios get device id 
Swift :: add months to date swift 
Swift :: swift inheritance 
Swift :: if else if and else statments in swift language 
Swift :: how to merge 2 arrays in swift 
Swift :: how to bold 1 word swift 
Swift :: swift sort list true before false 
Swift :: Swift Calling a function in Swift 
Swift :: Swift Closures as Function Parameter 
Swift :: swift while loop 
Swift :: Create a Swift Array 
Swift :: swiftui menu 
Swift :: swift dictionary 
Swift :: swiftui image aspect ratio 
Swift :: SwiftCSV and RealmSwift library 
Swift :: Swift Remove an Element from a Set 
Swift :: ring Desktop, WebAssembly and Mobile Using QTableWidget 
Swift :: swift print struct name 
Swift :: Swift Conform Class To Swift Protocol 
Swift :: Swift for vs while loop 
Swift :: Swift Bitwise NOT Operator 
Swift :: swift view controller background image 
Swift :: xib image shown on simulator but not on device 
Swift :: Speech recognizer swiftui 
Swift :: swift animate constraint 
Swift :: Convert struct to JSON string in swift 5 
Swift :: how to delete from list tvos swiftui coredata 
Ruby :: iterate over string ruby 
Ruby :: travis ci NameError: uninitialized constant SimpleCov 
Ruby :: ruby read file 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =