iOS PDF Creation in iOS Create PDF

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);
    
[self drawText];
    
UIGraphicsEndPDFContext();

fileName is the document file where You are going to append or attach

 NSString* temporaryFile = @"firstIOS.PDF";
    NSArray *arrayPaths =
    NSSearchPathForDirectoriesInDomains(
                                        NSDocumentDirectory,
                                        NSUserDomainMask,
                                        YES);
    
    NSString *path = [arrayPaths objectAtIndex:0];
    
    NSString* fileName = [path stringByAppendingPathComponent:fileName];

Where drawText is

   (void)drawText
{
    NSString* textToDraw = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
   
    CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
    
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
    
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
    
    CGRect frameRect = CGRectMake(0, 0, 300, 100);
    
    CGMutablePathRef framePath = CGPathCreateMutable();
    
    CGPathAddRect(framePath, NULL, frameRect);
    
    CFRange currentRange = CFRangeMake(0, 0);
    
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
    CGPathRelease(framePath);
    
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    
   
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
    
   
    CGContextTranslateCTM(currentContext, 0, 450);
    
    CGContextScaleCTM(currentContext, 2, -2);
    
    CTFrameDraw(frameRef, currentContext);
    
    CFRelease(frameRef);

    CFRelease(stringRef);

    CFRelease(framesetter);
}

PDF



Got any iOS Question?