Is PdfContext coordinates inverse????

Discussion in 'iOS Programming' started by AbhishekApple, Dec 17, 2010.

  1. AbhishekApple, Dec 17, 2010
    Last edited: Dec 17, 2010

    AbhishekApple macrumors member

    Joined:
    Aug 5, 2010
    #1
    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
    Code:
    
    -(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
    Code:
    <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.
     
  2. Andreas Grauel macrumors newbie

    Joined:
    Dec 1, 2010
    Location:
    Bonn, Germany
    #2
    That's correct the origin is on the bottom left and not the upper left.
     
  3. AbhishekApple thread starter macrumors member

    Joined:
    Aug 5, 2010
    #3
    I got one solution but it isn't working

    Code:
    // Flip the context vertically
    CGContextTranslateCTM(pdfContext, 0.0, pageRect.size.height);
    CGContextScaleCTM(pdfContext, 1.0, -1.0);
    Here is how i implemented the above code
    Code:
    
    -(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);
    [COLOR="DarkOrange"]
    
    			NSData *imageData = [[NSData alloc] initWithBytes:sqlite3_column_blob(selectStmt, 5) length:blobLength];
    	
    			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);
    			
    			
    			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));
    		}[/COLOR]
    	}
    
    	CGContextEndPage (pdfContext);
    	CGContextRelease (pdfContext);
    	
    	NSLog(@"Pdf created");
    	
    }
    
     

Share This Page