So if you want to display static content from HTML and PDF in your application, you probably know that it’s pretty easy to just throw the files into a folder and display them in a UIWebView something like this:
NSString *basePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/html/"];
NSURL *baseURL = [NSURL fileURLWithPath:basePath];
NSString *dataPath = [basePath stringByAppendingString:FILENAMEHERE];
NSString *dataType = [dataPath hasSuffix:@"pdf"] ? @"application/pdf" : @"text/html" ;
NSData *infoData = [NSData dataWithContentsOfFile:dataPath];
[infoView loadData:infoData MIMEType:dataType textEncodingName:@"UTF-8" baseURL:baseURL];
which works just fine, if your data is formatted nicely for iPhone display. Which for HTML is easy to sort; but it’s rather less likely that the PDFs you’re provided are suitable for iPhone display, they may be unduly large, inappropriate compression, yadayadayada. Well, here’s how you fix that quickly: just get the handy-dandy PDF Shrink desktop utility and apply iPhone-targeted compression settings. Convenient, yes?
Now, if you’d prefer a free alternative, or if you’re interested in perhaps putting programmatic conversion into a utility of your own, here is a useful walkthrough of creating a Quartz Filter to apply to exporting from Preview.app, which presumably you could apply from within your own code as well.
But if you really want your PDFs to get displayed professionally with table of contents and everything, here’s just your thing: The fellow who published the book ‘Scientific Scripting with Python’ exclusively on the App Store has very kindly provided a detailed walkthrough of constructing the application, formatting the document for the device, providing a table of contents … all with BSD-licensed open source!
If you decide to publish your own book using the source code, here is what you will need to do:
- Create a document like the one I made in Pages, with the same or similar page layout and text formatting.
- Generate a PDF from the document, and include it in the Xcode project.
- Change the value of the
BKPDFFileName variable in
BooksViewController.m to point to your PDF file.
- Change the data in the
TableOfContents.plist file so the table of contents is correct.
- Double click the ‘Python’ target, and change the name to something suitable for your book.
- In the Build settings of the target, change the ‘Product Name’ setting.
That should cover most of it.
Sweet! Not that we have any actual full books to publish at the moment, but hey if we ever do we certainly know where to start!
Continue Reading →