3

how to create UIWebView Content as pdf file..? i followed this link but this one not creating the entire webview content(still have some data on webview when i scroll) it is only creating a pdf what the screen is showing..

How to Convert UIView to PDF within iOS?

any help

Community
  • 1
  • 1
Sagar Babu
  • 53
  • 4

2 Answers2

3

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

You're right, you need a Webview.

If you have the .pdf file in the bundle, simply do this :

@implementation{
     UIWebView *documentWebView;
}

- (void)viewDidLoad{     //Or any other method that you want to create the webview with.

    //Getting the full screen size
    UIWindow* currentWindow = [UIApplication sharedApplication].keyWindow;
    CGRect fullScreenRect = currentWindow.bounds;

    //Getting the pdf from the bundle ; change filepath with URL if its from the internet
    filePath = [[NSBundle mainBundle] pathForResource:@"YourPDFFileName" ofType:@"pdf"];
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];

    //Creating the view programatically, because I'm like that.
    documentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(fullScreenRect.origin.x, fullScreenRect.origin.y, fullScreenRect.size.width, fullScreenRect.size.height)];
    documentWebView.scalesPageToFit = YES;

    //Setting a back button to dismiss the view.
    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(fullScreenRect.size.width-115, 10, 100, 30)];
    backButton.backgroundColor = [UIColor grayColor];
    backButton.alpha = 0.8;
    [backButton setTitle:@"Back" forState:UIControlStateNormal];

    //Linking the action & the button
    [backButton addTarget:self action:@selector(closeWebView:) forControlEvents:UIControlEventTouchUpInside];

    //Loading the pdf into the actual view
    [documentWebView loadRequest:request];

    //Adding the views to the main view
    [currentWindow addSubview:documentWebView];
    [documentWebView addSubview:backButton];
}

//Dismissing the view
- (IBAction)closeWebView:(id)sender{
    [documentWebView removeFromSuperview];
    [documentWebView release];

}
Gil Sand
  • 5,802
  • 5
  • 36
  • 78