Cocoa PDF page splitting - objective-c

In the application I'm creating, I load a long page of HTML into a webView and then print it to a PDF using the following:
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
if ([frame isEqual:[[self doc] mainFrame]])
{
NSMutableData *newData = [[NSMutableData alloc] init];
NSPrintInfo *newInfo = [NSPrintInfo sharedPrintInfo];
NSView *docView = [[[[self doc] mainFrame] frameView] documentView];
NSPrintOperation *newPrintOp = [NSPrintOperation PDFOperationWithView:docView insideRect:docView.bounds toData:newData printInfo:newInfo];
BOOL runPrint = [newPrintOp runOperation];
if (!runPrint)
{
NSLog(#"Print Failed");
}
PDFDocument *newDoc = [[PDFDocument alloc] initWithData:newData];
[newData release];
[self setPdf:newDoc];
//Other code here
}
}
The problem is that when I look at newDoc, it is a huge PDF of a single page. What I would prefer would be the printing acting the same as it does from the "save as PDF..." dialog - that is, splitting the PDF into multiple reasonably-sized pages.
Does anyone know how to accomplish this?
I attempted inserting the following after NSPrintInfo *newInfo = [NSPrintInfo sharedPrintInfo];
[newInfo setVerticalPagination:NSAutoPagination];
[newInfo setHorizontalPagination:NSAutoPagination];
NSAutoPagination is described in the docs as the following:
NSAutoPagination
The image is divided into equal-sized rectangles and placed in one column of pages.
Available in Mac OS X v10.0 and later.
Declared in NSPrintInfo.h.
This had no effect on the printed PDF.

