Skip to content

Commit 6767874

Browse files
Fix a bug where the UIWebView goes out of scope when a method calling saveHtmlAsPdf: or saveUrlAsPdf: finishes.
UIWebView objects were locally scoped to the method calls, meaning that when those methods returned the web view was deallocated. Now the webView object is scoped to the entire class, ensuring that the delegate methods are triggered and that the PDFs are generated properly. webView is disposed of after the callbacks, since web pages for PDFs may be pretty hefty in size. Also updated the README with similar instructions for use: Your examples suggest that a local allocation of BNHtmlPdfKit will work as expected, however it goes out of scope before the delegate methods are called.
1 parent 55347aa commit 6767874

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

BNHtmlPdfKit.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ - (CGRect)printableRect {
4343

4444
@interface BNHtmlPdfKit () <UIWebViewDelegate> {
4545
NSString *_outputFile;
46+
UIWebView *_webView;
4647
}
4748
- (CGSize)_sizeFromPageSize:(BNPageSize)pageSize;
4849
@end
@@ -106,9 +107,9 @@ - (void)saveHtmlAsPdf:(NSString *)html {
106107
- (void)saveHtmlAsPdf:(NSString *)html toFile:(NSString *)file {
107108
_outputFile = file;
108109

109-
UIWebView *webView = [[UIWebView alloc] init];
110-
webView.delegate = self;
111-
[webView loadHTMLString:html baseURL:[NSURL URLWithString:@"http://localhost"]];
110+
_webView = [[UIWebView alloc] init];
111+
_webView.delegate = self;
112+
[_webView loadHTMLString:html baseURL:[NSURL URLWithString:@"http://localhost"]];
112113
}
113114

114115
- (void)saveUrlAsPdf:(NSURL *)url {
@@ -118,9 +119,9 @@ - (void)saveUrlAsPdf:(NSURL *)url {
118119
- (void)saveUrlAsPdf:(NSURL *)url toFile:(NSString *)file {
119120
_outputFile = file;
120121

121-
UIWebView *webView = [[UIWebView alloc] init];
122-
webView.delegate = self;
123-
[webView loadRequest:[NSURLRequest requestWithURL:url]];
122+
_webView = [[UIWebView alloc] init];
123+
_webView.delegate = self;
124+
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
124125
}
125126

126127
#pragma mark - UIWebViewDelegate
@@ -163,12 +164,16 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView {
163164
[_delegate htmlPdfKit:self didSavePdfFile:_outputFile];
164165
}
165166
}
167+
168+
_webView = nil;
166169
}
167170

168171
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
169172
if ([_delegate respondsToSelector:@selector(htmlPdfKit:didFailWithError:)]) {
170173
[_delegate htmlPdfKit:self didFailWithError:error];
171174
}
175+
176+
_webView = nil;
172177
}
173178

174179
#pragma mark - Private Methods

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,26 @@ This all started with a [post of mine](http://itsbrent.net/2011/06/printing-conv
2020

2121
## Usage
2222

23+
Be sure to retain a reference to the `BNHtmlPdfKit` object outside the scope of the calling method. Otherwise, no delegate methods will be called:
24+
25+
```objective-c
26+
@interface MyCoolViewController ()
27+
{
28+
BNHtmlPdfKit *_htmlPdfKit;
29+
}
30+
31+
- (void) createPdf:(id)sender {
32+
_htmlPdfKit = [[BNHtmlPdfKit alloc] init];
33+
_htmlPdfKit.delegate = self;
34+
[_htmlPdfKit saveUrlAsPdf:[NSURL URLWithString:@"http://itsbrent.net/index.html"]];
35+
}
36+
37+
@end
38+
39+
// Delegate methods go here...
40+
41+
```
42+
2343
### Initializers
2444
2545
```objective-c

0 commit comments

Comments
 (0)