PDF exports from iOS app

Few days ago we uploaded our application for UAT and the nightmare for us was PDF reports weren’t good enough. We followed Apple’s QUARTZ for PDF exports and after spending some insane amount of time we realised it wasn’t good enough.

So the search for some third-party library begun, I come from .Net & Android development background and I can say for sure that PDF exports for reports in both the platforms mentioned is quite easy and straight-forward in two steps,

  1. Create Template in HTML file and put strings to be replaced in curly braces {String-To-Be-Replaced}
  2. Get this HTML file string in code and replace the necessary String from template and give it to Library. VOILA! you are done.

So taking my previous experience into consideration , i gave it a try and stumbled upon one library iOS-htmlToPDF . I cloned the git repo and tried the sample project. It was Easier than I thought.

First of all let me tell you about the application, The application is Image-Centric application for Construction-Sites, the site-inspector/concern person visits the construction-site and takes image and adds some Notes,Lines,Pointers like below

Image Editing Screen of the application

So after user has finished editing, he can upload to server or if he chooses to share than a list of contact opens up and image is embedded into the PDF report template and sent to desired contact.

Now let’s take a look at our PDF template, basically we had two templates one for 1 image and other for more than 1 images. I will focus on multiple-images template.

Multiple Images PDF report Template

Let’s get started with our PDF html template. I divided the template into two parts,

  1. The Header
  2. Row template

So i made two files in my Xcode project

Html template for PDF

Here is how pdf_header.html looks,

PDF header template in HTML you can see “%@” in places, this for the strings to be replaced dynamically from code

and here is how our pdf_template_row looks like

HTML code for pdf_template_row, you can see “%@” in places for replacing the strings dynamically from code

Now comes the iOS part,

PDF create function with explanation
Writing the html string we just made and saving it to file which then we pass to library and get our PDF back.

So that was it and here is the return of PDF we got from the library,

PDF generated via Html template and HtmlToPDF library

This still needs some neatness and finishing, but this process of creating PDF reports from app is so easy and scalable.