You get a file with one large page because + PDFOperationWithView: method doesn't support pagination at all. For that reason calling - setVerticalPagination: or - setHoriziontalPagination: doesn't change anything.
You could try use "classical" + printOperationWithView:printInfo: method, configure it to save PDF to temporary location and then create PDFDocument with contents of obtained file. I hope that fragment of code below will help.
NSMutableDictionary *dict = [[NSPrintInfo sharedPrintInfo] dictionary];
[dict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
[dict setObject:temporaryFilePath forKey:NSPrintSavePath];
NSPrintInfo *pi = [[NSPrintInfo alloc] initWithDictionary:dict];
[pi setHorizontalPagination:NSAutoPagination];
[pi setVerticalPagination:NSAutoPagination];
NSPrintOperation *op = [NSPrintOperation printOperationWithView:[[[webView mainFrame] frameView] documentView] printInfo:pi];
[pi release];
[op setShowsPrintPanel:NO];
[op setShowsProgressPanel:NO];
if ([op runOperation] ){
PDFDocument *doc = [[[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath: temporaryFilePath]] autorelease];
// do with doc what you want, remove file, etc.
}

Related

how to open powerpoint by pspdfkit?

NSURL *documentURL = [[[NSBundle mainBundle] resourceURL] URLByAppendingPathComponent:#"item_70_1_3.ppt"];
PSPDFDocument *document = [PSPDFDocument documentWithURL:documentURL];
NSURL *tempURL = PSPDFTempFileURLWithPathExtension(#"flattened_signaturetest", #"pdf");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[[PSPDFProcessor defaultProcessor] generatePDFFromDocument:document pageRange:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, document.pageCount)] outputFileURL:tempURL options:#{kPSPDFProcessorAnnotationTypes : #(PSPDFAnnotationTypeAll)} progressBlock:^(NSUInteger currentPage, NSUInteger numberOfProcessedPages, NSUInteger totalPages) {
// Access UI only from main thread.
dispatch_async(dispatch_get_main_queue(), ^{
[PSPDFProgressHUD showProgress:(numberOfProcessedPages+1)/(float)totalPages status:PSPDFLocalize(#"Preparing...")];
});
} error:NULL];
// completion
dispatch_async(dispatch_get_main_queue(), ^{
[PSPDFProgressHUD dismiss];
PSPDFDocument *flattenedDocument = [PSPDFDocument documentWithURL:tempURL];
PSPDFViewController *pdfController = [[PSPDFViewController alloc] initWithDocument:flattenedDocument];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:pdfController];
[self presentViewController:navController animated:YES completion:NULL];
});
});
i open a empty content using above code : (
The PSPDFProcessor feature is experimental. Please try if the PPT file works if you open it with Safari, it uses mostly Apple's libraries and they can fail for certain files. The code here looks like it's simply copied from my examples in PSPDFCatalog and is thus fine.

name of the picked image xcode

I want to get the name of the image picked from the library or newly clicked in -
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
I am using this code:
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
AppDelegate *del = [[UIApplication sharedApplication]delegate];
[self dismissModalViewControllerAnimated:YES];
image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(#"imagekbgfg=====>%#",image);
[self.imageView setImage:image];
imgName = [info objectForKey:UIImagePickerControllerOriginalImage];
del.mainImgName = imgName;
del.imageData = UIImageJPEGRepresentation(image, 1.0);
del.imageApp = image;
}
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
}
How can I do this?
Using the code bellow you will get path like:assets-library://asset/asset.JPG?id=79450962-C0FD-484C-808B-83E045F40972&ext=JPG when your picking image from photo album. This path is valid. It contains the asset ID and asset type.
// Firstly get the picked image's file URL.
NSURL *imageFileURL = [info objectForKey:UIImagePickerControllerReferenceURL];
// Then get the file name.
NSString *imageName = [imageFileURL lastPathComponent];
NSLog(#"image name is %#", imageName);
If you want to get image user picked again using the path you get using UIImagePickerController.(Here maybe you want to save the path so that you can read that image back without asking user to pick that image again) You will need the ALAssetsLibrary otherwise you don't need it.
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block UIImage *returnValue = nil;
[library assetForURL:imageFileURL resultBlock:^(ALAsset *asset) {
returnValue = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]];
} failureBlock:^(NSError *error) {
NSLog(#"error : %#", error);
}];
Take a look at the UIImagePickerControllerDelegate Protocol. The string UIImagePickerControllerReferenceURL contains an URL of your file. You could use it to construct the filename from it.
Other than that there seems to be no built-in way to get the file name from an UIImage or UIImageView object.
Reference:
Stackoverflow answer
You can get the file name using below code:
NSURL *assetURL = [imageDic objectForKey:UIImagePickerControllerReferenceURL];
__block NSString *originalFileName = nil;
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetURL resultBlock:^(ALAsset *asset){
originalFileName = [[asset defaultRepresentation] filename];
}
originalFileName give the original file name of resource.

Array with UIImageView elements - setImageWithURL

