Reputation: 4778
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?
Upvotes: 7
Views: 1754
Reputation: 2283
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.
Upvotes: 1
Reputation: 15520
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.
Upvotes: 5