Skip to content

Easily specify bundle or documents location for the HTML Template #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions PdfReportKit/NSURLWebViewProtocol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// NSURLWebViewProtocol.h
//
// Created by Aaron Wardle on 29/11/2013.
// http://www.aaronwardle.oc.uk
//

#import <Foundation/Foundation.h>
#define kDocumentsImageUrl @"documentsimage"
#define kBundleImageUrl @"bundleimage"


@interface NSURLWebViewProtocol : NSURLProtocol

@end
88 changes: 88 additions & 0 deletions PdfReportKit/NSURLWebViewProtocol.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// NSURLWebViewProtocol.m
//
// Created by Aaron Wardle on 29/11/2013.
// http://www.aaronwardle.co.uk
//

#import "NSURLWebViewProtocol.h"

@implementation NSURLWebViewProtocol


+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
if ([request.URL.scheme caseInsensitiveCompare:kDocumentsImageUrl] == NSOrderedSame) {
return YES;
}

if ([request.URL.scheme caseInsensitiveCompare:kBundleImageUrl] == NSOrderedSame) {
return YES;
}

return NO;
}


+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
return request;
}

- (void)startLoading {

NSString *fileExtension;
NSString *fileName;
NSString *urlPrefix;
NSString *filePath;

[self extractFileName:&fileName fileExtension:&fileExtension urlPrefix:&urlPrefix];


NSURLResponse *response =[[NSURLResponse alloc]initWithURL:self.request.URL
MIMEType:nil expectedContentLength:-1
textEncodingName:nil];


if ([urlPrefix isEqualToString:kBundleImageUrl]) {
filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:fileExtension];
} else {
filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
filePath = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileName, fileExtension]];
}

NSData *data = [NSData dataWithContentsOfFile:filePath];

[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
[[self client] URLProtocol:self didLoadData:data];
[[self client] URLProtocolDidFinishLoading:self];

}


- (void)extractFileName:(NSString**)fileName fileExtension:(NSString **)extension urlPrefix:(NSString **)urlPrefix {

NSString *urlString = self.request.URL.absoluteString;
*extension = [urlString pathExtension];


NSRange isRange = [urlString rangeOfString:kBundleImageUrl options:NSCaseInsensitiveSearch];
if (isRange.location == 0) {
*urlPrefix = kBundleImageUrl;
} else {
isRange = [urlString rangeOfString:kDocumentsImageUrl options:NSCaseInsensitiveSearch];
if (isRange.location == 0) {
*urlPrefix = kDocumentsImageUrl;
}
}

urlString = [urlString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@://", *urlPrefix] withString:@""];

*fileName = [urlString stringByDeletingPathExtension];

}


- (void)stopLoading {

}

@end
4 changes: 4 additions & 0 deletions PdfReportKit/PRKRenderHtmlOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#import "PRKRenderHtmlOperation.h"
#import "PRKGenerator.h"
#import "NSURLWebViewProtocol.h"

@implementation PRKRenderHtmlOperation

Expand All @@ -27,6 +28,9 @@ - (id)initWithHtmlContent:(NSString *)html andSectionType: (PRKSectionType)secti

renderingWebView = [[UIWebView alloc] init];
renderingWebView.delegate = self;

// Register the custom NSURL WebView Protocol
[NSURLWebViewProtocol registerClass:[NSURLWebViewProtocol class]];
}

return self;
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ Then you must conform to `PRKGeneratorDataSource` and `PRKGeneratorDelegate` in
// Report generated!!! Now we can use NSData as we want!
}

## Specifying Images from the App Bundle or Documents Location
Within the html template you can now specify if an image for the PDF is located in the App Bundle Path or Documents Location (i.e. an image downloaded by your app)

To do this within your template use the following image prefixes:

```html
<img src="bundleimage://yourimage.png" />

or

<img src="documentsimage://yourdownloadedimage.png" />
```

## Result
Here it is your wonderful report generated in few simple steps (you can skip all the steps above and see the result just by executing the example application provided)!
![Report](http://img706.imageshack.us/img706/6599/captureylw.png)