I have not used the Observation framework or SwiftUI's image renderer, but I think you need to move your pdfReport function out of the view model and into a SwiftUI view. I don't think you can create the image renderer in a view model.
One question about your code. Why do you pass an array of Person objects to pdfReport and have the personsToPrint property in the ViewModel class? You don't use the person argument in the pdfReport function.
Wouldn't it be easier to get rid of the personsToPrint property and use the array of Person objects in the for loop? Then you wouldn't have to worry about the array being nil.
@MainActor func pdfReport(people: [Person]) {
for person in people {
// Rest of code omitted
}
}
Topic:
App & System Services
SubTopic:
General
Tags: