0

I am creating pdf from iphone app using following code but when i call this method it gives exception.I have got this code from site.

here is the code

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


// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];

// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();


// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData

[aView.layer renderInContext:pdfContext];

// remove PDF rendering context
UIGraphicsEndPDFContext();

// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);


 }

I am calling this method here

    [self createPDFfromUIView:self.view];

i think problem is in parameter passing while calling method. thanks

here is the exception Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainViewController createPDFfromUIView:]: unrecognized selector sent to instance 0x613a950'

Ali Imran
  • 179
  • 4
  • 12
  • What is that exception pls share it. – Priyank Gandhi May 17 '13 at 04:00
  • @Impossible here is the exception Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainViewController createPDFfromUIView:]: unrecognized selector sent to instance 0x613a950' – Ali Imran May 17 '13 at 04:01
  • you have to pass pdf name with your function call like [self createPDFfromUIView:self.view saveToDocumentsWithFileName:@"Test"]; – Priyank Gandhi May 17 '13 at 04:06
  • @Impossible again exception Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainViewController createPDFfromUIView::]: unrecognized selector sent to instance 0x6156660' – Ali Imran May 17 '13 at 04:09
  • @Impossible thanks it worked but it does not show whole content it just show part of the view – Ali Imran May 17 '13 at 04:19

1 Answers1

2

Your implementation says it all

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename

Expects 2 parameters ad you have specified only 1, so instead of

[self createPDFfromUIView:self.view];

Try

[self createPDFfromUIView:self.view saveToDocumentsWithFileName:@"Name of the file"]
Deepesh Gairola
  • 1,252
  • 12
  • 18
  • yes it is working but when I NSLog the file it shows path and when find that path it does not show that path – Ali Imran May 17 '13 at 04:15
  • it is creating pdf but it does not show the whole view it just shows half i have a view in which i have scrollver view and i am adding content in scroller view so it show all the view – Ali Imran May 17 '13 at 04:18
  • @AliImran: I have never tried creating pdf files from `UIScrollView`. But it seems others have also faced same issue. Hope this might help you. http://stackoverflow.com/questions/15553895/uigraphicsgetcurrentcontext-and-uiscrollview – Deepesh Gairola May 17 '13 at 04:27
  • can you please edit in my code how may i use this i am not clear about the link you have given – Ali Imran May 17 '13 at 04:32