2

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.

Saurav Sinha
  • 41
  • 1
  • 2
  • 8

4 Answers4

3

Generate PDF from Html Page. I Hope this Will Help You.

Ashvin
  • 8,227
  • 3
  • 36
  • 53
2

Load html in UIWebView

Use UIPrintPageRenderer from UIWebView Follow below steps :

Add 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

Add these define for A4 size

#define kPaperSizeA4 CGSizeMake(595.2,841.8)

Now in 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");
  }
}
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
0

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:

http://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/drawingwithquartz2d.pdf

Community
  • 1
  • 1
MCKapur
  • 9,127
  • 9
  • 58
  • 101
0

Instead of taking it as a screen shot you can use quartz core to draw the UIwebview in PDF context. Here is the code.

Community
  • 1
  • 1
Vignesh
  • 10,205
  • 2
  • 35
  • 73