The image is reversed up and down by Swift's PDFKit.

Asked 2 years ago, Updated 2 years ago, 28 views

I am trying to create a PDF using PDFKit in swift4.0.
The PDF itself was easy to create, but when I opened the contents, it turned upside down.
Some specification may be insufficient/extra.Please let me know.

import UIKit
import PDFKit
classViewController:UIViewController {
    override func viewDidLoad(){
        super.viewDidLoad()
    }

    override funcdidReceiveMemoryWarning(){
        super.didReceiveMemoryWarning()
    }

    // PDF and return URL
    func createPDF()->URL{
        let document —PDFDocument=PDFDocument()
        Let page1:PDFPage=PDFPage(image:UIImage(named:"IMG_0115.jpg")!)!
        document.insert (page1, at: document.pageCount)
        let documentsPath=NSSearchPathForDirectoriesInDomains (FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first!

        let url:URL=URL(fileURLWithPath:documentsPath+"/output.pdf")
        document.write(to:url)
        return url
    }

    @ IBAction funcpdfAction(_sender:Any){
        let url —URL = createPDF()
        let items = [url]
        let activityVc = UIAactivityViewController (activityItems, applicationActivities:nil)
        self.present(activityVc, animated:true, completion:nil)        
    }
}

swift

2022-09-29 21:52

1 Answers

I think it's a PDFKit bug.
Currently, the only workaround is to reverse the UIImage and then generate a PDFPage.

https://stackoverflow.com/questions/46219729/pdfkit-is-vertically-flipping-pdfpage-initialised-with-image


2022-09-29 21:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.