Preventing PDF file to be modified - ios

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 );

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.

iOS - Display PDF with signature

I'm trying to display signed pdf in an iOS applications.
While displaying a PDF is easy (plenty of libraries, UIDocumentInteractionController, etc.), I can't seem to find how to display the signatures in the PDF.
By signature, I mean what is in this sample: http://blogs.adobe.com/security/SampleSignedPDFDocument.pdf
I've seen it done in an iOS application, but I've no idea how to do it myself. I tried poking around with CGContextDrawPDFPage, without much success.
Any hint?
The signature you saw in the PDF file is the appearance of the signature field widget. The field widget is a specific PDF annotation.
The problem with the default PDF rendering engine in iOS (used by UIDocumentInteractionController, QuickLook framework, etc) is that it does not display the annotations on the PDF page, it displays only the main page content.
The only solution is to use a 3rd party PDF rendering engine, such as MuPDF, Foxit, PDFTron, etc.
Are you converting HTML to PDF?
If yes then it may be helpful :)
UIGraphicsBeginImageContext(imgapplicationSignature.bounds.size);
[imgapplicationSignature.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image1 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self saveImage:image1 :#"imgapplicationSignature.png"];
NSString *documentsDirectory1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)[0];
[wbView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"var imageElement = document.getElementById('img_01'); imageElement.setAttribute('src', '%#');", [NSString stringWithFormat:#"file://%#/imgapplicationSignature.png", documentsDirectory1]]];

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)];

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?

How to Print Word Documents Using uiprintinteractioncontroller class

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];

Resources