I have the following code for generating a one page PDF:
@MainActor func render() -> URL {
let renderer = ImageRenderer(content: pdfView))
let url = URL.documentsDirectory.appending(path: "output.pdf")
renderer.render { size, context in
var document = CGRect(x: 0, y: 0, width: 2550, height: 3300)
guard let pdf = CGContext(url as CFURL, mediaBox: &document, nil) else {
return
}
pdf.beginPDFPage(nil)
context(pdf)
pdf.endPDFPage()
pdf.closePDF()
}
return url
}
I'm trying to write code to create a multi-page PDF if there is multiple ImageRenderers.
I tried something shown below but I'm not sure how to properly implement.
@MainActor func render() -> URL {
let renderer = ImageRenderer(content: pdfView)
let url = URL.documentsDirectory.appending(path: "output.pdf")
for image in renderer {
image.render { size, context in
var page = generatePage(image: image)
}
}
return url
}
func generatePage(image: ImageRenderer<<#Content: View#>>) -> CGContext {
var view = CGRect(x: 0, y: 0, width: 2550, height: 3300)
guard let pdf = CGContext(url as CFURL, mediaBox: &view, nil) else {
return
}
pdf.beginPDFPage(nil)
context(pdf)
pdf.endPDFPage()
pdf.closePDF()
return pdf
}
Any guidance would be greatly appreciated. Thank you.
In the example you provided you're calling "guard let pdf = CGContext(url as CFURL, mediaBox: &view, nil)" inside of your generatePage every time you want to add a page to the document. I was expecting to see you calling that once at the beginning and then using the same context over and over again when adding pages using the beginPDFPage(), endPDFPage() pair. Then, after you have added all of the pages, you'd call closePDF() on that context. You're pretty close. I think all you need to do is just a little restructuring of the calls you are making.