0

I have to create or append new pages to a PDFKit document using Swift 4. The document format needs to be German DIN A4 (letter format).

I thought that the PDFDocumentAttribute could specify this, but they did not.

Is there any proper way to specify the document dimensions?

Tobonaut
  • 2,245
  • 2
  • 26
  • 39
  • I am not familiar with PDFKit, but apparently PDFPage has methods to set the bounding boxes. (Btw, "DIN A4" and "Letter" are different page formats) – Martin R Feb 21 '19 at 09:25
  • @MartinR thanks. Yes - I know, but I was not sure that DIN A4 is well-known outside of Germany. :) – Tobonaut Feb 21 '19 at 10:29

1 Answers1

2

Use the PDFView / PDFKit Tutorial of

https://www.raywenderlich.com/4023941-creating-a-pdf-in-swift-with-pdfkit

After diving in into the topic and some calculations based in the PDF documentation, I ended up with the magic number: let cm = CGFloat(28.346), resulting in:

let cm = CGFloat(28.346) // 72.0 / 2.54, Resolution divided by conversion factor inch -> cm
let a4Width = CGFloat(21.0)  // A4 width in cm
let a4Height = CGFloat(29.7) // A4 height in cm
let a4Paper = CGRect(x: 0, y: 0, width: cm * a4Width, height: cm * a4Height)

It comes from the resolution of 72.0, mentioned in the tutorial, divided by the conversion factor from inch to cm, which is 2.54: 72.0 / 2.54 = 28.346. So if you calculating in inch, you can just use 72.0 multiplied by the length in inch.

FrugalResolution
  • 568
  • 4
  • 18