0

I am a new iOS developer. I want to complete create a PDF file but problem is that the PDF file does not save in device. When I open the PDF in click open button rather than than display, it is not stored in the original device. Here is my code.

#import "MTViewController.h"
#define kPadding 20

@interface MTViewController ()
{
    CGSize _pageSize;
}
@end

@implementation MTViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)didClickOpenPDF {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"NewPDF.pdf"];

    if([[NSFileManager defaultManager] fileExistsAtPath:pdfPath]) {

        ReaderDocument *document = [ReaderDocument withDocumentFilePath:pdfPath password:nil];

        if (document != nil)
        {
            ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];
            readerViewController.delegate = self;

            readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
            readerViewController.modalPresentationStyle = UIModalPresentationFullScreen;

            [self presentModalViewController:readerViewController animated:YES];
        }
    }
}

- (IBAction)didClickMakePDF {
    [self setupPDFDocumentNamed:@"NewPDF" Width:850 Height:1100];

    [self beginPDFPage];
    NSString *test=@"jigar";
    NSString *test1=@"ravi";
    CGRect textRect = [self addText:[NSString stringWithFormat:@"%@%@",test,test1]
                          withFrame:CGRectMake(kPadding, kPadding, 400, 200) fontSize:48.0f];

    CGRect blueLineRect = [self addLineWithFrame:CGRectMake(kPadding, textRect.origin.y + textRect.size.height + kPadding, _pageSize.width - kPadding*2, 4) 
                                       withColor:[UIColor blueColor]];

    UIImage *anImage = [UIImage imageNamed:@"tree.jpg"];
    CGRect imageRect = [self addImage:anImage 
                              atPoint:CGPointMake((_pageSize.width/2)-(anImage.size.width/2), blueLineRect.origin.y + blueLineRect.size.height + kPadding)];

    [self addLineWithFrame:CGRectMake(kPadding, imageRect.origin.y + imageRect.size.height + kPadding, _pageSize.width - kPadding*2, 4) 
                 withColor:[UIColor redColor]];

    [self finishPDF];

}

- (void)setupPDFDocumentNamed:(NSString*)name Width:(float)width Height:(float)height {
    _pageSize = CGSizeMake(width, height);

    NSString *newPDFName = [NSString stringWithFormat:@"%@.pdf", name];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName];

//    NSString *resourceDocPath = [[NSString alloc] initWithString:[
//                                                                  [[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
//                                                                  stringByAppendingPathComponent:@"Documents"]];
//    
//    NSString *filePath = [resourceDocPath
//                          stringByAppendingPathComponent:@"myPDF.pdf"];
//    [newPDFName writeToFile:filePath atomically:YES];


    UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil);
}

- (void)beginPDFPage {
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, _pageSize.width, _pageSize.height), nil);
}

- (void)finishPDF {
    UIGraphicsEndPDFContext();
}

- (CGRect)addText:(NSString*)text withFrame:(CGRect)frame fontSize:(float)fontSize {
    UIFont *font = [UIFont systemFontOfSize:fontSize];

    CGSize stringSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(_pageSize.width - 2*20-2*20, _pageSize.height - 2*20 - 2*20) lineBreakMode:UILineBreakModeWordWrap];

    float textWidth = frame.size.width;

    if (textWidth < stringSize.width)
        textWidth = stringSize.width;
    if (textWidth > _pageSize.width)
        textWidth = _pageSize.width - frame.origin.x;

    CGRect renderingRect = CGRectMake(frame.origin.x, frame.origin.y, textWidth, stringSize.height);

    [text drawInRect:renderingRect 
            withFont:font
       lineBreakMode:UILineBreakModeWordWrap
           alignment:UITextAlignmentLeft];

    frame = CGRectMake(frame.origin.x, frame.origin.y, textWidth, stringSize.height);

    return frame;
}

- (CGRect)addLineWithFrame:(CGRect)frame withColor:(UIColor*)color {
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    CGContextSetStrokeColorWithColor(currentContext, color.CGColor);

    // this is the thickness of the line
    CGContextSetLineWidth(currentContext, frame.size.height);

    CGPoint startPoint = frame.origin;
    CGPoint endPoint = CGPointMake(frame.origin.x + frame.size.width, frame.origin.y);

    CGContextBeginPath(currentContext);
    CGContextMoveToPoint(currentContext, startPoint.x, startPoint.y);
    CGContextAddLineToPoint(currentContext, endPoint.x, endPoint.y);

    CGContextClosePath(currentContext);    
    CGContextDrawPath(currentContext, kCGPathFillStroke);

    return frame;
}

- (CGRect)addImage:(UIImage*)image atPoint:(CGPoint)point {
    CGRect imageFrame = CGRectMake(point.x, point.y, image.size.width, image.size.height);
    [image drawInRect:imageFrame];

    return imageFrame;
}

- (void)dismissReaderViewController:(ReaderViewController *)viewController {
    [self dismissModalViewControllerAnimated:YES];
}
Mildwood
  • 349
  • 2
  • 13
Jigar
  • 1,801
  • 1
  • 14
  • 29
  • What do you mean by "not storing in original device" ? – CZ54 Mar 03 '16 at 09:53
  • @jigay your PDF is created Properly? – Vvk Mar 04 '16 at 05:09
  • 1
    `NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory=[paths objectAtIndex:0]; NSString *finalPath=[documentDirectory stringByAppendingPathComponent:[NSString stringWithFormat: @"Favourite/myFile.pdf"]]; //check your path correctly and provide your name dynamically NSLog(@"finalpath--%@",finalPath); NSData *datapdf = [NSData dataWithContentsOfURL:[NSURL urlWithString:yourURLString]]; //add url string here NSLog(@"data--%@",datapdf); if(datapdf) [datapdf writeToFile:finalPath atomically:YES];` – Vvk Mar 04 '16 at 09:12
  • try this with your pDF name – Vvk Mar 04 '16 at 09:13
  • @DarjiJigar other links are.....http://stackoverflow.com/questions/14288052/save-pdf-file-in-device – Vvk Mar 04 '16 at 09:13
  • http://stackoverflow.com/questions/16646039/how-to-save-created-pdf-in-document-folder-and-merge-in-ios – Vvk Mar 04 '16 at 09:13
  • @VvkAghera ok bro i will try\ – Jigar Mar 04 '16 at 09:24
  • I agree with @VvkAghera... – Jagat Dave Mar 04 '16 at 10:16
  • @VvkAghera i will try but no error solve – Jigar Mar 04 '16 at 11:09
  • @VvkAghera i will write nslog and display this type of message. 2016-03-05 10:16:32.997 PDFMaker[1387:60b] finalpath--/var/mobile/Applications/C14C7A91-DF7E-4D8C-970C-A57BDEDBF663/Documents/Favourite/NewPDF.pdf 2016-03-05 10:16:33.128 PDFMaker[1387:60b] data--(null) – Jigar Mar 05 '16 at 04:47

0 Answers0