0

I load 10 page pdf file in uiwebview in objective c. After I want to write new pdf file from uiwbview. Please any one help to answer i want to write UIWebview.scrollView. Because i have added some image in UIWebview scrollview subview . its possible

Please check my below code im getting 10 pages in single pdf page. I dont know what the issues ?

-(void)createPDFfromUIView:(UIWebView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{


    NSArray *viewsToRemove1 = [self.pdfView.scrollView subviews];

    for (UIView *v1 in viewsToRemove1)
    {
        int imgL = v1.frame.size.width;
            if (imgL < 75 && imgL > 67)
            {
                [v1 removeFromSuperview];
                aView = pdfView;
            }
    }


    CGSize fullSize = aView.scrollView.contentSize;


    NSMutableData *pdfData = [NSMutableData data];
    CGRect oldFrame = aView.frame;
    [aView sizeToFit];
     CGRect rect = CGRectMake(aView.scrollView.contentOffset.x, aView.scrollView.contentOffset.y, fullSize.width, fullSize.height);
    // Points the pdf converter to the mutable data object and to the UIView to be converted
    UIGraphicsBeginPDFContextToData(pdfData, self.pdfView.scrollView.bounds, nil);

    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();
    [self.pdfView.scrollView.layer renderInContext:pdfContext];
    // remove PDF rendering context
    UIGraphicsEndPDFContext();

    [pdfData writeToFile:getPdfpath atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",getPdfpath);
   }
2vision2
  • 4,933
  • 16
  • 83
  • 164

2 Answers2

1

You can use something like this to load the PDF into an NSData object:

NSData *pdfFile = [NSData dataWithContentsOfURL:webView.request.URL];

Then, you can use the NSData methods to write the data to a file:

[pdfFile writeToFile:filePath atomically:YES];

Of course, you'll need to setup filePath to be an appropriate writable location to store the file/

jnpdx
  • 45,847
  • 6
  • 64
  • 94
  • Thanks for your answer. Yes its working i want to write UIWebview.scrollView. Because i have added some image in UIWebview scrollview subview . its possible ? – 2vision2 Jan 06 '15 at 11:51
  • Hm -- you may need to start a new question with this, since the edited version is almost completely different than what I answered, but I can try to help. Are you saying you need to export a UIWebView with a PDF that's been loaded, plus the changes you made by adding/removing UIViews? – jnpdx Jan 06 '15 at 19:32
  • You can write a PDF of the view (bitmapped) by using this technique: http://stackoverflow.com/questions/5443166/how-to-convert-uiview-to-pdf-within-ios – jnpdx Jan 06 '15 at 20:20
  • Yes i need to add some image and text in uiwebview scrollView. After i need to export – 2vision2 Jan 07 '15 at 04:22
  • I added/removing in subviews for uiwebview. its possible to export report? – 2vision2 Jan 07 '15 at 05:18
  • I don't know what the "report" is you're referring to. Did you read the link I sent about exporting a UIView to PDF? – jnpdx Jan 07 '15 at 08:08
  • ya i read that but i want to view word,excel to view. So only i added in uiwebview. its possible to view all document in UIview?. Sorry not report export PDF – 2vision2 Jan 07 '15 at 11:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68337/discussion-between-2vision2-and-jn-pdx). – 2vision2 Jan 07 '15 at 11:57
-1

If you need to call and access any file then first of all you need to add that file in your project after that you can use the below code to access it.

 NSString* path = [[NSBundle mainBundle] pathForResource:@"Terms"
                                                         ofType:@"txt"];
   NSString *content = [NSString stringWithContentsOfFile:path
                                                      encoding:NSUTF8StringEncoding
                                                         error:NULL];
Yash
  • 171
  • 2
  • 8