Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-10-2011, 10:09 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 1
Ankush Mittal is on a distinguished road
Default PDF Generation from UIScrollView

Hello All,

I want to generate a pdf of UIScrollView whose content size is near about 320*2000. if i use current graphic image context to capture the scroll view layer, then it only captures the visible part of the scroll view not the whole layer of that scroll view.

i am using the below code.
Code:
-(void)CreatePdf:(id)sender
{
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *saveDirectory = [paths objectAtIndex:0];
	NSString *saveFileName = @"myPDF.pdf";
	[self writePDFAma];
	CGRect arect=CGRectMake(0, 0, 768, 1024);
	
	CreatePDFFileAma(arect, nil);
}



-(void) writePDFAma
{
	UIImage *image;
	NSData *data;
	UIAlertView *successAlert;
	UIAlertView *failureAlert;
	//	
	NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	//	// This should be our documents directory
	NSString *saveDiirectory = [documentPath objectAtIndex:0];
	//	// Our PDF is named 'Example.pdf'
	NSString *saveFileName = @"FormImage2.JPG";
	//	// Create the full path using our saveDirectory and saveFileName
	NSString *finalPath = [saveDiirectory stringByAppendingPathComponent:saveFileName];
	//NSLog(@"%@",finalPath);
	CGSize asize=CGSizeMake(_scrollview.frame.size.width, _scrollview.frame.size.height);
	//NSLog(@"%d..%d",mainViewAma.frame.size.width, mainViewAma.frame.size.height);
	UIGraphicsBeginImageContext(asize);
		[[_scrollview layer] renderInContext:UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
	data = UIImageJPEGRepresentation(image, 1.0);
	
	BOOL catch;
    if(!catch){
		if ([[NSFileManager defaultManager] createFileAtPath:finalPath contents:data attributes:nil])
		{
			successAlert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Doodle was successfully saved to the Photo Library." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
			//[successAlert show];
			[successAlert release];
		} else {
			failureAlert = [[UIAlertView alloc] initWithTitle:@"Failure" message:@"Failed to save doodle to the Photo Library." delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
			//[failureAlert show];
			[failureAlert release];
		}
		
	}
	
}


void CreatePDFFileAma (CGRect pageRect, const char *filename)
{
	// This code block sets up our PDF Context so that we can draw to it
	//some code here
	CGContextRef pdfContext;
    CFURLRef url;
    CFMutableDictionaryRef myDictionary = NULL;
  	NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	//	// This should be our documents directory
	NSString *saveDirectory = [documentPath objectAtIndex:0];
	//	// Our PDF is named 'Example.pdf'
	
	
	
	//NSString *saveFileName = @"PDFForm2.pdf";
	
	
	
	//	// Create the full path using our saveDirectory and saveFileName
	NSString *finalPath = [saveDirectory stringByAppendingPathComponent:saveFileName];
	NSURL * aurl=[[NSURL alloc]initFileURLWithPath:finalPath];
	url=(CFURLRef)aurl;
    // This dictionary contains extra options mostly for 'signing' the PDF
    myDictionary = CFDictionaryCreateMutable(NULL, 0,
                                             &kCFTypeDictionaryKeyCallBacks,
                                             &kCFTypeDictionaryValueCallBacks);
    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("AMA FORM PDF"));
    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name2"));
    // Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary
    pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
    // Cleanup our mess
    CFRelease(myDictionary);
    CFRelease(url);
    // Starts our first page
    CGContextBeginPage (pdfContext, &pageRect);
	
    // Draws a black rectangle around the page inset by 50 on all sides
    CGContextStrokeRect(pdfContext, CGRectMake(50, 50, 768, 1024));
	
    // This code block will create an image that we then draw to the page
    CGImageRef image;
    CGDataProviderRef provider;
    CFURLRef pictureURL;
	documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	//	// This should be our documents directory
	saveDirectory = [documentPath objectAtIndex:0];
	NSString *saveFileName2 = @"FormImage2.JPG";
	//	// Create the full path using our saveDirectory and saveFileName
	NSString *finalPath2 = [saveDirectory stringByAppendingPathComponent:saveFileName2];
	NSURL * aurl2=[[NSURL alloc]initFileURLWithPath:finalPath2];
	pictureURL=(CFURLRef)aurl2;
	provider = CGDataProviderCreateWithURL (pictureURL);
    CFRelease (pictureURL);
    image = CGImageCreateWithJPEGDataProvider(provider, NULL, TRUE, kCGRenderingIntentDefault); //DataProvider (, NULL, true, kCGRenderingIntentDefault);
    CGDataProviderRelease (provider);
    CGContextDrawImage (pdfContext, CGRectMake(0, 0,768, 1024),image);
    CGImageRelease (image);
    CGContextEndPage (pdfContext);
    // We are done with our context now, so we release it
    CGContextRelease (pdfContext);
}

Thanks
Ankush Mittal


i would realy appreciate if anyone can show the track or provide me some refrence code or links to solve my problem
Ankush Mittal is offline   Reply With Quote
Old 03-12-2011, 12:17 PM   #2 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 38
macdudly is on a distinguished road
Default

is it possible to see your sample code, I am trying to do the same exactly thing.

you can send the source code here, thanks for that
dott.marco@mac.com

anyway for your problem maybe this can help:




You are capturing the visible content only because your scroll view is sized match the visible content only, which it should be. You need to resize your scrollview to match the content you need to capture before you start the drawing code using something like this:

Code:
self.scrollView.frame = CGRectMake(0, 0, "your content width here", "your content height here");
After you are done drawing make sure to resize your scroll view to back, in this case to the container view:

Code:
self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
t worked on my iPad app. I hope it works for you.
macdudly is offline   Reply With Quote
Old 02-29-2012, 12:01 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2011
Location: india
Posts: 2
ravoorinandan is on a distinguished road
Default

Hi Currently am having images and textview inside a scrollview i just need to convert all these things/contexts into pdf context could any one help by steering me in the right direction???thanks in advance.
ravoorinandan is offline   Reply With Quote
Reply

Bookmarks

Tags
code, generate, pdf, pdfcontext, scrollview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 372
12 members and 360 guests
Absentia, akphyo, apatsufas, BinHex, fredidf, gmarro, jeroenkeij, Kirkout, MarkC, mottdog, whitey99, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,667
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, host number one
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:22 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0