2015-05-26 64 views
7

在我的iOS應用程序中,我需要從我的webview內容創建一個pdf文檔。我看過這些帖子:Creating PDF file from UIWebViewhttps://coderchrismills.wordpress.com/2011/06/25/making-a-pdf-from-a-uiwebview/iOS從UIWebview內容創建pdf

我不知道是否有一個更簡單的方法來做到這一點。例如對於我的Mac項目,我使用這個:

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

有什麼簡單的方法可以做到這一點在iOS?

哪一個是最好的選擇,從webview內容獲得最好的質量PDF文檔?

+0

引用此問題:http://stackoverflow.com/questions/15813005/creating-pdf-file-from-uiwebview – Bhushan

回答

4

有沒有一種方法可以直接通過SDK,就像在Mac上一樣,但是您可能希望看一下BNHtmlPdfKit,它允許您將URL,Web視圖的內容以及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返回一個PAGECOUNT以及呈現文檔的。