0

Would someone please explain to me why this pdf generator I'm attempting to use is always returning nil? I'm attempting to get a thumbnail to display in a UITableView alongside the filename of the PDF. Unfortunately, out of the four or so thumbnail generators I've tried, none of them have returned anything other than nil.

func uploadPDF() {
        let types = UTType.types(tag: "pdf",
                                 tagClass: UTTagClass.filenameExtension,
                                 conformingTo: nil)
        let documentPickerController = UIDocumentPickerViewController(forOpeningContentTypes: types)
        documentPickerController.delegate = self
        self.present(documentPickerController, animated: true, completion: nil)
    }

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        for url in urls {
            let thumbnail = thumbnailFromPdf(withUrl: url, pageNumber: 0)
            self.modelController.bidPDFUploadThumbnails.append(thumbnail!)
        }
        tableView.reloadData()
    }

func thumbnailFromPdf(withUrl url:URL, pageNumber:Int, width: CGFloat = 240) -> UIImage? {
        guard let pdf = CGPDFDocument(url as CFURL),
            let page = pdf.page(at: pageNumber)
            else {
                return nil
        }

        var pageRect = page.getBoxRect(.mediaBox)
        let pdfScale = width / pageRect.size.width
        pageRect.size = CGSize(width: pageRect.size.width*pdfScale, height: pageRect.size.height*pdfScale)
        pageRect.origin = .zero

        UIGraphicsBeginImageContext(pageRect.size)
        let context = UIGraphicsGetCurrentContext()!

        // White BG
        context.setFillColor(UIColor.white.cgColor)
        context.fill(pageRect)
        context.saveGState()

        // Next 3 lines makes the rotations so that the page look in the right direction
        context.translateBy(x: 0.0, y: pageRect.size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.concatenate(page.getDrawingTransform(.mediaBox, rect: pageRect, rotate: 0, preserveAspectRatio: true))

        context.drawPDFPage(page)
        context.restoreGState()

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }

Generator source: Thumbnail Generator

Christian W
  • 353
  • 2
  • 12

1 Answers1

0

the pdf document starts from page 1 not 0 because its not an array. so simple is

let thumbnail = thumbnailFromPdf(withUrl: url, pageNumber: 1)

you'll get it

rather than using page number you can direct access the thumbnail of by default first page as follow:

import PDFKit

func generatePdfThumbnail(of thumbnailSize: CGSize , for documentUrl: URL, atPage pageIndex: Int) -> UIImage? {
    let pdfDocument = PDFDocument(url: documentUrl)
    let pdfDocumentPage = pdfDocument?.page(at: pageIndex)
    return pdfDocumentPage?.thumbnail(of: thumbnailSize, for: PDFDisplayBox.trimBox)
}
Zeeshan Ahmad II
  • 1,047
  • 1
  • 5
  • 9
  • I tried both of your solutions and still returned nil every time. Is there something wrong with the url? Or perhaps a permission that I need to add somewhere? What doesn't make sense is that they are valid urls... I know this because I also get the name of the pdf from the url, which is working fine. – Christian W Jul 18 '22 at 16:42
  • nothing is wrong with url I have tested it and it works perfectly. – Zeeshan Ahmad II Jul 19 '22 at 03:57