Skip to content

Commit 3fc36f2

Browse files
author
Antonio Scandurra
committed
Merge pull request #12 from aaronwardle/master
Easily specify bundle or documents location for the HTML Template
2 parents fc5849e + 0f10fe6 commit 3fc36f2

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

PdfReportKit/NSURLWebViewProtocol.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// NSURLWebViewProtocol.h
3+
//
4+
// Created by Aaron Wardle on 29/11/2013.
5+
// http://www.aaronwardle.oc.uk
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
#define kDocumentsImageUrl @"documentsimage"
10+
#define kBundleImageUrl @"bundleimage"
11+
12+
13+
@interface NSURLWebViewProtocol : NSURLProtocol
14+
15+
@end

PdfReportKit/NSURLWebViewProtocol.m

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//
2+
// NSURLWebViewProtocol.m
3+
//
4+
// Created by Aaron Wardle on 29/11/2013.
5+
// http://www.aaronwardle.co.uk
6+
//
7+
8+
#import "NSURLWebViewProtocol.h"
9+
10+
@implementation NSURLWebViewProtocol
11+
12+
13+
+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
14+
if ([request.URL.scheme caseInsensitiveCompare:kDocumentsImageUrl] == NSOrderedSame) {
15+
return YES;
16+
}
17+
18+
if ([request.URL.scheme caseInsensitiveCompare:kBundleImageUrl] == NSOrderedSame) {
19+
return YES;
20+
}
21+
22+
return NO;
23+
}
24+
25+
26+
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
27+
return request;
28+
}
29+
30+
- (void)startLoading {
31+
32+
NSString *fileExtension;
33+
NSString *fileName;
34+
NSString *urlPrefix;
35+
NSString *filePath;
36+
37+
[self extractFileName:&fileName fileExtension:&fileExtension urlPrefix:&urlPrefix];
38+
39+
40+
NSURLResponse *response =[[NSURLResponse alloc]initWithURL:self.request.URL
41+
MIMEType:nil expectedContentLength:-1
42+
textEncodingName:nil];
43+
44+
45+
if ([urlPrefix isEqualToString:kBundleImageUrl]) {
46+
filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:fileExtension];
47+
} else {
48+
filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
49+
filePath = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", fileName, fileExtension]];
50+
}
51+
52+
NSData *data = [NSData dataWithContentsOfFile:filePath];
53+
54+
[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
55+
[[self client] URLProtocol:self didLoadData:data];
56+
[[self client] URLProtocolDidFinishLoading:self];
57+
58+
}
59+
60+
61+
- (void)extractFileName:(NSString**)fileName fileExtension:(NSString **)extension urlPrefix:(NSString **)urlPrefix {
62+
63+
NSString *urlString = self.request.URL.absoluteString;
64+
*extension = [urlString pathExtension];
65+
66+
67+
NSRange isRange = [urlString rangeOfString:kBundleImageUrl options:NSCaseInsensitiveSearch];
68+
if (isRange.location == 0) {
69+
*urlPrefix = kBundleImageUrl;
70+
} else {
71+
isRange = [urlString rangeOfString:kDocumentsImageUrl options:NSCaseInsensitiveSearch];
72+
if (isRange.location == 0) {
73+
*urlPrefix = kDocumentsImageUrl;
74+
}
75+
}
76+
77+
urlString = [urlString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@://", *urlPrefix] withString:@""];
78+
79+
*fileName = [urlString stringByDeletingPathExtension];
80+
81+
}
82+
83+
84+
- (void)stopLoading {
85+
86+
}
87+
88+
@end

PdfReportKit/PRKRenderHtmlOperation.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#import "PRKRenderHtmlOperation.h"
1616
#import "PRKGenerator.h"
17+
#import "NSURLWebViewProtocol.h"
1718

1819
@implementation PRKRenderHtmlOperation
1920

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

2829
renderingWebView = [[UIWebView alloc] init];
2930
renderingWebView.delegate = self;
31+
32+
// Register the custom NSURL WebView Protocol
33+
[NSURLWebViewProtocol registerClass:[NSURLWebViewProtocol class]];
3034
}
3135

3236
return self;

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ Then you must conform to `PRKGeneratorDataSource` and `PRKGeneratorDelegate` in
7474
// Report generated!!! Now we can use NSData as we want!
7575
}
7676

77+
## Specifying Images from the App Bundle or Documents Location
78+
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)
79+
80+
To do this within your template use the following image prefixes:
81+
82+
```html
83+
<img src="bundleimage://yourimage.png" />
84+
85+
or
86+
87+
<img src="documentsimage://yourdownloadedimage.png" />
88+
```
89+
7790
## Result
7891
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)!
7992
![Report](http://img706.imageshack.us/img706/6599/captureylw.png)

0 commit comments

Comments
 (0)