iOS 从 UIWebview 内容创建 pdf

iOS create pdf from UIWebview content

在我的 iOS 应用程序中,我需要从我的 webview 内容创建一个 pdf 文档。我看了这些帖子:Creating PDF file from UIWebView and https://coderchrismills.wordpress.com/2011/06/25/making-a-pdf-from-a-uiwebview/

不知道有没有更简单的方法。例如,对于我的 Mac 项目,我使用这个:

NSData *pdf = [[[[webView mainFrame] frameView] documentView] dataWithPDFInsideRect:[[[webView mainFrame] frameView] documentView].frame];
PDFDocument *doc = [[PDFDocument alloc] initWithData:pdf];

iOS有什么简单的方法可以做到这一点吗?

从 webview 内容中获取质量最好的 pdf 文档的最佳选择是什么?

没有一种方法可以像 Mac 上那样直接通过 SDK 进行此操作,但是您可能希望查看 BNHtmlPdfKit,它允许您保存 URL 的内容、网页浏览量以及 html PDF 格式的字符串。

例如如下:

self.htmlPdfKit = [BNHtmlPdfKit saveUrlAsPdf:[NSURL URLWithString:@"http://itsbrent.net"] toFile:@"...itsbrent.pdf" pageSize:BNPageSizeA6 success:^(NSString *pdfFileName) {
    NSLog(@"Done");
} failure:^(NSError *err) {
    NSLog(@"Failure");
}];

它使用自定义 UIPrintPageRenderer 覆盖 paperRectprintableRect 从而导致 UIPrintFormatter 到 return pageCount 并呈现文件.

我在 AnderCover 找到了很好的答案 “Creating PDF file from UIWebView” 它也没有使用任何第三方 api。从 webview 创建 pdf。

希望对你有所帮助。