reading PDF protected by password using UIWebview - ios

I'm using this code to load my PDF into webView:
NSURL *targetURL = [NSURL URLWithString:#"http://example.com/demo.pdf"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:targetURL];
[viewWeb loadRequest:requestObj];
and everything works fine, but there's one problem to resolve.
My demo.pdf is protected by password, of course I know this password but don't want to input it everytime when pdf is loaded.
Can I set my password as variable/string and load PDF without type password?

I don't think there's anything as straight forward as telling the webview what the password is, but instead I would download the PDF file manually (NSURLConnection or something), then unlock it using CGPDFDocumentUnlockWithPassword(doc, pass).
You could then generate an unlocked version as NSData in memory and load that into the web view (not password protected at this point)
You can draw it into NSData using the following (skip over the parts about custom drawing a red box on top):
http://b2cloud.com.au/how-to-guides/drawing-over-a-pdf-in-ios-pdf-template
Of course if your PDF is huge or has a ton of images this could take some time...
CGPDFDocument reference:
https://developer.apple.com/library/ios/documentation/graphicsimaging/reference/CGPDFDocument/Reference/reference.html

Related

How to open .csv link in UIWebView

I want to open .csv link in the UIWebView. I am getting link some .csv links from the web how to open that link in the UIWebView. I am using the below code
NSString *urlString = #"https://xoxoengage-images-test.s3.amazonaws.com/image/clients/gxoxo/annoucement/XXXXdetails201805021526899124.csv";
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];//[NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[_webviewobj loadRequest:urlRequest];
but the result is shown in the below image, help me out to sort this issue.
The UIWebView doesn't support the CSV format natively.
See this: File Formats Supported by UIWebView
If your server returns "text/plain" (or you have this file as ".txt") you'll see its raw text in the web view.
If you want a grid table view, then you need to convert csv to to one of the supported formats. The obvious choice would be HTML.
If you can convert on the server side - that's better, otherwise download the csv file, parse it line by line, and output as HTML. Load the HTML into the web view. loadHTMLString is what you could use.
Also with UIWebView this can be done sort of transparently using a custom NSURLProtocol.
This might be helpful: Where can I find a CSV to NSArray parser for Objective-C?

How to convert a PDF file (contains text,images, may be interactive forms) to HTML string in iOS?

Is there any way to convert PDF file which contains text,images & may be some interactive form to a html string?
By converting it to html string, I can display that file in WebView & achieve annotation & bookmark without third party library.
And convert that HTML back to PDF file.
Thanks
You can load PDF file directly in UIWebView. Not only PDF, UIWebview supports multiple formats. So there is no need to convert it into html string.
NSURL *url = [NSURL fileURLWithPath:pdfPath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];

Encrypted pdf files crashes on UIWebView in iOS 7

I am trying to open pdf files in my UIWebView in below steps.
self.docViewer is a strong property.
self.docViewer.delegate = self;
NSURLRequest *urlReq = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:docurl]];
[self.docViewer loadRequest:urlReq];
It opens normal pdf files and password protected files with no issues.
When the file is an encrypted one, it crashes after executing delegate methods.
- (void)webViewDidFinishLoad:(UIWebView *)webView
with below log
unsupported security handler `Adobe.PubSec'.
FlateDecode: decoding error: incorrect header check.
FlateDecode: decoding error: incorrect header check.
unsupported security handler `Adobe.PubSec'.
I checked all possible ways mentioned in suggested treads. I didn't find any solution.
Could anyone please help me out of this crash.Thanks a Lot

hyperlink not working in a pdf in a UIWebView

I have text file (instructions.docx) with a hyperlink in it - I convert it to a pdf (instructions.pdf) and use it in a UIWebView as follows:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Instructions" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
[_webView setScalesPageToFit:YES];
All works fine except the hyperlink does not work - shows blue - but tapping it does nothing.
Am I going about this the wrong way - or am I missing something?
I'm not sure, but you can try this line of code:
[_webView setDataDetectorTypes:UIDataDetectorTypeLink];
Otherwise, you can try Reader library by vfr. PDF embedded links work in it.
Also you may want to take a look at this question.
I had a similar problem. Turns out the links in the PDF weren't hyperlinks. They were just typed in (even though they were blue and looked like real hyperlinks).
Resolved the issue by:
Manually adding hyperlinks
Checked Word to PDF conversion allowed for "Best for electronic distribution and accessibility"

Clear UIWebView cache when use local image file

I use a UIWebView to load a local html, and there is a PNG file inside the html created by Objc.
After the PNG file has been modified, I reload the html in UIWebView, but the image doesn't change. However, if I quit the app and reopen it, the image file will be changed to the new one.
I have checked the PNG file in Documents with Finder, so I'm sure it has been modified, but the old one is still in UIWebView.
So, as I think that it's a UIWebView cache problem, I've tried:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:url isDirectory:NO ] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:1]]; or NSURLRequestReloadIgnoringCacheData
None of them works, and I can't change the filename, because the PNG file is used in a lot of places (other html and objc code).
I've tried this too:
some.png?r=randomNumber
but it can't be showed.
How do I clear the UIWebView cache when using a local image file inside a local html?
Other than renaming every file on each access, I've only seen one thing work for this and that is modifying the HTML with javascript to add a timestamp onto the image url so it tricks the webview into thinking it's a different file. Images (usually) load the same no matter what you put after the ? in their url. I think this would be easier than renaming every file each time you load the web view. Something like this (using jQuery):
<img src="myimg.jpg" />
<script>
$(function() {
$('img').each(function(i, el) {
$(el).attr('src', $(el).attr('src')+'?pizza='+(new Date()).getTime());
});
});
</script>
I guess this is assuming that this javascript loads and runs before the images are loaded, but in theory this should work.
For a little background, I've made a page in a webview that used RequireJS to asynchronously load quite a few files. I had the EXACT same problem that this question is talking about except that I was loading javascript files instead of images. The key to fixing this issue was adding a timestamp to every path of javascript file and thus tricking the webview (ex me.js?ts=236136236 and whatever.js?ts=3153524623). I found this to work great.
One other thing I needed to do was add a timestamp to the path of my HTML file when loading it into the webview like this:
[NSURL URLWithString:[[NSString stringWithFormat:#"%#/index.html?pizza=%f", webDirectoryPath, [[NSDate date] timeIntervalSince1970]]
I now can modify all the local files and each time the webview appears the changes come through.
You can try this, in your AppDelegate.m
+(void)initialize {
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
}
If your html didn't change and the only change was in image you should use UIWebView's reload message instead of loading request again.
Something like this:
- (void)reloadWebView:(id)sender
{
if (self.loaded) {
[self.webView reload];
} else {
NSString *html = [[NSBundle mainBundle] pathForResource:#"web" ofType:#"html"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:html]];
[self.webView loadRequest:request];
self.loaded = YES;
}
}
You don't even need any manipulations with cache.

Resources