How to Print Word Documents Using uiprintinteractioncontroller class - ios

Need some help.
I am able to print pdf & images directly
but need to print document pr ppt files.
Please help me out
Thanks in advance.

You don't need to convert any format to PDF before printing. Simply set the printing data & present the menu.
[UIPrintInteractionController sharedPrintController].printingItem = [NSData dataWithContentsOfURL:self.url]; //url of your word doc
[[UIPrintInteractionController sharedPrintController] presentAnimated:YES completionHandler:nil];

Related

iOS create pdf from UIWebview content

In my app for iOS i need to create a pdf document from my webview content. I watched these posts: Creating PDF file from UIWebView and https://coderchrismills.wordpress.com/2011/06/25/making-a-pdf-from-a-uiwebview/
I wonder if there is a simpler way to do it. For example for my project for Mac i use this:
NSData *pdf = [[[[webView mainFrame] frameView] documentView] dataWithPDFInsideRect:[[[webView mainFrame] frameView] documentView].frame];
PDFDocument *doc = [[PDFDocument alloc] initWithData:pdf];
Is there any simple way to do this in iOS?
Which is the best option to obtain best quality pdf document from a webview content?
There isn't a method that allows this directly via the SDK like there is on Mac however you may wish to take a look at BNHtmlPdfKit which allows you to save the contents of URLs, web views and also html strings as PDFs.
For example, as follows:
self.htmlPdfKit = [BNHtmlPdfKit saveUrlAsPdf:[NSURL URLWithString:#"http://itsbrent.net"] toFile:#"...itsbrent.pdf" pageSize:BNPageSizeA6 success:^(NSString *pdfFileName) {
NSLog(#"Done");
} failure:^(NSError *err) {
NSLog(#"Failure");
}];
It makes use of a custom UIPrintPageRenderer which overrides paperRect and printableRect thus causing the UIPrintFormatter to return a pageCount as well as render the document.
I found good answer by AnderCover at
"Creating PDF file from UIWebView"
also it's not using any third party api. To create pdf from webview.
Hope it help's you.

Preventing PDF file to be modified

My app generates a PDF from HTML using UIWebview and UIViewPrintFormatter then getting pdf data from UIGraphicPDFContext.
All works well, but I want to prevent pdf file to be modified.
Do you have an idea how to achieve this?
Thanks a lot.
Thierry
When using UIGraphicsBeginPDFContextToData you can supply a documentInfo dictionary which contains keys such as kCGPDFContextAllowsCopying and kCGPDFContextAllowsPrinting (CFBoolean values).
The solution is to set an owner password and pdf will be signed with.
NSDictionary *pdfInfo = [NSDictionary dictionaryWithObjectsAndKeys:#"password", kCGPDFContextOwnerPassword, [NSNumber numberWithInt:128], kCGPDFContextEncryptionKeyLength, nil];
UIGraphicsBeginPDFContextToData( pdfData, kPdfPageRect, pdfInfo );

Issue on attaching csv & pdf file- Objective c [duplicate]

I'm working with PDF generation,it generated the PDF, viewed using QLPreviewController, all worked fine except mail forwarding, I have two attachments of type .pdf and .csv. I have the following issues while emailing.
sometimes no attachments while emailing
pdf size will be very huge when pdf contains image(10 mb for one page pdf, if it contains image)
Problem is when testing in device,in simulator it all works fine...,
I come to know some exporting or importing UTI associated issues here
since I am a starter I feel difficult to understand it, I think some UTI's I have to add somewhere.. Can anyone please help me to identify the issue and solve, please. Thanks in advance.
1. Attachment issue
set mime type as text/csv instead of application/csv or image/csv. code is as shown below
[mailComposer addAttachmentData:[NSData dataWithContentsOfFile:self.csvFilePath]
mimeType:#"text/csv" fileName:[NSString stringWithFormat:#"csvPage.csv" ]];
1. Size issue
I struggled a lot.. at last when wrote image in jpeg format to pdf page using below code, size got reduced ten times!!
UIImage *lowResImage = [UIImage imageWithData:UIImageJPEGRepresentation(plotImage, 0.02)];

Getting Image URLs in a Web Directory

I want to get URLs of all images or lets say "JPEG" files in a web directory (www.abcde.com/images). I just want their URLs in an array.. I couldnt manage that. Could u pls help me with this?
Thanks in advance..
Assuming you have access to an index file you could simply load via NSURL the whole html file and cut out the link lines. This however will not work (or hardly work) when you want to search ("spider or crawl") for links in more complex documents. On iOS i would suggest you use the simple, yet quite powerfull "hpple" framework (https://github.com/topfunky/hpple). It is used to parse html. You can search with it for certain html elements, such as <a href...> constructs.
a sample with hpple could looks like this:
NSURL *url = [NSURL URLWithString:#"whatver.com/images"];
NSData *data = [NSData url];
TFHpple *hppleParser = [TFHpple data];
NSString *images = #"//img"; // grabbs all image tags
NSArray *node = [hppleParser searchWithXPathQuery:images]
find a bigger example at http://www.raywenderlich.com/14172/how-to-parse-html-on-ios
Create a server side script(eg php) which gives you a list of all images in that directory as xml or json. From iOS send a request to that script get the xml or JSON parse it and use the image urls.

How to use AirPrint to print a locked pdf file

I know use CGPDFDocumentUnlockWithPassword to unlock a pdf, but it returned a CGPDFDocumentRef, and if I want use AirPrint to print it, it should be a NSData or a url, But I don't know how to convert a CGPDFDocumentRef to a NSData object or a save it as a file . Did anyone have a idea to solve this case?
Simply printing each page of your source pdf into a newly created CGPDFContext and saving the resulting pdf to separate file should do it. Principially, it should look like this:
// create PDFContext
NSURL* dstPath = [NSURL fileURLWithPath:pathInCachesFolder]
CGRect pageRect = CGRectMake(0,0,1024,1024); // example, use real page size of src document here
CGContextRef pdfContext = CGPDFContextCreateWithURL(dstPath, &pageRect, nil);
// use for loop here to repeat following stuff for each pdf page from the src pdf
CGPDFContextBeginPage(pdfContext, NULL);
CGContextDrawPDFPage(pdfContext, sourcePDFpageRef);
CGPDFContextEndPage(pdfContext);
// close pdfContext, saves dst file
CGPDFContextClose(pdfContext);
CGContextRelease (pdfContext);
This piece of code just prints a page from the source pdf to the newly created pdf and saves the pdf to the dst path. Of course, you'll have repeat the drawing part for each page from the source document. Afterwards you should be able to print the pdf via AirPrint without any problems.
I already solved this question. you can have information here:
How to pass a password to UIPrintInteractionController when using AirPrint to print a locked pdf?

Resources