Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS create pdf from UIWebview content

Tags:

ios

pdf

uiwebview

In my app for iOS i need to create a pdf document from my webview content. I watched these posts: Creating PDF file from UIWebView and https://coderchrismills.wordpress.com/2011/06/25/making-a-pdf-from-a-uiwebview/

I wonder if there is a simpler way to do it. For example for my project for Mac i use this:

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

Is there any simple way to do this in iOS?

Which is the best option to obtain best quality pdf document from a webview content?

like image 420
user3065901 Avatar asked May 26 '15 17:05

user3065901


2 Answers

There isn't a method that allows this directly via the SDK like there is on Mac however you may wish to take a look at BNHtmlPdfKit which allows you to save the contents of URLs, web views and also html strings as PDFs.

For example, as follows:

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

It makes use of a custom UIPrintPageRenderer which overrides paperRect and printableRect thus causing the UIPrintFormatter to return a pageCount as well as render the document.

like image 147
Joshua Avatar answered Oct 14 '22 02:10

Joshua


I found good answer by AnderCover at "Creating PDF file from UIWebView" also it's not using any third party api. To create pdf from webview.

Hope it help's you.

like image 42
sschunara Avatar answered Oct 14 '22 01:10

sschunara