How can I create pdf from html without taking screenshot or image of uiwebview in objective c in iphone app?
When I create pdf to capture screenshot of UIwebView, pdf resolution is not looking good.In zoom in or zoom-out pixel of its text distroyed.
How can I create pdf from html without taking screenshot or image of uiwebview in objective c in iphone app?
When I create pdf to capture screenshot of UIwebView, pdf resolution is not looking good.In zoom in or zoom-out pixel of its text distroyed.
Generate PDF from Html Page. I Hope this Will Help You.
UIWebViewUse UIPrintPageRenderer from UIWebView Follow below steps :
Category of UIPrintPageRenderer for getting PDF Data@interface UIPrintPageRenderer (PDF)
- (NSData*) printToPDF;
@end
@implementation UIPrintPageRenderer (PDF)
- (NSData*) printToPDF
{
NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData( pdfData, self.paperRect, nil );
[self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)];
CGRect bounds = UIGraphicsGetPDFContextBounds();
for ( int i = 0 ; i < self.numberOfPages ; i++ )
{
UIGraphicsBeginPDFPage();
[self drawPageAtIndex: i inRect: bounds];
}
UIGraphicsEndPDFContext();
return pdfData;
}
@end
#define kPaperSizeA4 CGSizeMake(595.2,841.8)
UIWebView's webViewDidFinishLoad delegate use UIPrintPageRenderer property of UIWebView.- (void)webViewDidFinishLoad:(UIWebView *)awebView
{
if (awebView.isLoading)
return;
UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
[render addPrintFormatter:awebView.viewPrintFormatter startingAtPageAtIndex:0];
//increase these values according to your requirement
float topPadding = 10.0f;
float bottomPadding = 10.0f;
float leftPadding = 10.0f;
float rightPadding = 10.0f;
CGRect printableRect = CGRectMake(leftPadding,
topPadding,
kPaperSizeA4.width-leftPadding-rightPadding,
kPaperSizeA4.height-topPadding-bottomPadding);
CGRect paperRect = CGRectMake(0, 0, kPaperSizeA4.width, kPaperSizeA4.height);
[render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
[render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];
NSData *pdfData = [render printToPDF];
if (pdfData) {
[pdfData writeToFile:[NSString stringWithFormat:@"%@/tmp.pdf",NSTemporaryDirectory()] atomically: YES];
}
else
{
NSLog(@"PDF couldnot be created");
}
}
Get a PDF/PNG as output from a UIWebView or UIView
That is a good SO question that has the same approach..
however to create pdf programmatically, you can start here Generate PDF on iPhone:
http://iphonesdksnippets.com/post/2009/02/13/Generate-PDF-on-iPhone.aspx
And:
Drawing With Quartz2d:
Instead of taking it as a screen shot you can use quartz core to draw the UIwebview in PDF context. Here is the code.