How to use AirPrint to print a locked pdf file - ios

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?

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.

UIGraphicsBeginPDFContextToData creates PDFs unreadable by Acrobat

Im creating PDF's from iOS using a ordinary code block.
NSMutableData *pdfdata = [NSMutableData dataWithLength:2048];
CGRect newf = CGRectMake(0, 0,1024,1024);
UIGraphicsBeginPDFContextToData(pdfdata,newf,nil);
UIGraphicsBeginPDFPage();
//rendering code
UIGraphicsEndPDFContext();
iOS and OS X read these PDF's just fine but Acrobat claims they are damaged and unreadable. The interesting thing is that doing a duplicate in Preview unmangles them and makes them readable by Acrobat again.
By diff-ing the two versions I can see that the OS X PDF encoder has added a bunch of stuff (its binary so no idea what)
Anyone know the secret sauce to making my PDF's Acrobat compatible again.
EDIT
Broken file - will not open in Acrobat
The document starts with a block of 2048 bytes filled with zero and this damages all offsets in the file.
Create an empty NSMutableData without initial length and the problem should be fixed.

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

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

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