Convert .png images to pdf in objective c - ios

Am having a set of images in document directory path,i need to convert all of my saved images into pdf with page number.am using the following code for converting pdf
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 64, 320,430)];
pdfData = [PDFImageConverter convertImageToPDF: imageVIew.image
withHorizontalResolution: 550 verticalResolution: 320];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent: #"Documents/new.pdf"];
NSLog(#"path %#",path);
[pdfData writeToFile:path atomically:NO];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
[self.view addSubview:webview];
In this code i can able to convert a single image into pdf.How to convert all of my images into pdf ?

Here is the tutorial to generate PDF from image and other assets: http://code.tutsplus.com/tutorials/generating-pdf-documents--mobile-11265 worked for me always.

Related

loadHTMLString from MainBundle or Documents Directory. Possible?

When showing a html file in a UIWebView, is it possible to first try to load the images from the DocumentsDirectory, and if it doesn't exist, load from the main bundle?
The reason is I dynamically load the html file from the server and store it locally, and want to do the same with the images. Often times, Images are added and the img tag code below will only load the image from the bundle, and not the documents directory where the downloaded images live.
Thanks in advance
<img src="myImage.jpg"/>
[self.webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
I think that what you need is this code
-(void) loadWebView
{
NSString *termsFilePath = [[NSBundle mainBundle] pathForResource:#"terms_conditions" ofType:#"html"];
NSError *err = nil;
NSString *html = [NSString stringWithContentsOfFile:termsFilePath
encoding:NSUTF8StringEncoding
error:&err];
[webView loadData:[html dataUsingEncoding:NSUTF8StringEncoding] MIMEType:#"text/html" textEncodingName:#"utf-8" baseURL:[NSURL URLWithString:#""]];
}
Hope this helps you
<img src="file:///Users/MyMac/Library/Developer/CoreSimulator/Devices/A6780B96-2F9C-418A-BE5C-5A8136A1B9C4/data/Containers/Data/Application/11245AB0-8736-4931-980C-951604F1CB8B/tmp/myImage.jpg">
As you can see, you need set the image path to src attribute of <image> tag. The path should be file://, I was try without file:// and image didn't show. Hope this helps.

Unable to read contents of .doc file in iOS

I am trying to read in arabic text that I have contained inside of a .doc file, and use it in my app. Unfortunately, the only way I am able to retrieve the text is if I convert the document into .txt file.
Here is the code I have:
NSError *error = nil;
NSString *path = #"MyArabicDocument";
NSString *root = [[NSBundle mainBundle]pathForResource:path ofType:#"doc"];
NSString *myFile = [NSString stringWithContentsOfFile:root encoding:NSUTF8StringEncoding error:&error];
NSLog(#"my file contents are: %#", myFile);
NSLog(#"error is: %#", error);
The output of my NSString object is (null), and the error I get is:
error is: Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x7aace470 {NSFilePath=/Users/MyName/Library/Developer/CoreSimulator/Devices/.../data/Containers/Bundle/Application/..MyApp.app/MyArabicDocument.doc}
If I convert my document into an .rtf format, then my output (after changing the extension in the above block of code) is the following:
my file contents are: {\rtf1\ansi\ansicpg1252\cocoartf1347\cocoasubrtf570
{\fonttbl\f0\fnil\fcharset0 LucidaGrande;\f1\fnil\fcharset178 AlBayan;\f2\fnil\fcharset178 GeezaPro;
}
{\colortbl;\red255\green255\blue255;}
\vieww10800\viewh8400\viewkind0
\deftab709
\pard\pardeftab709\pardirnatural
\f0\fs46 \cf0 1
\f1 - \'de\'f3\'dc\'c7\'e1\'f3 \'c7\'c8\'fa\'dc\'e4\'f5 \'c2\'c8\'f3\'f8 \'e6\'f3\'c7\'d3\'fa\'e3\'f5\'dc\'e5\'f5 \'e3\'f5\'cd\'f3\'e3\'f3\'f8\'dc\'cf\'f5
\f0 ~~~
\f1 \'c7\'e1\'e1\'e5\'f3 \'dd\'f6\'dc\'ed \'df\'f5\'dc\'e1\'f6\'f8 \'c7\'e1\'c3\'f5\'e3\'f5\'dc\'e6\'d1\'f6 \'c3\'f3\'cd\'fa\'dc\'e3\'f3\'dc\'cf\'f5 \
...
If I try to use an NSAttributedString object instead of an NSString object, but I still get a (null) value for my NSAttributedString object:
NSDictionary *attrs = #{NSDocumentTypeDocumentAttribute: NSRTFTextDocumentType, NSWritingDirectionAttributeName:#[#(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]};
NSAttributedString *text = [[NSAttributedString alloc] initWithFileURL:[[NSBundle mainBundle] URLForResource:#"MyArabicDocument" withExtension:#"doc"] options:attrs documentAttributes:nil error:&error];
The reason why this is important is that while my arabic text does indeed appear in my UITextView in my app, the problem is that it's appearance is nowhere near as nice as in the original document, which is what I would like to maintain in my app. Is this not possible?
.doc file in question is in binary format. (probably compressed like .docx)
http://en.wikipedia.org/wiki/Doc_(computing)
So you cannot put it in NSString as is. But you can get NSData:
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyArabicDocument" ofType:#"doc"];
NSData *data = [NSData dataWithContentsOfFile:path];
Unfortunately you cannot make an NSAttributedString from .doc in iOS, but you can in OS X (in iOS there only four doc types supported)
NSError *attrError;
NSDictionary *options = #{NSDocumentTypeDocumentAttribute: NSDocFormatTextDocumentType};
NSAttributedString *content = [[NSAttributedString alloc] initWithData:data options:options documentAttributes:nil error:&attrError];
Instead you may try to load your .doc file into WebView.
Using NSData:
[self.webView loadData:data MIMEType:#"application/msword" textEncodingName:#"UTF-8" baseURL:nil];
But I think better with NSURLRequest (since you don't nee to set up encoding there)
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
NOTE: Any method you choose very likely will BREAK your format, I mean rendered document will be corrupted. Instead I recommend to convert .doc to .pdf In this case it will be good-loking.
For example Dropbox app for iOS defenetly converts .doc/.docx to pdf and than presented to the user as PDF (Of course not telling that it is PDF indeed).
I think you have a encoding issue when reading a file,
Refer below link
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Strings/Articles/readingFiles.html
May be it solve your problem
Best of luck!

How can I add a image into uiwebview

In iOS, I need to load a website in uiwebview and I want to add a local image at the beginning of the webpage.
I added < img src="logo.png"/> after < body > tag but it only showed a null pic(a small white box).
How can I load my local image as the src of the img inside the html? Thanks
Using relative paths or file: paths to refer to images does not work with UIWebView. Instead you have to load the HTML into the view with the correct baseURL:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
You can then refer to your images like this:
(from uiwebview revisited)
You have to define the extact path of the image source see code below
NSString *imagePath=[[NSBundle mainBundle] pathForResource:#"image" ofType:#"png"];
imagePath=[NSString stringWithFormat:#"file://%#",imagePath];
NSString *html=[NSString stringWithFormat:#"<html><body><img src=\"%#\"/></body></html>",imagePath];
[webView loadHTMLString:html baseURL:nil];
Cheers.

View PDF file converted to BLOB image in UIImage or UIScroller

my App requirement is to view uploaded documents from IPAD.
i am importing document from database in the form of BLOB image and passing it to IPAD and with below code i could view image.
NSString *strImage; // String has BLOB image from DB
NSData *imageData = [self dataFromBase64EncodedString:strImage];
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *m_oImageViewer = [[UIImageView alloc]init];
m_oImageViewer.image = image;
m_oImageViewer.frame=CGRectMake((self.view.frame.size.width/2)- 250,60,500,500);
[self.view addSubview:m_oImageViewer];
-(NSData *)dataFromBase64EncodedString:(NSString *)string{
if (string.length > 0)
{
NSString *data64URLString = [NSString stringWithFormat:#"data:;base64,%#", string];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:data64URLString]];
return data;
}
return nil;
If PDF file converted to BLOB why i am getting image as nil, as i can see BLOB is common so why only .png files i can se and not PDF file even after converting to BLOB.
i read support document for UIImage it does not give PDF file but i am giving BLOB image. where i am going wrong.
i have to use any other property, or this is not possible.
PDF isn't image format, its something like container with text, fonts, vector and raster images etc, so you can't convert it into UIImage, but UIWebView can show pdf (and a lot another format). I think, you should save pdf BLOB to temp folder and then show it in UIWebView, something like:
[pdfData writeToFile:tempFilePath atomically:YES];
NSURL *url = [NSURL fileURLWithPath:tempFilePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

iOS 6 - Able to save .pdf from URL, able to display it in a WebView, NOT able to move it to iBooks

This is what I'm trying to do:
Get a .pdf from external URL
Save it into my local disk
Display it in a WebView
Allow the user to move the .pdf to another app who can read .pdf
Everything from 1 to 3 works fine. But nothing is moved/shared to/with other apps. I can't understand what I'm doing wrong. This is what I'm doing.
How I save the pdf in the Documents folder (viewDidLoad):
// to save the pdf into local file system (tempString is the pdf url)
NSData *pdfData = [[NSData alloc]
initWithContentsOfURL:[NSURL URLWithString:tempString]];
NSString *resourceToPath = [[NSString alloc]
initWithString:[[[[NSBundle mainBundle] resourcePath]
stringByDeletingLastPathComponent]
stringByAppendingPathComponent:#"Documents"]];
NSString *filePAth = [resourceToPath stringByAppendingPathComponent:#"myPDF.pdf"];
[pdfData writeToFile:filePAth atomically:YES];
// to populate the WebView
NSURL *url2 = [NSURL fileURLWithPath:filePAth];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];
[my_web_view setUserInteractionEnabled:YES];
//[editoriale_view setDelegate:self];
[my_web_view loadRequest:requestObj];
In my viewDidLoad() function I create a button to allow the user to open a list of apps who can read .pdf files:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self
action:#selector(show_Button)];
And here's my show_Button function:
-(void)show_Button {
NSString *resourceToPath = [[NSString alloc]
initWithString:[[[[NSBundle mainBundle] resourcePath]
stringByDeletingLastPathComponent]
stringByAppendingPathComponent:#"Documents"]];
NSString *filePAth = [resourceToPath
stringByAppendingPathComponent:#"myPDF.pdf"];
NSLog(#"filePath = %#", filePAth);
NSURL *url2 = [NSURL fileURLWithPath:filePAth];
NSLog(#"url2 = %#", url2);
UIDocumentInteractionController *docContr = [UIDocumentInteractionController
interactionControllerWithURL:url2];
[docContr presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
When I try this on my device everything works fine until I tap on one of the icons in the list (i.e. the iBooks one). Then the app closes (it doesn't crash, it simply closes).
Here's what the console prints for the two logs I put in the show_Button function:
1. filePath = /Users/[MY_USER]/Library/Application Support/iPhone
Simulator/6.1/Applications/[MY_EXAD_APP_ID]/Documents/myPDF.pdf
2. url2 = file://localhost/Users/[MY_USER]/Library/Application%20Support/
iPhone%20Simulator/6.1/Applications/[MY_EXAD_APP_ID]/Documents/myPDF.pdf
Anyone wants to try to make me understand what I'm doing wrong? I'm using Xcode 4.6. I browsed my iPhone app file system with a third-party software and the file "MyPDF.pdf" actually IS in the Documents" folder, and that's clear because the WebView is correctly populated.
Change CGRectZero to self.view.bounds when you display the document controller.
Solved. I had not implemented the UIDocumentenInteractionController delegate in the .h file. Now I have and everything works fine. Thank you to #trojanfoe for the useful hint.

Resources