Create a PDF-File of a view with correct papersize

Hello together,

does anyone know how I can scale a view to a given document size (e.g. A4: 210/297 mm) without scaling the view itself?

With enclosed code I can create the pdf-document, but the paperize is not the intended size. It is too big.

Variation this line of code var mediaBox = CGRect(origin: .zero, size: CGSize(width: size.width, height: size.height))

into

var mediaBox = CGRect(origin: .zero, size: CGSize(width: 595, height: 842)

does not scale the view (it just shows a part of it at the correct papersize).

So what do I have to do to scale the rendered view to the proper size?

Enclosed a very simple code snipped to see th strange behavior.

Thx, best regards

Peter

import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack {
            Rectangle()
                .frame(width: 2100, height: 2910)
                .foregroundColor(.red)
            Rectangle()
                .frame(width: 2100/2, height: 2910/3)
                .foregroundColor(.blue)
            
            Text("Hello, world!")
        }
        .padding()
        
        .onAppear(perform: {
            generatePDF()
        })
    }
    
    // generate pdf from given view
    @MainActor func generatePDF() -> URL {
     //  Select UI View to render as pdf
        let image = ImageRenderer(content: ContentView())
        
        let url = URL.documentsDirectory.appending(path: "generatedPDF.pdf")
       
        image.render{ size, context in
            var mediaBox = CGRect(origin: .zero, size: CGSize(width: size.width, height: size.height))

            guard let consumer = CGDataConsumer(url: url as CFURL),
                  let pdfContext =  CGContext(consumer: consumer, mediaBox: &mediaBox, nil)
            else {
                return
            }
            pdfContext.beginPDFPage(nil)
            pdfContext.translateBy(x: mediaBox.size.width / 2 - size.width / 2,
                                   y: mediaBox.size.height / 2 - size.height / 2)
            context(pdfContext)
            pdfContext.endPDFPage()
            pdfContext.closePDF()
        }
        
        print("Saving PDF to \(url.path())")
        return url
    }
}

#Preview {
    ContentView()
}
![]("https://developer.apple.com/forums/content/attachment/67b11ec8-af9e-4ab7-a0a5-0c020b7a45d4" "title=generatedPDF (without scaling).jpg;width=2652;height=2526")


![]("https://developer.apple.com/forums/content/attachment/b2c24a25-3805-4664-8adb-dcb81c3e96c4" "title=generatedPDF (with scaling).jpg;width=2132;height=1510")

Replies

Hey,

I had the same issue, setting a CGRect to a PDFPage does work with the .setBounds(pageRect, for: .mediaBox) method, but finding a proper size to input was an issue.

After some research, I found out the size should be expressed in PostScript Points

I'm using the size I found there for A4 and US Letter: https://pdfkit.org/docs/paper_sizes.html A4 (595.28 x 841.89) LETTER (612.00 X 792.00)

App side I check NSLocale.current.usesMetricSystem to switch between the two.

Have a good one. Adrien.

Add a Comment