Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

AbhishekApple

macrumors member
Original poster
Aug 5, 2010
86
0
Hi adam

This is how i generated Multiple page pdf

I have stored the product details in array aryPrdCode,aryPrdName,aryPrdImageData which i want to draw on pdf

Rest follow the comments..
Happy coding
Code:
-(void)savePdf{

	
	NSLog(@"Create Pdf");
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *saveDirectory = [paths objectAtIndex:0];
	NSString *filename =@"abc";
	filename = [filename stringByAppendingString:@".pdf"]; 
	NSLog(@"Filename= %@",filename);
	NSString *saveFileName = filename;
	NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
	const char *filenm = [newFilePath UTF8String];
	
	CGRect pageRect = CGRectMake(0, 0, 612, 700);
	// This code block sets up our PDF Context so that we can draw to it
	CGContextRef pdfContext;
	CFStringRef path;
	CFURLRef url;
	CFMutableDictionaryRef myDictionary = NULL;
	// Create a CFString from the filename we provide to this method when we call it
	path = CFStringCreateWithCString (NULL, filenm,
									  kCFStringEncodingUTF8);
	// Create a CFURL using the CFString we just defined
	url = CFURLCreateWithFileSystemPath (NULL, path,
										 kCFURLPOSIXPathStyle, 0);
	NSLog(@"path= %@",path);
	CFRelease (path);
	// This dictionary contains extra options mostly for 'signing' the PDF
	myDictionary = CFDictionaryCreateMutable(NULL, 0,
											 &kCFTypeDictionaryKeyCallBacks,
											 &kCFTypeDictionaryValueCallBacks);
	CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
	CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
	// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
	pdfContext = CGPDFContextCreateWithURL(url, &pageRect, myDictionary);
	
	//CGContextTranslateCTM(pdfContext, 0.0, pageRect.size.height);
	//CGContextScaleCTM(pdfContext, 1.0, -1.0);
	
	NSLog(@"%@",url);
	CFRelease(myDictionary);
	CFRelease(url);


	//pdf page creation code starts here…

	//I have stored the product details in array aryPrdCode,aryPrdName,aryPrdImageData

	int count = [aryPrdCode count];
	int perPage = 4; //Number of products to be shown per page
	float pages = ((float)count / (float)perPage);
	pages = ceilf(pages);

	NSLog(@"Count = %d",count);
	NSLog(@"PerPage = %d",perPage);
	NSLog(@"Ceil Page = %f",pages);
	
	int itemCount=0;//to keep the count of products which we draw on pdf

	for (int i=0; i < pages; i++) {
		
		NSLog(@"page no=%d",i);
		// Starts pdf context for every page
		CGContextBeginPage (pdfContext, &pageRect);
		
		for (int j=0 ; j<perPage ; j++) {
	
			if(itemCount==count)
				break;
			
			NSLog( @"Item Namess = %@",[aryPrdName objectAtIndex:itemCount]);
		
			
			//NSData *imageData = [NSData dataWithBytes:[Dictionary getaryTmpImageData] length:]
			UIImage *img = [UIImage imageWithData:[aryPrdImageData objectAtIndex:itemCount]];
			CGImageRef image=[img CGImage];

		CGContextSelectFont (pdfContext, "Helvetica", 12, kCGEncodingMacRoman); // this code generates some error check the error below think coz it is unable to find font
			CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
			CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
			const char *text = [[aryPrdName objectAtIndex:itemCount] UTF8String];
			const char *code = [[aryPrdCode objectAtIndex:itemCount] UTF8String];

			const char *logo = "Header Name";
			
			CGPoint itemLoc = [self getItemFrame:j];  // get the location where the product needs to be drawn

			NSLog(@"itemLoc x=%f y=%f",itemLoc.x,itemLoc.y);
			NSLog(@"Coordinates= x=%f y=%f w=%f h=%f",itemLoc.x+20, pageRect.size.height-(itemLoc.y+150), 135, 135);
			
			CGContextDrawImage (pdfContext,CGRectMake( itemLoc.x+20, pageRect.size.height-(itemLoc.y+150), 135, 135) ,image);
			CGContextShowTextAtPoint (pdfContext, itemLoc.x+20 , pageRect.size.height-(itemLoc.y+150) , text, strlen(text));
			CGContextShowTextAtPoint (pdfContext, itemLoc.x+20 , pageRect.size.height-(itemLoc.y+170) , code, strlen(code));
				
			CGContextShowTextAtPoint (pdfContext, 80 , pageRect.size.height-50 , logo, strlen(logo));
					
			itemCount++;
		}
		//End our page
		CGContextEndPage (pdfContext);
	}
	
	// We are done with our context now, so we release it
	CGContextRelease (pdfContext);
}


-(CGPoint)getItemFrame:(int)location{

		switch (location) {
			case 0:
				return CGPointMake(50, 50);
				break;
			case 1:
				return CGPointMake(350, 50);
				break;
			case 2:
				return CGPointMake(50, 350);
				break;
			case 3:
				return CGPointMake(350, 350);
				break;
		}
	return CGPointMake(0, 0);
}

maybe this error generated coz it din't find the font......if any one can suggest
Wed Dec 22 11:13:09 Admins-iMac-3.local PCB[298] <Error>: CGFont/Freetype: The function `create_subset' is currently unimplemented.
Wed Dec 22 11:13:09 Admins-iMac-3.local PCB[298] <Error>: invalid Type1 font: unable to stream font.
Wed Dec 22 11:13:09 Admins-iMac-3.local PCB[298] <Error>: FT_Load_Glyph failed: error 6.
Wed Dec 22 11:13:09 Admins-iMac-3.local PCB[298] <Error>: FT_Load_Glyph failed: error 6.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.