BNHtmlPdfKit代码在iOS上没有做任何事情

时间:2014-06-21 05:20:19

标签: ios pdf-generation

我正在尝试使用BNHtmlPdfKit将某些HTML另存为PDF。只是为了看它是否有效,我正在尝试将网页写入PDF。我无法让它工作(根本)。以下是我的代码。

首先,我包括委托引用:

@interface PPToolsTableViewController () <BNHtmlPdfKitDelegate>

然后我执行以下操作:

NSString *exportsPath = [[PPHelpers documentsPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"exports/Exported.pdf"]];

BNHtmlPdfKit *htmlPdfKit = [[BNHtmlPdfKit alloc] init];
htmlPdfKit.delegate = self;
[htmlPdfKit saveUrlAsPdf:[NSURL URLWithString:@"http://google.com"] toFile:exportsPath];

什么都没发生。没有错误,也没有任何委托方法触发:

- (void) createPdf:(id)sender {
  NSLog(@"Create PDF");
}
- (void)htmlPdfKit:(BNHtmlPdfKit *)htmlPdfKit didSavePdfData:(NSData *)data {
  NSLog(@"PDF Save Data");
}
- (void)htmlPdfKit:(BNHtmlPdfKit *)htmlPdfKit didSavePdfFile:(NSString *)file {
  NSLog(@"PDF Save File");
}
- (void)htmlPdfKit:(BNHtmlPdfKit *)htmlPdfKit didFailWithError:(NSError *)error {
  NSLog(@"PDF Error");
}

熟悉这个图书馆的人是否能够为我提供一个有效的例子?或者也许发现我在这里做的事情有什么问题?提前谢谢。

1 个答案:

答案 0 :(得分:3)

我终于在文档中找到了这个小片段,这让我找到了解决方案:

  

请务必保留对BNHtmlPdfKit对象的引用   调用方法的范围。否则,没有委托方法   称为...

因此,使{BNHtmlPdfKit对象成为@property会产生重大影响:

@property (strong, nonatomic) BNHtmlPdfKit *htmlPdfKit;

......然后这起作用了:

NSString *exportsPath = [[PPHelpers documentsPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"exports/Logbook.pdf"]];

self.htmlPdfKit = [[BNHtmlPdfKit alloc] init];
self.htmlPdfKit.delegate = self;

[self.htmlPdfKit saveUrlAsPdf:[NSURL URLWithString:@"http://google.com"] toFile:exportsPath];

现在好多了。 :)