0

I have iphone app which created Pie Chart i want that chart should be save in pdf file in iphone.

Below is the code for PieChart but how can i save it in pdf i have read that we can save text in pdf but how to save this

   -(void)createGraph{

  PieClass *myPieClass=[[PieClass alloc]initWithFrame:CGRectMake(400,40, 320, 230)];


  myPieClass.itemArray=[[NSArray alloc]initWithObjects:textFieldOne.text,textFieldTwo.text,textFieldThree.text, nil];

 myPieClass.myColorArray=[[NSArray alloc]initWithObjects:[UIColor purpleColor],[UIColor redColor],[UIColor orangeColor], nil];

 myPieClass.radius=100;
 [self.view addSubview:myPieClass];

  [self creatPDFFromView:@"mydata.pdf"];

 }

    -(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);
 }
Jackson J
  • 119
  • 1
  • 5
  • 13
  • Maybe this could help: [How to Convert UIView to PDF within iOS?][1] [1]: http://stackoverflow.com/questions/5443166/how-to-convert-uiview-to-pdf-within-ios – CarlJ Apr 20 '12 at 09:05

2 Answers2

1

Which view you want to create as a pdf in that view do the following changes

NSString *fileName=@"PdfFromView.pdf";
[self createPDFfromUIView:self.view saveToDocumentsWithFileName:fileName];


-(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);

}

After this code has been executed go to documents folder you will find the pdf document containing the same contents of that view you have passed in

Blockquote

[self createPDFfromUIView:self.view saveToDocumentsWithFileName:fileName]

Blockquote

this method call. this code working fine Thanks to Antonio

Yuva
  • 125
  • 3
  • 11
  • this code is working fine but i don't see my data in pdf file – Jackson J Apr 20 '12 at 11:22
  • the code is working but i did not see any data in my pdf file – Jackson J Apr 20 '12 at 11:35
  • UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(0, 0,320,250)]; textView.text=@"This is the Textview Content"; [self.view addSubview:textView]; NSString *fileName=@"PdfFromView.pdf"; [self createPDFfromUIView:self.view saveToDocumentsWithFileName:fileName]; **now you find that textview content is in a pdf document** – Yuva Apr 21 '12 at 08:19
0

Took from here:

How to Convert UIView to PDF within iOS?

(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);
}
Community
  • 1
  • 1
Antonio MG
  • 20,382
  • 3
  • 43
  • 62