NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

Creating PDF from UIView in Swift

Forums > Swift

I have been stuck/ researching this problem for days, the method below successfully creates the pdf from a regular size UIView. However, I need the contents of my containerView that is located inside a UIScrollview. The height of my containerView is 5000. Can someone help me to render the containerView into multiple PDF pages or direct me to a better way of doing this.

func exportAsPdfFromView() -> String {

        let pdfPageFrame = self.bounds
        let pdfData = NSMutableData()
        UIGraphicsBeginPDFContextToData(pdfData, pdfPageFrame, nil)
        UIGraphicsBeginPDFPageWithInfo(pdfPageFrame, nil)
        guard let pdfContext = UIGraphicsGetCurrentContext() else { return "" }
        self.layer.render(in: pdfContext)
        return self.saveViewPdf(data: pdfData)



Can you explain what you're trying to accomplish in more details? My understanding is you have a view placed inside a UIScrollView, as it likely higher than the display size so you need to scroll to see all the content, and you want to make a PDF of the scrollview. What is self in this example? If it is the UIScrollView, you shouldn't use the bounds, contentSize is what you're interested in.


I am actually attempting to render the pdf from a UISrollview, I am still having a big fight trying to make it work and I don't know where else to research. The documentation is limited.


I am new to SwiftUI. I am converting a scrollable SwiftUI view to pdf but unable to do that. The output will be empty.

Here is my code

This is the view that I want to convert into pdf

GeometryReader{geo in
                                        FirstView(getResumeId: self.getResumeId).onAppear{

This is my function that generates the pdf from view

func exportToPDF(width:CGFloat, height:CGFloat) {
        let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let outputFileURL = documentDirectory.appendingPathComponent("\(self.getResumeId).pdf")
        //Normal with
        let width: CGFloat = width
        //Estimate the height of your view
        let height: CGFloat = height
        let charts = FirstView(getResumeId: self.getResumeId)
        let pdfVC = UIHostingController(rootView: charts)
        pdfVC.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
        //Render the view behind all other views
        let rootVC =
        rootVC?.view.insertSubview(pdfVC.view, at: 0)
        //Render the PDF

        let pdfRenderer = UIGraphicsPDFRenderer(bounds: CGRect(x: 0, y: 0, width: width, height: height))
        DispatchQueue.main.async {
            do {
                try pdfRenderer.writePDF(to: outputFileURL, withActions: { (context) in
                    rootVC?.view.layer.render(in: context.cgContext)
                UserDefaults.standard.set(outputFileURL, forKey: "pdf")
                print("wrote file to: \(outputFileURL.path)")
            } catch {
                print("Could not create PDF file: \(error.localizedDescription)")


The empty pdf generate or sometimes pdf did not open. I searched alot but unable to do that. If anyone help me I am verythankful to him.


Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.