TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Render SwiftUI view to pdf ( debug Errors)

Forums > SwiftUI

Hi everyone, I use this method for render a SwiftUI view to pdf, My method is work and my app not crashing but I got errors on the debug area. The errors appearing exactly when I tap ShareLink button.

Error image: https://ibb.co/7YN58Gc

my code:

struct ContentView: View {

    var body: some View { ...
        // Rest of the codes

            }
        }
        .navigationTitle("")
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItemGroup {
                ShareLink("Export PDF", item: render())
            }
        }
    }

    func render() -> URL {
        // 1: Render Hello World with some modifiers
        let renderer = ImageRenderer(content: PdfView)

        // 2: Save it to our documents directory
        let url = URL.documentsDirectory.appending(path: "test.pdf")

        // 3: Start the rendering process
        renderer.render { size, context in
            // 4: Tell SwiftUI our PDF should be the same size as the views we're rendering
            var box = CGRect(x: 0, y: 0, width: size.width, height: size.height)

            // 5: Create the CGContext for our PDF pages
            guard let pdf = CGContext(url as CFURL, mediaBox: &box, nil) else {
                return
            }

            // 6: Start a new PDF page
            pdf.beginPDFPage(nil)

            // 7: Render the SwiftUI view data onto the page
            context(pdf)

            // 8: End the page and close the file
            pdf.endPDFPage()
            pdf.closePDF()
        }

        return url
    }

var PdfView: some View { ...
        // Rest of the code
            }
        }
    }
}

How I can solve that?

Thanks in advance

3      

Hi, fairly new to SwiftUI, but found this solution. SwiftData is not accessible without the modelContext. Try and get the modelContext from the Environment, and then attach the modelContext to the view you want to render.

Try importing the Environment like this:

struct ContentView: View {
@Environment(\.modelContext) private var modelContext

var body: some View { ...
        // Rest of the codes

Try attaching your environment with the .modelContext modifier to the struct you are trying to render, like this:

func render() -> URL {
        // 1: Render Hello World with some modifiers
        let renderer = ImageRenderer(content:
          PdfView()
          .modelContext(modelContext)
        )

3      

I tryed your code and not succesfuly.

Thank you @VSilverstein for trying to help me

3      

Arh okay, thought it was SwiftData related. Not much code or debug code to visually see where the bug is (I'm new), but my guess is a missing @viewbuilder or use () for the PdfView:

let renderer = ImageRenderer(content: PdfView())

Btw, try using capitalized letters for URL, PDF etc. :)

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.