I have such code
arrayWithImages = [[NSMutableArray alloc] init];
NSEnumerator *enumForNames = [arrayWithNames objectEnumerator];
NSEnumerator *enumForURLs = [arrayWithURLs objectEnumerator];
id objName, objURL;
while(objName = [enumForNames nextObject]) {
objURL = [enumForURLs nextObject];
UIImageView *anImage = nil;
[anImage setImageWithURL:[NSURL URLWithString:objURL]];
(...)
[arrayWithImages addObject:anImage];
}
And each time I got SIGABRT in line with "[arrayWithImages addObject:anImage];"
What's here wrong?
I don’t see an setImageWithURL method on UIImageView. Where is this from?
Is there any output from the SIGABRT crash?
Try this code:
// Download the image
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:objURL]];
// Make an UIImage out of it
UIImage *anImage = [UIImage imageWithData:imageData];
// Create an image view with the image
UIImageView *imageView = [[UIImageView alloc] initWithImage:anImage];
// Check to make sure it exists
if (imageView != nil) {
// Add it to your array
[arrayWithImages addObject:imageView];
else {
NSLog(#"Image view is nil");
}
Note that you should download the images asynchronously to avoid hanging the loop. This blog post discussing asynchronous image downloading.
Also if you know that [enumForURLs nextObject]; will return an NSString (or even better, NSURL) you should assign objURL to type NSString (or NSURL).

Print Local HTML using Cocoa

I have a simple list of items that needs to be printed using Cocoa. I have a half-baked solution that uses an NSView with a custom drawRect: method, but it's fairly complex and not very easy to maintain.
What I would like to have is an HTML string (which could be easily constructed from the list) that can be embedded in a one-off WebView, then printed.
Assuming I have a simple NSString like:
NSString *htmlString = #"<b>Test</b>";
What's the easiest method for creating a WebView displaying this content? I've tried the below code, but it results in a single blank page:
WebView *webView = [[WebView alloc] init];
NSString *dir = #"/Users/Me/Desktop/";
NSString *fileUrl = [dir stringByAppendingPathComponent:#"Temp_Print.html"];
NSString *htmlString = #"<b>Hi!</b>";
[[htmlString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileUrl atomically:YES];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fileUrl]]];
[webView setFrame:NSMakeRect(0, 0, 500, 500)];
NSPrintOperation *po = [NSPrintOperation printOperationWithView:webView printInfo:pi];
[pi release];
[po runOperation];
Another one of those questions you solve right after asking it!
The run loop needs to iterate in order for the content to actually load. I simply finished running the actual print operation in the frame load delegate method:
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
...
}
Source

Instagram open UTI directly

I recently stumbled upon the following interesting feature: Instagram iPhone Hooks
I was wondering if one is able to open an app through the documentinteractioncontroller immediately, WITHOUT having to show a preview (– presentPreviewAnimated:) or an action sheet (– presentOpenInMenuFromRect:inView:animated:). Seems to me that it's the only way, but I might be missing something.
-(void) saveToInstagram {
NSURL *url;
UIDocumentInteractionController *dic;
CGRect rect = CGRectMake(0 ,0 , 0, 0);
UIImage *i = imageView.image;
CGRect cropRect = CGRectMake(i.size.width - 306 ,i.size.height - 306 , 612, 612);
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Test.ig"];
CGImageRef imageRef = CGImageCreateWithImageInRect([imageView.image CGImage], cropRect);
UIImage *img = [[UIImage alloc] initWithCGImage:imageRef];
CGImageRelease(imageRef);
[UIImageJPEGRepresentation(img, 1.0) writeToFile:jpgPath atomically:YES];
url = [[NSURL alloc] initFileURLWithPath:jpgPath];
dic = [self setupControllerWithURL:url usingDelegate:self];
[dic presentOpenInMenuFromRect:rect inView:self.view animated:YES];
[dic retain];
[img release];
[url release];
}
- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL: fileURL];
interactionController.delegate = interactionDelegate;
return interactionController;
}
- (void)documentInteractionControllerWillPresentOpenInMenu:(UIDocumentInteractionController *)controller {
}
First save your JPEG with a .ig or .igo extension instead of .jpg or .jpeg then create a NSURL with a file:// directive. Also I used the .igo extension and the UTI com.instagram.exclusivegram for exclusivity to Instagram but you can use the .ig extension and the UTI com.instagram.gram
For Example:
// URL TO THE IMAGE FOR THE DOCUMENT INTERACTION
NSURL *igImageHookFile = [[NSURL alloc] initWithString:[[NSString alloc] initWithFormat:#"file://%#", jpgPath]];
docInteractionController.UTI = #"com.instagram.exclusivegram";
[self setupDocumentControllerWithURL:igImageHookFile];
// OPEN THE HOOK
[docInteractionController presentOpenInMenuFromRect:self.frame inView:self animated:YES];
The only way to do it would be if Instagram registers a custom URL handler (e.g. igram://) and can accept data either as part of that URL (e.g. base64 encoded) or via the pasteboard.
Basically the limitations that UIDocumentInteractionController (and this technique) workaround are:
Apps are not allowed to query for or directly launch other apps.
Apps cannot generally open files from the sandboxed user directory of other apps.