I am making a pdf using UIGraphicsBeginPDFPageWithInfo from images,then loading that pdf into a UIWebView.
I want to select some text from that pdf and show it on a textview. Here is the code sample I am using to load a pdf on a web view.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"images.pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:filePath];
NSLog(#"pdf URL :: %#",pdfUrl);
NSURLRequest *request = [NSURLRequest requestWithURL:pdfUrl];
[_webView loadRequest:request];
I have tried using the following, but it is returning a blank string.
NSString *innerText = [_webView stringByEvaluatingJavaScriptFromString:#"document.body.innerText"];
edit:: I want the text selection functionality as implemented in ibooks.
can you see this example in gitHub: https://github.com/KurtCode/PDFKitten
Related
This question is kind of self-explanatory but I'll explain it anyways. For some reason when I try and load a new web page it will see the web page URL except it won't load it the first time you press the button. Only when you press the button again will it actually load. How do I fix this?
Here's the code for my button:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
// Download and write to file
NSURL *url = [NSURL URLWithString:_varString];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
//Load file in UIWebView
[_Hoot loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
*Also note, even though I'm copying the web page here and then loading it from a file, I still get the same problem if I'm just loading it from a URL.
Try this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
// Download and write to file
NSURL *url = [NSURL URLWithString:_varString];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
//Load file in UIWebView
NSURL *urlen = [NSURL fileURLWithPath:filePath];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlen];
[_Hoot loadRequest:urlRequest];
In one of my activity, I displayed a PDF using WebView on screen and tried to save this PDF using this code:
_pageSize = CGSizeMake(width, height);
NSString *newPDFName = [NSString stringWithFormat:#"%#", name];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:newPDFName];
UIGraphicsBeginPDFContextToFile(pdfPath, CGRectZero, nil);
NSLog(#"path=%#",pdfPath);
When I run this code in iOS simulator (using Xcode) and show the path and I opened this PDF file successfully in documents folder, but when I run this code in an iPhone, I got this path:
/var/mobile/Applications/0AF98361-C8DF-4C35-9E9F-EE48555185BC/Library/354746396.pdf
So where are PDF files stored in iPhone?
"..InDomains(NSLibraryDirectory, NSUserDomainMask, YES);"
"../Library/354746396.pdf"
Your paths is looking in the NSLibraryDirectory, it should be looking in the NSDocumentDirectory like this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
If still fail, try this code:
NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *savePath = [documents stringByAppendingPathComponent:#"filename.pdf"];
NSLog(#"savePath: %#", savePath);
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 367)];
webView.scalesPageToFit = YES;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"myPDF11.pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
}
my coding is right but i will not find the pdf file.i will solve my problem.i will find my pdf file in device.
there are two ways to find the pdf file.
1)install the iexplorer filemanager software.than open document folder.
2)connect your device in to the mac.select window menu and click the device in xcode.than after select your device name.select your app.
select show container.than after you can see the document folder....
I would like to load html files, which are downloaded into document folder. First page is loaded ok, but links to second page not works and images not work too. I am using this code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *path = [documentDirectory stringByAppendingPathComponent:#"index.htm"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[pruvodceWebView loadHTMLString:htmlString baseURL:nil];
Is there any reason, why webview do this? Thank you
The problem is that you have HTML source but not images
Instead of
[pruvodceWebView loadHTMLString:htmlString baseURL:nil];
You should define baseURL. If your images are in documents directory
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[pruvodceWebView loadHTMLString:htmlString baseURL:baseURL];
Or provide path to images if they have relative path
[pruvodceWebView loadHTMLString:htmlString baseURL:[NSURL URLWithString#"http://yourSiteHere.com/"]];
Check out this:
Load resources from relative path using local html in uiwebview
I tried to show an html page that is inside my app, but it did not show on the view:
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask ,YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"abt.html"];
NSURLRequest *documentsRequest = [NSURLRequest requestWithURL:
[NSURL fileURLWithPath:path]] ;
[_webview loadRequest:documentsRequest] ;
I followed this link: iOS - display content from an html resource file or remote webpage in a webview
by Matt Gibson
Please let me know how to show.
thanks
As you said your html page is inside the app so try this one, I have used it and it's work fine.
NSString *htmlFile=[[NSBundle mainBundle] pathForResource:#"abt" ofType:#"html"];
NSString *htmlString=[NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:Nil];
[_webview loadHTMLString:htmlString baseURL:nil];
To serialize the pdf I believe you could use NSData dataWithURL: and store in Core Data. I'm unsure as to how you'd deserialize back to a pdf however and view it with UIWebView.
You can use CGPDFDocumentCreateWithProvider:
If your NSData reference is pdfData then:
CFDataRef myPDFData = (CFDataRef)pdfData;
CGDataProviderRef provider = CGDataProviderCreateWithCFData(myPDFData);
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(provider);
But I guess you just want to display it in a web view and the above is not what you need.
However if you already have the url wouldn't it be easier to store the URL rather than the pdf as such.
Also if you have the data you can write it into a file inside your documents directory temporarily and then use web view to load this url.
i.e;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:#"New1.pdf"];
NSURL *fileURL = [NSURLRequest requestWithURL:appFile];
[data writeToURL:fileURL atomically:YES];
and then :
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[webView loadRequest:request];
And if you want to remove the file from the documents directory you could use:
[[NSFileManager defaultManager] removeItemAtURL:fileUrl error:nil];
to remove the temporary pdf you created.