10-09-2010, 09:17 AM
#1 (permalink )
Registered Member
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Load PDF from documents directory
Hi all,
I am using the 'Leaves' framework to add a page turn effect to my app. It displays PDF's using quartz and this works fine if you are loading a PDF from the main bundle. Here is the working code:
Code:
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("SampleIssue.pdf"), NULL, NULL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
NSLog(@"PDF: %@", pdf);
CFRelease(pdfURL);
I want to load a PDF from the documents directory though. Here is my code (which doesn't work):
Code:
// Get Documents Directory
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [searchPaths objectAtIndex:0];
NSString *tempPath = [documentsDirectoryPath stringByAppendingPathComponent:[appDelegate.issueToLoad stringByAppendingPathExtension:@"pdf"]];
NSString *path = [tempPath stringByReplacingOccurrencesOfString:@"localhost/" withString:@""];
NSLog(@"PATH: %@", path);
//Display PDF
CFURLRef pdfURL = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)path, kCFURLPOSIXPathStyle, FALSE);
NSLog(@"PDF URL: %@", pdfURL);
pdf = CGPDFDocumentCreateWithURL(pdfURL);
NSLog(@"PDF: %@", pdf);
My code gets the correct link to the file. However the final NSLog shows the 'pdf' is NULL. This is obviously a problem and the PDF doesn't load. Can anybody see any obvious problems with my code? Or do you know how I can accomplish this?
Thanks!
03-04-2011, 05:41 AM
#2 (permalink )
Registered Member
Join Date: Sep 2010
Posts: 9
Bump!
Did you get this to work?! Really need this.
03-04-2011, 06:28 AM
#3 (permalink )
Registered Member
Join Date: Aug 2008
Location: Ireland
Age: 21
Posts: 472
Quote:
Originally Posted by
Jacos
Did you get this to work?! Really need this.
Ah, sorry I forgot to add the solution to the thread. Here is the code:
Code:
// Get Documents Directory
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [searchPaths objectAtIndex:0];
NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:[appDelegate.issueToLoad stringByAppendingPathExtension:@"pdf"]];
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the file exists
if ([fileManager fileExistsAtPath:path])
{
//Display PDF
CFURLRef pdfURL = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)path, kCFURLPOSIXPathStyle, FALSE);
pdf = CGPDFDocumentCreateWithURL(pdfURL);
leavesView.backgroundRendering = NO;
CFRelease(pdfURL);
[leavesView reloadData];
}
03-06-2011, 11:13 AM
#4 (permalink )
Registered Member
Join Date: Sep 2010
Posts: 9
Didn't solve my problem :(
Hey, tried this for a couple of days now, it don't work. I tried to implement this in my PDF code.
Code:
- (void)viewDidLoad {
[super viewDidLoad];
[scrollView removeFromSuperview];
[myContentView removeFromSuperview];
[tiledLayer removeFromSuperlayer];
self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
self.view.frame = CGRectMake(0, 0, 320, 411);
myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL URLWithString:@"9A.pdf"]);
NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:@"9A.pdf" withExtension:nil];
myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
myPageRef = CGPDFDocumentGetPage(myDocumentRef, 1);
tiledLayer = [CATiledLayer layer];
tiledLayer.delegate = self;
tiledLayer.tileSize = CGSizeMake(320, 411);
tiledLayer.levelsOfDetail = 100;
tiledLayer.levelsOfDetailBias = 100 ;
tiledLayer.frame = self.view.frame;
myContentView = [[UIView alloc] init];
myContentView.backgroundColor = [UIColor whiteColor];
myContentView.frame = CGRectMake(0, 0, 320, 411);
[myContentView.layer addSublayer:tiledLayer];
CGRect viewFrame = CGRectMake(0, 0, 320, 411);
viewFrame.origin = CGPointZero;
scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.frame = CGRectMake(0, 0, 320, 411);
scrollView.delegate = self;
scrollView.contentSize = self.view.frame.size;
scrollView.maximumZoomScale = 5;
[scrollView addSubview:myContentView];
[self.view addSubview:scrollView];
}
Have you any idea how I could load an PDF from the documents dir with this code...?
03-11-2011, 01:30 PM
#5 (permalink )
Registered Member
Join Date: Sep 2010
Posts: 9
Bump!
Hi, really would appreciate if you helped me out here!
Thanks in advance!
-----
Jacob L
03-11-2011, 03:10 PM
#6 (permalink )
Super Moderator
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
Jacos,
You really need to start a new thread if you are not using Leaves. The answer to this thread has already been posted.
08-28-2011, 10:14 AM
#7 (permalink )
Registered Member
Join Date: Aug 2011
Posts: 10
can you please tell me where did you put the code.. i've put it in the viewWillAppear and it didn't work and it didn't work on init method in pdfviewcontroller either so where exactly did you put it ?
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 343
10 members and 333 guests
alexP , gordo26 , headkaze , mistergreen2011 , nobstudio , Objective Zero , rayjeong , revg , Sloshmonster , v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs