3

I want to create a PDF invoice inside my iOS App (either in Objective-C or Swift).
My main problem is that the invoice might have several pages, which is very difficult to realize with the existing APIs from Apple (CoreGraphics, Quartz 2D, etc).

By now, I already have a barely working solution:

  1. I created a HTML template which is the basic structure for the invoice
  2. The template is filled with data using GRMustache
  3. I load the generated HTML file into a UIWebView and save it as PDF (I used NDHTMLtoPDF to do this)

So far, so good.

The problem with this solution is that page breaks don't work properly. There are some tables and images and the page break often cut's off tables or images.
I have tried to use the page-break-inside: avoid; css property for the images and the tables but UIWebView seems to ignore them completely...

My question is:

  1. Do you know how to fix the page break problem?
  2. Can you recommend another solution to create PDFs on iOS?
  3. Should I design the invoice in Storyboard and generate a PDF from the UIView? What about the page breaks here?

I would prefer to have a template (e.g. HTML), fill it with data and save it as PDF, rather than doing everything in code.

Community
  • 1
  • 1
Sebastian
  • 43
  • 2
  • 4
  • Is there a reason you want to go through HTML? iOS lets you use CG calls to create a PDF file with content yourself, that would potentially be more work but you would have full control over what you generate, where you break or don't break pages etc... – David van Driessche Jun 04 '15 at 20:34
  • The reason why I wanted to use HTML is because I can easily create a template and fill it with information. Additionally, I could re-use the template in other (also non iOS) Projects. Moreover, I thought it is more difficult to manually create the Layout in CG (I would also have to care about page breaks and other stuff myself). If there's no other solution, I think I have to use CG. – Sebastian Jun 05 '15 at 20:44
  • 1
    if you want to create PDF invoice on iOS using Objective C check this [tutorial](http://www.raywenderlich.com/6818/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial-part-2). – x4h1d Aug 27 '15 at 12:15

1 Answers1

0

I actually wrote a PDF reporting component myself with presumably similar requirements:

  • templating is done via HTML / CSS
  • Static Header / Footer on each page

You can achieve this with UIPrintPageRenderer and UIViewPrintFormatter APIs, with the downside of probably getting your app rejected in the app store review, since you're using private Apple APIs. So this approach might only be viable in an academic setting or if your developing in-house apps.

There are some tutorials on how to do this which I'm not going to repeat, but for me these resources were quite useful:

JonEasy
  • 921
  • 10
  • 22