Home > Enterprise >  How can I render multiple images to a multi page pdf document using SwiftUI
How can I render multiple images to a multi page pdf document using SwiftUI

Time:01-27

I am successfully creating a pdf document from 3 images using the following code:

struct ExportImagesView: View {
    
    var body: some View {
        ShareLink("Export Licence as PDF", item: render())
    }

    func render() -> URL {
        // 1: Render Hello World with some modifiers
        let renderer = ImageRenderer(content:
                                        VStack {
                                            Image("Page1")
                                            Image("Page2")
                                            Image("Page3")
            
                                        }
        )

        // 2: Save it to our documents directory
        let url = URL.documentsDirectory.appending(path: "output.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
    }
}

The above is from a tutorial at the following link:

https://www.hackingwithswift.com/quick-start/swiftui/how-to-render-a-swiftui-view-to-a-pdf

How can I iterate through the images to create a 3 page pdf document with one image per each page? I will specify the page size.

CodePudding user response:

you have it already. simply reshuffle the order of operations:

struct ExportImagesView: View {
    let pages = [ "Page1", "Page2", "Page3" ]
    
    var body: some View {
        VStack {
            ShareLink("Export Licence as PDF", item: render())
        }
    }
    
    @MainActor func render() -> URL {
        
        // 1: Save path
        let url = URL.documentsDirectory.appending(path: "output.pdf")
        
        // 2: PDF size
        var box = CGRect(x: 0, y: 0, width: 600, height: 800)
        
        // 3: Create the CGContext for our PDF pages
        guard let pdf = CGContext(url as CFURL, mediaBox: &box, nil) else {
            return url
        }
        
        // 4: Render each page
        for page in pages {
            
            pdf.beginPDFPage(nil)
            
            let renderer = ImageRenderer(content:
                Image(page)
            )
            
            renderer.render { size, context in
                context(pdf)
            }
            
            pdf.endPDFPage()
            
        }
        pdf.closePDF()
        
        return url
    }
}
  • Related