Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Are CGPdfContext coordinates inverse???

AbhishekAppleAbhishekApple Posts: 72Registered Users
edited December 2010 in iPhone SDK Development
I am trying to create pdf but if i draw an image at x=10 & y=10 it draws at the bottom of the pdf....

I am struggling a lot to arrange the images and text below the image in the pdf....

Please help here is my code,,,, its bit long please see the orange part if u hv less time
-(void)createPdf{
	NSLog(@"Create Pdf");
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *saveDirectory = [paths objectAtIndex:0];
	NSString *saveFileName = @"myPDF.pdf";
	NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
	const char *filename = [newFilePath UTF8String];
	
	CGRect pageRect = CGRectMake(0, 0, 612, 700);
	CGContextRef pdfContext;
	CFStringRef path;
	CFURLRef url;
	CFMutableDictionaryRef myDictionary = NULL;
	path = CFStringCreateWithCString (NULL, filename,
									  kCFStringEncodingUTF8);
	url = CFURLCreateWithFileSystemPath (NULL, path,
										 kCFURLPOSIXPathStyle, 0);
	NSLog(@"path= %@",path);
	CFRelease (path);
	myDictionary = CFDictionaryCreateMutable(NULL, 0,
											 &kCFTypeDictionaryKeyCallBacks,
											 &kCFTypeDictionaryValueCallBacks);
	CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
	CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
	pdfContext = CGPDFContextCreateWithURL(url, &pageRect, myDictionary);
	NSLog(@"%@",url);
	CFRelease(myDictionary);
	CFRelease(url);

	CGContextBeginPage (pdfContext, &pageRect);
	const char *sql = "select pageno,rectframe,prod_name,prod_code,prod_imagename,prod_image from tblCatlogDetail where catmaster_id=1";
	//sqlite3_stmt *selectStmt;
	if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK) {
		
		[COLOR="orange"]while(sqlite3_step(selectStmt) == SQLITE_ROW) {
			NSLog(@"inside select");
			
			NSString *rect = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStmt, 1)];
			NSArray *arryRect = [rect componentsSeparatedByString:@","];
			float x = [[arryRect objectAtIndex:0] floatValue];
			float y = [[arryRect objectAtIndex:1] floatValue];
			float w = [[arryRect objectAtIndex:2] floatValue];
			float h = [[arryRect objectAtIndex:3] floatValue];
			
			NSString *prd_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStmt, 2)];
			
			NSUInteger blobLength = sqlite3_column_bytes(selectStmt, 5);
			
			NSData *imageData = [[NSData alloc] initWithBytes:sqlite3_column_blob(selectStmt, 5) length:blobLength];
			//const char *picture = "Picture";
			UIImage *img = [UIImage imageWithData:imageData];
			
			CGImageRef image=[img CGImage];
			CGContextDrawImage (pdfContext,CGRectMake( x+55, pageRect.size.height-(y+150), 135, 135) ,image);
			//CGContextDrawImage (pdfContext,CGRectMake( x, y, 135, 135) ,image); 
                          // if i use above commented line it draws in non desired location 
			
			NSLog(@"%@ ==> x=%f y=%f x=%f h=%f",prd_name ,x ,y , w, h);
			//CGImageRelease (image);
			[imageData release];

			CGContextSelectFont (pdfContext, "Helvetica", 16, kCGEncodingMacRoman);
			CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
			CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
			const char *text = [prd_name UTF8String];
			CGContextShowTextAtPoint (pdfContext, x+55,pageRect.size.height-(y+150+135) , text, strlen(text));
		}[/COLOR]
	}
	
	CGContextEndPage (pdfContext);
	CGContextRelease (pdfContext);
	
	NSLog(@"Pdf created");
	
}

