ShigaSuresh ShigaSuresh - 4 months ago 14
iOS Question

How to generate multi page pdf file with Array of multiple images in iOS programmatically?

Hi i need to generate a pdf file of multiple pages with array of multiple images can any one help me with sample code please?

Answer

You can use these methods to generate a PDF file containing images:

- (void) generatePdfWithFilePath: (NSString *)thefilePath
{
    NSArray *pathsImage = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docspath = [pathsImage objectAtIndex:0];
    NSString *dataPath = [docspath stringByAppendingPathComponent:folder];
    NSString *imagepath=[dataPath stringByAppendingPathComponent:imageName];

    UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
    NSInteger currentPage = 0;
    BOOL done = NO;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *folder=[NSString stringWithFormat:@"ImageFolder"];
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:folder];

    UIImage *imgBrush = [UIImage imageWithContentsOfFile:imagepath];

    for (int i=0; i<directoryContent.count; i++) {

        [self drawImage:imgBrush];

    }
}


- (void) drawImage :(UIImage*) imgBrush
{
    if (imgBrush.size.width > pdfpageSize.width || imgBrush.size.height > pdfpageSize.height) {
        [imgBrush drawInRect:CGRectMake((pdfpageSize.width/2)-(imgBrush.size.width/4), (pdfpageSize.height/2)-(imgBrush.size.height/4), imgBrush.size.width/2, imgBrush.size.height/2)];
    } else {
        [imgBrush drawInRect:CGRectMake((pdfpageSize.width/2)-(imgBrush.size.width/2), (pdfpageSize.height/2)-(imgBrush.size.height/2), imgBrush.size.width, imgBrush.size.height)];
    }

}

Hey here is another method that you can use to render images in a PDF through code:

-(void)drawImagesToPdf:(UIImageView *)button

{
    CGSize pageSize = CGSizeMake(button.frame.size.width*5, button.frame.size.height*5+30);
    NSLog(@"page size %@",NSStringFromCGSize(pageSize));


    NSString *fileName = @"Demo.pdf";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
    UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectMake(0, 0, button.frame.size.width, button.frame.size.height*3), nil);

    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0.0, pageSize.width, pageSize.height), nil);

    NSArray *arrImages = [NSArray arrayWithObjects:@"3.png", @"4.png", @"5.png", @"6.png", nil];
    float y = 220.0;

    for (int i=0; i<arrImages.count; i++) {
        UIImage * myPNG = [UIImage imageNamed:[arrImages objectAtIndex:i]];

        [myPNG drawInRect:CGRectMake(50.0, y, myPNG.size.width, myPNG.size.height)];

        y += myPNG.size.height + 20;
    }

    UIGraphicsEndPDFContext();
}

Try my updated code...

Hope this is helpful to you... All the best..!!!

Comments