Koh
Koh

Reputation: 2897

Swift PDFKit: Unable to scale view to a small size

I am attempting to display a pdf document on a pdfView and would like it to scale such that I can see the full content view within the bounds of the device. However, I am unable to scale it small enough to do so. I have attempted some solutions on SO but still doesn't work.

My implementation so far:

let pdfView: PDFView = {
  let v = PDFView()
  v.translatesAutoresizingMaskIntoConstraints = false
  return v
}()

override func viewDidLoad() {
  super.viewDidLoad()

  //addSubview and autolayout constraints to superview

  if let url = Bundle.main.url(forResource: "documentName", withExtension: "pdf"), let document = PDFDocument(url: url) {
    pdfView.document = document

    guard let page = document.page(at: 0) else {return}
    let pageSize = page.bounds(for: .cropBox)
    let screenSize = UIScreen.main.bounds
    let scale = screenSize.width / pageSize.width
    pdfView.scaleFactor = scale
  }
}

Note that these are things I have tried but doesn't work:

pdfView.autoScales = true
pdfView.displayMode = .singlePage
pdfView.minScaleFactor = // to a very small value

I also attempted to iterate the scaleFactor manually by setting 0.01 but realised that the view will not scale lower than 0.25. Is there a way for me to scale even lower? The order of scaling in my appliction is in the order of 0.1-0.2. Thanks.

Upvotes: 1

Views: 2418

Answers (2)

Patrick Lawler
Patrick Lawler

Reputation: 1

Here's what worked for me. After you set your document, then set the scale factor to what you want. The issue I was having is even though PDFDocument(url:) is not an Async func, if you're getting a document from the web you should get the document before you set document to the viewer. Here's my work around. I used the method to fit the pdf to the screen, but you can use a scale factor also.

override func viewDidLoad() {
        super.viewDidLoad()
        configureView()
        getDocumentFromUrl { document in
            DispatchQueue.main.async { [weak self] in
                guard let self = self else { return }
                self.spinner.stopAnimating()
                guard let document = document else { return }
                self.pdfView.document = document
                self.pdfView.scaleFactor = pdfView.scaleFactorForSizeToFit
            }
        }
      
    }
func getDocumentFromUrl(completion: @escaping(PDFDocument?) -> Void) {
        DispatchQueue.global().async {
            if let url = URL(string: self.urlString) {     completion(PDFDocument(url: url)) }
            else { completion(nil) }
        }
    }

Upvotes: 0

qtngo
qtngo

Reputation: 1679

If I remember correctly, PDFKit seems a bit buggy concerning autoScales. PDFView must be set up in the correct order. You can try:

  1. Add PDFView in subview in viewDidLoad
  2. Set up other PDFView properties, like displayMode, displayDirection, and set up constraint for PDFView in viewWillAppear
  3. Finally, set pdfView.document = document then pdfView.autoScales = true

This setup order work for me.

I don't have answer for your next question. But it could also be related to the setup order. Let me know if it works for you.

Hope this helps.

Upvotes: 3

Related Questions