iosswiftpdfkitpdfview

PDFKit - PdfView won't be RTL without usePageViewController()


I want to display PDF with horizontal, pages from right to left, page by page continuously, and users pinch one page will make all pages the same scale synchronized.

I wrote the codes as below:

        if let pdfDocument = PDFDocument(url: pdfUrl) {
            pdfView.document = pdfDocument
            pdfView.displayMode = .singlePageContinuous
            pdfView.displaysRTL = true
            pdfView.autoScales = true
            pdfView.autoresizesSubviews = true
            pdfView.displayDirection = .horizontal
            pdfView.displaysPageBreaks = true
            pdfView.pageBreakMargins = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            
            pdfView.maxScaleFactor = 4.0
            pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
        }

But displaysRTL will not work, and default scale will not show whole single page but fit to the PDF page height (page right side will out of screen).

If I add

pdfView.usePageViewController(true)

PDF displayRTL works, and every page will fit screen size by default. BUT this will make PDF not displayed page by page, and zoom in scale will not the same with other pages.

result after adding usePageViewController(true)

The left page scale is not the same with current zoom in page on the right side. I need them all the same scale if user pinch a page.

Is there any way to fix this problem?

In addition, every page's top-left corner will display an icon, I don't know what is it mean and how to hide it..


Solution

  • I find a way that make it displayRTL manually.
    Don't use usePageViewController(true), and just reordering the pages from the PDFDocument.

    let doc = PDFDocument()
    var getPage = pdfDocument.pageCount - 1
    for i in 0..<pdfDocument.pageCount {
        guard let pageRef = pdfDocument.page(at: getPage) else { fatalError() }
        doc.insert(pageRef, at: i)
        getPage -= 1
    }
    pdfView.document = doc
    
    // Display PDF from last page if Right to Left
    if (isPageRTL) {
        pdfView.goToLastPage()
    } 
    

    I found PDFView by Apple will get memory leak and crash if PDF is large in iOS 12 and iOS 13 (iOS 14 seems to be fine). So I have to find other way to display PDF that fit my need.