Create PDF file
Sometimes we need to make the pdf file for saving the image or string (No editable) . for this we need to follow some steps.
1: Collect your all strings data and images and write the function to call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
- (IBAction) createPdf:(id)sender { //Get Document Directory path= NSArray * dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //Define path for PDF file NSString * documentPath = [[dirPath objectAtIndex:0] stringByAppendingPathComponent:@"Heading2.PDF"]; UIGraphicsBeginPDFContextToFile(documentPath, CGRectZero, nil); // Mark the beginning of a new page. UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil); NSString *yourString = @"22Hello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello World Hello WorldHello WorldHello WorldHello\n WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World qqq"; CGSize maximumLabelSize = CGSizeMake(353,9999); UILabel *shortDescription = [[UILabel alloc] initWithFrame:CGRectMake(5, 0,580, 20)]; [shortDescription setText:yourString]; [shortDescription setFont:[UIFont fontWithName:@"ArialMT" size:10.0f]]; [shortDescription setLineBreakMode:NSLineBreakByWordWrapping]; shortDescription.numberOfLines = 0; shortDescription.baselineAdjustment = UIBaselineAdjustmentAlignBaselines; shortDescription.textAlignment = NSTextAlignmentLeft; shortDescription.preferredMaxLayoutWidth = 580; [shortDescription sizeToFit]; NSLog(@" %f",shortDescription.frame.size.height); [ViewController drawText:yourString inFrame:CGRectMake(20, shortDescription.frame.size.height + 100 , 580, shortDescription.frame.size.height + 50)]; [ViewController drawLogo:CGRectMake(10,shortDescription.frame.size.height + 150,150,50)]; // images //[ViewController drawText:@"Hello World Hello WorldHello WorldHello WorldHello WorldHello WorldHello WorldHello World " inFrame:CGRectMake(0, 150, 692, 50)]; // Close the PDF context and write the contents out. UIGraphicsEndPDFContext(); } |
Here first you need to calculate the height and width of string that will take due to that reason I have used UILabel for calculating their width and height and set font after this we plot on pdf file.
Now write their function in same class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect { CFStringRef stringRef = (__bridge CFStringRef)textToDraw; // Prepare the text using a Core Text Framesetter CFMutableAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL); CTFontRef font = CTFontCreateWithName((CFStringRef)@"ArialMT", 10.0f, nil); CFAttributedStringSetAttribute(currentText,CFRangeMake(0, textToDraw.length-1), kCTFontAttributeName, font); CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText); CGMutablePathRef framePath = CGPathCreateMutable(); CGPathAddRect(framePath, NULL, frameRect); // Get the frame that will do the rendering. CFRange currentRange = CFRangeMake(0, 0); CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL); CGPathRelease(framePath); // Get the graphics context. CGContextRef currentContext = UIGraphicsGetCurrentContext(); // Put the text matrix into a known state. This ensures // that no old scaling factors are left in place. CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity); // Core Text draws from the bottom-left corner up, so flip // the current transform prior to drawing. CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2); CGContextScaleCTM(currentContext, 1.0, -1.0); // Draw the frame. CTFrameDraw(frameRef, currentContext); CGContextScaleCTM(currentContext, 1.0, -1.0); CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2); CFRelease(frameRef); CFRelease(stringRef); CFRelease(framesetter); } +(void)drawImage:(UIImage*)image inRect:(CGRect)rect { [image drawInRect:rect]; } +(void)drawLogo :(CGRect)inRect{ UIImage* logo = [UIImage imageNamed:@"ray-logo.png"]; [self drawImage:logo inRect:inRect]; } |
2: Now you need to access the pdf file and display.
1 2 3 4 5 6 7 8 |
- (IBAction)show:(id)sender { NSError *error; NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"Heading1.PDF"]; NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; NSLog(@"%@", filePath); String filePath1 = filePath; } |
3: after this, we need to open this file using web view.
1 2 3 4 |
NSURL *urlen = [NSURL fileURLWithPath:_filepath]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:urlen]; [_webView loadRequest:urlRequest]; [_webView setScalesPageToFit:YES]; |