Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS improve PDF render speed

I'm writing an app that manages documents for a user and (eventually) posts PDF files served out through a web service on the iPhone/iPad. These are almost exclusively scanned PDF files, and they appear to REALLLLLY bog down the iOS device.

The first solution I came up with was to simply host the PDF in a UIWebView. This works really, really well for "generated" PDF files, but NOT for scanned PDF files (I'm guessing the difference here is raster vs. vector?).

My next solution was to implement a UIDocumentInteractionController, which was said to increase snappiness. I can report that it does, indeed, seem to be faster than UIWebView, but it's still unacceptably slow, even on smallish, 2 page PDF files. (On a side note, the "auto-open-in-another-app" feature, along with built-in printing is super slick!)

I've read a post or 2 about the QuickLook framework, and I plan to look into that, but I've also stumbled across a few posts talking about CGPDFDocument classes and such. Those seem to have a finer control over document navigation (a la xPdf), but I have no idea where to start with that. Plus, I'm not even sure if it offers a performance benefit for what I'm doing.

So, first question: what's the fastest way to render scanned PDF files on the iPhone/iPad?

Second question: The scanned PDF files are generated by my company, so I have some control over the PDF generation settings. Does anybody know what settings might improve load speed for image-based PDF files?

Thanks!

(By the way: I've been coding for 19 hours straight today, so if I rambled or didn't make sense, please forgive me! :) )

like image 438
mbm29414 Avatar asked Aug 27 '26 23:08

mbm29414


2 Answers

The fastest solution will be to write your own custom pdf parser and rendering framework using the CGPDFDocument classes. The secret to superfast pdf rendering is using techniques like:

  • Off-screen rendering of full-size pages to images.
  • Only activate CATiledLayer drawing after a certain zoom level. When the user views your pdf page at the default zoom level there's no need to activate the CATiledLayer drawing because it's very expensive. Just display the already off-screen rendered image. When the user starts zooming in you can activate the CATiledLayer.
  • Using a smart algorithm to cache off-screen rendered pdf pages. You can cache the previous and next pages as off-screen rendered images.
  • On fast devices (iPhone 4 or greater and iPad 2 or greater) you can start a background job that will render all pages off-screen and save them to disk.
  • Cache regularly used information about pdf pages like: original rect, rotation, rotated rect etc.
  • You'll use a lot of CGPDFPageRef objects. It's important to know that this will increase memory usage a lot. A little trick is to close and re-open the CGPDFDocumentRef object when you receive a memory warning.

I used all of the above mentioned techniques in PDFTouch SDK for iOS which is a fast pdf rendering framework developed by me!

like image 69
Kemal Taskin Avatar answered Aug 29 '26 14:08

Kemal Taskin


You may wanna try to reduce image size. Large PDFs are really pushing the iPad/iPhone to its limits. Of course this means that you need to draw/manage the pdf yourself, with Quartz calls.

More speed is possible with proper caching. You can render pages off-screen, and display them without opening the actual pdf - which is much, much faster.

like image 40
steipete Avatar answered Aug 29 '26 13:08

steipete



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!