Reputation: 117
I need create a single page pdf file with multiple images in iOS. i used this code:
- (void)createPDFWithImagesArray:(NSMutableArray *)array andFileName:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *PDFPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.pdf",fileName]];
for (UIImage *image in array)
{
// Mark the beginning of a new page.
CGRect pagebounds=CGRectMake(0, 0, image.size.width, image.size.height);
CGRect imagebounds=CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginPDFContextToFile(PDFPath, pagebounds, nil);
{
UIGraphicsBeginPDFPage();
[image drawInRect:imagebounds];
}
}
UIGraphicsEndPDFContext();
}
but it is not generating pdf file and getting error can any body help me please...
Upvotes: 1
Views: 996
Reputation: 117
According to @kanan Vora i have solved my issue with this code
{
CGSize pageSize = CGSizeMake(button.frame.size.width*5, button.frame.size.height*5+600);
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);
NSArray *arrImages = [NSArray arrayWithObjects:@"1.png", @"2.png", @"1.png",@"2.png", @"1.png", nil];
float y = 220.0;
for (int i=0; i<arrImages.count; i++) {
UIImage * myPNG = [UIImage imageNamed:[arrImages objectAtIndex:i]];
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0.0, pageSize.width, pageSize.height), nil);
[myPNG drawInRect:CGRectMake(120.0, y, myPNG.size.width, myPNG.size.height)];
// y -= myPNG.size.height+1;
}
UIGraphicsEndPDFContext();
}
Upvotes: 1