iosswiftuiprintpagerendereruiprintformatter

UIPrintPageRenderer how to switch between US-Letter and DIN-A4 format


I tried to implement a custom PDF-Renderer, inherited from UIPrintPageRenderer. My problem ist that the output-size of the PDF is 215,9 x 279,4 mm. But the correct DIN A4 dimensions i need are 210 x 297 mm. Obviously im getting the size of a US-Letter instead of the DIN A4. How can i change it to DIN A4?

...
class PDFRenderer: UIPrintPageRenderer
{
    private let A4PageWidth : Double = 595.2
private let A4PageHeight : Double = 841.8
private let leftPadding: CGFloat = 18.0;

override init()
{
    super.init()

    let pageFrame : CGRect = CGRect(x: 0, y: 0, width: A4PageWidth, height: A4PageHeight)
...

Thank you


Solution

  • I think you use the wrong rect when you calling the drawPage method for your pdfRenderer.

    many examples shows something like this:

    pdfRenderer.drawPage(at: i, in: UIGraphicsGetPDFContextBounds()) 
    

    use rect for a A4 format instead of UIGraphicsGetPDFContextBounds()

    e.g. CGRect.init(x: 0, y: 0, width: 595.2, height: 841.8)