3

I'm working on a function in my app that lets users export their data into a nicely designed PDF.

I'm using UIGraphicsPDFRenderer for this and I went through Apple's documentation.

I'm having a problem adding a footer like "page x of y". While "x" is easy - I'm having troubles determining the "y" since I only know how many pages my document has once I have fully rendered the PDF. I cannot determine the number of pages in advance because of the rather complex layout.

Now I also know that new pages are created with beginPage(). Is there also a way to go to the previous page? - Because then I could simply go through the document and add the missing footer.

Here's the code I use (very much simplified, but it should be enough to get the idea) in Swift 4:

let pdf = renderer.pdfData { (context) in
  let attributes = [ NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15) ]
  // page 1
  context.beginPage()
  NSAttributedString(string: "Page 1", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))

  // page 2
  context.beginPage()
  NSAttributedString(string: "Page 2", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))

  // page 3
  context.beginPage()
  NSAttributedString(string: "Page 3", attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))

  // Now that I know that the PDF will have 3 pages, add the footer on all 3 pages - but how?
  }
}
Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
Chris
  • 107
  • 1
  • 10
  • 1
    Please, provide a code you tried so far. – fewlinesofcode Nov 15 '18 at 11:39
  • Can't you create a pdf without the footer, get the pageCount, discard that pdf and create another one or simply edit the footer pages count? – Leo Dabus Nov 15 '18 at 12:20
  • @fewlinesofcode I've added a sample code to get the idea of what I'm trying to do. – Chris Nov 15 '18 at 13:02
  • @LeoDabus Thanks, that's a good idea - that will work for sure. Only thing I do not so much like about it is, that I more or less create the same document twice. I'm going to have to test it with larger amounts of data to see how it's going to work out :) – Chris Nov 15 '18 at 13:06
  • @Chris you should also post your Swift version if you are not using the latest one. – Leo Dabus Nov 15 '18 at 13:11
  • And don't use NSString when coding in Swift. You should always use String Swift native type. String doesn't have a draw method but you can use `NSAttributed` initializer to add the text attributes and then you can draw the result. `NSAttributedString(string: text, attributes: attributes).draw(in: CGRect(x: 0, y: 0, width: 500, height: 200))` – Leo Dabus Nov 15 '18 at 13:14
  • 1
    @LeoDabus thanks for the hints - added the Swift version (version 4) and updated the code to use NSAttributedString. – Chris Nov 15 '18 at 14:05

1 Answers1

3

UIGraphicsPDFRenderer is a forward only pdf generator.
You have 2 options:

  1. (recommended) Analyze the data before writing it to pdf to determine on how many pages it fits. Since your code performs the page breaks you should be able to compute how many pages are required for your data.
  2. Create your document without ‘page x of y’ and save it. Create a new document, open the previously saved document and draw its pages on the pages of the new document and then add the ‘page x of y’.

2 is more cumbersome and inefficient so I would go with 1. Because saving, editing and deleting takes more CPU and memory.

Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
Mihai Iancu
  • 1,818
  • 2
  • 11
  • 10
  • Thanks, Mihai! I did go with option 1 and it works pretty fast - even with large data. :) – Chris Nov 20 '18 at 16:52