Also i get this error if i set font...i searched for this but no luck
<Error>: CGFont/Freetype: The function `create_subset' is currently unimplemented.
Fri Dec 17 17:34:05 Admins-iMac-3.local PCB[4937] <Error>: invalid Type1 font: unable to stream font.
Fri Dec 17 17:34:05 Admins-iMac-3.local PCB[4937] <Error>: FT_Load_Glyph failed: error 6.
Post edited by AbhishekApple on

Replies

  • ChrisLChrisL Posts: 580Registered Users @ @ @
    edited December 2010
    Yes, the y-axis is flipped in PDF and bitmap contexts (see here in the Quartz2D programming guide). It's easy to flip the coordinate system around to match the usual UIKit coordinates, though, by modifying the context's transform before you draw, e.g.:
    // Flip the context vertically
    CGContextTranslateCTM(pdfContext, 0.0, pageRect.size.height);
    CGContextScaleCTM(pdfContext, 1.0, -1.0);
    
    Alternatively, the UIImage class has methods, like drawInRect: or drawAtPoint:, that directly draw the image to the current graphics context. I believe that these methods already take the flipped coordinates into account, so you don't need to worry about modifying the context's transform if you use them.
  • AbhishekAppleAbhishekApple Posts: 72Registered Users
    edited December 2010
    ChrisL wrote: »
    Yes, the y-axis is flipped in PDF and bitmap contexts (see here in the Quartz2D programming guide). It's easy to flip the coordinate system around to match the usual UIKit coordinates, though, by modifying the context's transform before you draw, e.g.:
    // Flip the context vertically
    CGContextTranslateCTM(pdfContext, 0.0, pageRect.size.height);
    CGContextScaleCTM(pdfContext, 1.0, -1.0);
    
    Alternatively, the UIImage class has methods, like drawInRect: or drawAtPoint:, that directly draw the image to the current graphics context. I believe that these methods already take the flipped coordinates into account, so you don't need to worry about modifying the context's transform if you use them.


    Hi ChrisL thanks for the information....
    but after doing the changes its still not working....heres wht i did....
    
    -(void)createPdf{
    	NSLog(@"Create Pdf");
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	NSString *saveDirectory = [paths objectAtIndex:0];
    	NSString *saveFileName = @"myPDF.pdf";
    	NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];
    	const char *filename = [newFilePath UTF8String];
    	
    	CGRect pageRect = CGRectMake(0, 0, 612, 700);
    	CGContextRef pdfContext;
    	CFStringRef path;
    	CFURLRef url;
    	CFMutableDictionaryRef myDictionary = NULL;
    	path = CFStringCreateWithCString (NULL, filename,
    									  kCFStringEncodingUTF8);
    	url = CFURLCreateWithFileSystemPath (NULL, path,
    										 kCFURLPOSIXPathStyle, 0);
    	NSLog(@"path= %@",path);
    	CFRelease (path);
    	myDictionary = CFDictionaryCreateMutable(NULL, 0,
    											 &kCFTypeDictionaryKeyCallBacks,
    											 &kCFTypeDictionaryValueCallBacks);
    	CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
    	CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
    [COLOR="DarkOrange"]
    pdfContext = CGPDFContextCreateWithURL(url, &pageRect, myDictionary);
    	
    	CGContextTranslateCTM(pdfContext, 0.0, pageRect.size.height);
    	CGContextScaleCTM(pdfContext, 1.0, -1.0);
    	[/COLOR]
    	NSLog(@"%@",url);
    	CFRelease(myDictionary);
    	CFRelease(url);
    
    	CGContextBeginPage (pdfContext, &pageRect);
    		
    	const char *sql = "select pageno,rectframe,prod_name,prod_code,prod_imagename,prod_image from tblCatlogDetail where catmaster_id=1";
    	//sqlite3_stmt *selectStmt;
    	if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK) {
    		
    		while(sqlite3_step(selectStmt) == SQLITE_ROW) {
    			NSLog(@"inside select");
    			
    			NSString *rect = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStmt, 1)];
    			NSArray *arryRect = [rect componentsSeparatedByString:@","];
    			float x = [[arryRect objectAtIndex:0] floatValue];
    			float y = [[arryRect objectAtIndex:1] floatValue];
    			float w = [[arryRect objectAtIndex:2] floatValue];
    			float h = [[arryRect objectAtIndex:3] floatValue];
    
    			NSString *prd_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStmt, 2)];
    			
    			NSUInteger blobLength = sqlite3_column_bytes(selectStmt, 5);
    
    			NSData *imageData = [[NSData alloc] initWithBytes:sqlite3_column_blob(selectStmt, 5) length:blobLength];
    	
    			[COLOR="DarkOrange"]UIImage *img = [UIImage imageWithData:imageData];
    			
    			CGImageRef image=[img CGImage];
    			//[img drawInRect:CGRectMake( x, y, 135, 135)];
    			
    			//CGContextDrawImage (pdfContext,CGRectMake( x+20, pageRect.size.height-(y+150), 135, 135) ,image);
    			CGContextDrawImage (pdfContext,CGRectMake( x, y, 135, 135) ,image);
    			[/COLOR]
    			
    			NSLog(@"%@ ==> x=%f y=%f x=%f h=%f",prd_name ,x ,y , w, h);
    
    			[imageData release];
    	
    			CGContextSelectFont (pdfContext, "Helvetica", 16, kCGEncodingMacRoman);
    			CGContextSetTextDrawingMode (pdfContext, kCGTextFill);
    			CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);
    			const char *text = [prd_name UTF8String];
    			CGContextShowTextAtPoint (pdfContext, x , y+140 , text, strlen(text));
    		}
    	}
    
    	CGContextEndPage (pdfContext);
    	CGContextRelease (pdfContext);
    	
    	NSLog(@"Pdf created");
    	
    }
    
Sign In or Register to comment.