How to convert a UIView to a multi-page PDF in swift?
Asked Answered
Y

0

10

I have an app, written in Swift and want to export an entire UIView as a PDF. I found the code below does this well, but only produces a single-paged, cropped PDF. The exportable UIView contains a tableview which is dynamic, so I need to calculate the number of pages depending on the length of the UIView (which contains a scrollView for this purpose). Any help would be massively appreciated.

Thanks!

func createPDFFromView(aView: UIView, saveToDocumentsWithFileName fileName: String)
{
    let pdfData = NSMutableData()
    UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil)
    UIGraphicsBeginPDFPage()

    guard let pdfContext = UIGraphicsGetCurrentContext() else { return }

    aView.layer.renderInContext(pdfContext)
    UIGraphicsEndPDFContext()

    if let documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first {
        let documentsFileName = documentDirectories + "/" + fileName
        debugPrint(documentsFileName)
        pdfData.writeToFile(documentsFileName, atomically: true)
    }

}
Yocum answered 1/9, 2016 at 11:47 Comment(2)
did get any solution for this question? if yes please give update.Mccary
@RajJoshi just check PDFGenerator link. It works for me, also they have a demo of how to use it.Atomism

© 2022 - 2024 — McMap. All rights reserved.