Im having real trouble with this one. I dont know what is wrong, but it spits this out:
ERROR: attempting to display print options with no printing source(item/items/formatter/renderer) set
The code works if i set it to print a png image, but not the contents of a NSString.
Here is my code:
- (IBAction)pushPrint:(id)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setDateFormat:#"dd/MM/YY HH:mm:ss"];
NSString* currentTime = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *path = [[NSBundle mainBundle] pathForResource:t[NSString stringWithFormat:#"Results Printout\n"
"\n"
"%#\n"
"\n"
"\n"
"%#", currentTime, pasteboard.string] ofType:nil];
NSData *myData = [NSData dataWithContentsOfFile:path];
UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];
print.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
print.printInfo = printInfo;
print.showsPageRange = YES;
print.printingItem = myData;
void (^completionHandler)(UIPrintInteractionController *,BOOL, NSError *) = ^(UIPrintInteractionController *print,BOOL completed, NSError *error) {
if (!completed && error) {
NSLog(#"houston we have a problem");
}
};
[print presentAnimated:YES completionHandler:completionHandler];
}
You seem to be wanting to print a plain NSString, yet you are trying to load a file with a file path that cannot be valid. Thus, myData is very likely nil (please confirm with the debugger) and thus the error message is correct (print.printingItem must not be nil).
To print a simple text, create an instance of UISimpleTextPrintFormatter (with initWithText:) and assign that instance to print.printFormatter.
Related
I'm trying to pull a .pdf from my apps documents directory and load it in the PIC. The PIC displays without errors but shows no data in the preview.
What am I missing here? Am I just taking a completely wrong approach?
-(IBAction)actPrintPDF:(id)sender
{
NSString* fileName = #"Observation.PDF";
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];
NSLog(#"Path = %#",path);
NSLog(#"pdfFileName = %#",pdfFileName);
NSData *myData = [NSData dataWithContentsOfFile:path];
UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];
print.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [pdfFileName lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
print.printInfo = printInfo;
print.showsPageRange = YES;
print.printingItem = myData;
UIViewPrintFormatter *viewFormatter = [self.view viewPrintFormatter];
viewFormatter.startPage = 0;
print.printFormatter = viewFormatter;
UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {};
[print presentAnimated:YES completionHandler:completionHandler];
}
The log is showing this:
***2015-11-08 08:56:30.462 ShortShooter4[16153:7505181] Path = /Users/myUserName/Library/Developer/CoreSimulator/Devices/02F8C004-6BA3-4F7B-9FC4-743DA205D7DD/data/Containers/Data/Application/796A70BC-4159-4B18-825E-B35043BC5704/Documents
2015-11-08 08:56:30.462 ShortShooter4[16153:7505181] pdfFileName = /Users/muUserName/Library/Developer/CoreSimulator/Devices/02F8C004-6BA3-4F7B-9FC4-743DA205D7DD/data/Containers/Data/Application/796A70BC-4159-4B18-825E-B35043BC5704/Documents/Observation.PDF
2015-11-08 08:56:31.080 ShortShooter4[16153:7505181] Unbalanced calls to begin/end appearance transitions for .***
So I can see the document is there. I also have the unbalanced calls issue but I'll attack that separately.
problem solved by removing:
UIViewPrintFormatter *viewFormatter = [self.view viewPrintFormatter];
viewFormatter.startPage = 0;
print.printFormatter = viewFormatter;
Hope This helps someone else.
First off, I'm a complete beginner.
This might be a stupid question, but here it goes:
I'm currently working on an App than contains Latin texts that the users can view and read.
I'm using Xcode 4 with the storybord function.
Theway the app is built: user selects author - then the book - then app shows the text.
I am kind of confused because i need to have various text files, depending on the users choice.
As i have done or do usually just embedded the files as resource and by using the below code I read-out the files
in .h file
-(NSString *)readFile:(NSString *)fileName;
in .m file
-(NSString *)readFile:(NSString *)fileName
{
NSLog(#"readFile");
NSString *appFile = fileName;
NSFileManager *fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:appFile])
{
NSError *error= NULL;
NSString *resultData = [NSString stringWithContentsOfFile: appFile encoding: NSUTF8StringEncoding error: &error];
if (error == NULL)
return resultData;
}
return NULL;
}
May this will help you
Enjoy Coding :)
EDIT
What I have done is according to the date change on one button click and also at the time of viewWillApper I call the function below
-(void)dateAndContentReload
{
NSLog(#"dateAndContentReload");
NSLog(#"Appdelegate Date: %#",appDelegate.dateSelected);
self.currentDate = appDelegate.dateSelected;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM yyyy"];
NSString *dateString = [dateFormatter stringFromDate: appDelegate.dateSelected];
self.navigationItem.title = dateString;
[dateFormatter setDateFormat: #"Myyyy"];
NSString *dateComp = [dateFormatter stringFromDate: appDelegate.dateSelected];
//NSLog(#"date : %#",dateString);
NSString *data;
NSString *path;
if(![dateComp isEqualToString: #"12012"])
{
NSString *size = [NSString stringWithFormat: #"%f",appDelegate.size] ;
NSLog(#"dateAndContentReload size: %f",appDelegate.size);
data = #"<html><body><font size='";
data = [data stringByAppendingString: size];
data = [data stringByAppendingString: #"'><P>"];
data = #"Data not Available for Current Date.";
data = [data stringByReplacingOccurrencesOfString: #"\n" withString:#"<br>"];
data = [data stringByAppendingString:#"</P></font></body></html>"];
}
else
{
[dateFormatter setDateFormat:#"MMMMd"];
dateString = [dateFormatter stringFromDate: appDelegate.dateSelected];
[dateFormatter release];
NSLog(#"new Format: %#",dateString);
path = [[NSBundle mainBundle] pathForResource: dateString ofType: #"txt"];
data = nil;
NSString *size = [NSString stringWithFormat: #"%f",appDelegate.size] ;
NSLog(#"dateAndContentReload size: %f",appDelegate.size);
data = #"<html><body><font size='";
data = [data stringByAppendingString: size];
data = [data stringByAppendingString: #"'><P>"];
data = [data stringByAppendingString: [self readFile: path]];
data = [data stringByReplacingOccurrencesOfString: #"\n" withString:#"<br>"];
data = [data stringByAppendingString:#"</P></font></body></html>"];
}
[self.currentView loadHTMLString: data baseURL: nil];
}
Basically I read file and display in UIWebView as client request it.
I attached a file to the mail I am using this code.
[mail addAttachmentData:[myView PDFData] mimeType:#"application/pdf" fileName:#"name.pdf"];
How can I do the same thing for printing a file, I need to print this [myView PDFData].
I found only this for printing:
NSString *PDFFileWithName = [[NSBundle mainBundle] pathForResource:#"name" ofType:#"pdf"];
NSData *dataFromPath = [NSData dataWithContentsOfFile:PDFFileWithName];
Thanks
You should read through the Drawing and Printing Guide for iOS. The printingItem property of UIPrintInteractionController can be set to the NSData of a PDF.
Update for added code
The value of dataFromPath should be equal to [myView PDFData] although I would recommend changing the variable name once you get it working.
NSData *dataFromPath = [myView PDFData];
Full code to print pdf
UIPrintInteractionController *pc = [UIPrintInteractionController
sharedPrintController];
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.orientation = UIPrintInfoOrientationPortrait;
printInfo.jobName =#"Report";
pc.printInfo = printInfo;
pc.showsPageRange = YES;
pc.printingItem = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://test.com/Print_for_Client_Name.pdf"]];
// You can use here image or any data type to print.
UIPrintInteractionCompletionHandler completionHandler =
^(UIPrintInteractionController *printController, BOOL completed,
NSError *error) {
if(!completed && error){
NSLog(#"Print failed - domain: %# error code %ld", error.domain,
(long)error.code);
}
};
[pc presentFromRect:CGRectMake(0, 0, 300, 300) inView:self.view animated:YES completionHandler:completionHandler];
write below code and check it
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathFolder = [NSString stringWithFormat:#"%#",pdfFileName];
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:pathFolder];
NSURL *targetURL = [NSURL fileURLWithPath:path];
UIPrintInteractionController *pc = [UIPrintInteractionController sharedPrintController];
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.orientation = UIPrintInfoOrientationPortrait;
printInfo.jobName =#“Print”;
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pc.printInfo = printInfo;
pc.showsPageRange = YES;
pc.printingItem = targetURL;
UIPrintInteractionCompletionHandler completionHandler =
^(UIPrintInteractionController *printController, BOOL completed,
NSError *error) {
if(!completed && error){
NSLog(#"Print failed - domain: %# error code %ld", error.domain, (long)error.code);
}
};
[pc presentFromRect:shareButton.frame inView:self.view animated:YES completionHandler:completionHandler];
Posted the wrong link earlier - this one should help!
Blog - Printing in iOS - Goes into great detail and includes a tutorial on Printing PDFs
my application though paper feed action is called but nothing is printed and paper comes out blank.. I am using the following code for print.
-(IBAction)printButtonAction:(id)sender{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:#"Preview.pdf"];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:writableDBPath]];
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [writableDBPath lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
controller.printInfo = printInfo;
controller.showsPageRange = YES;
controller.printingItem = data;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
if (!completed && error)
NSLog(#"FAILED! due to error in domain %# with error code %u",
error.domain, error.code);
};
UIViewPrintFormatter *viewFormatter = [documentView viewPrintFormatter];
viewFormatter.startPage = 0;
controller.printFormatter = viewFormatter;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[controller presentFromBarButtonItem:printButton animated:YES
completionHandler:completionHandler];
} else {
[controller presentAnimated:YES completionHandler:completionHandler];
}
}
Thanks
Deepika jain
I think the below code will work,
-(IBAction)printButtonAction:(id)sender{
float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion>4.1) {
NSData *myPdfData = [NSData dataWithContentsOfFile:pdfPath]; //check the value inside |myPdfData| and |pdfPath| is the path of your pdf.
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
if (controller && [UIPrintInteractionController canPrintData:myPdfData]){
controller.delegate = delegate; //if necessary else nil
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [pdfPath lastPathComponent];
//printInfo.duplex = UIPrintInfoDuplexLongEdge;
controller.printInfo = printInfo;
controller.showsPageRange = YES;
controller.printingItem = myPdfData;
// We need a completion handler block for printing.
UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
if(completed && error){
NSLog(#"FAILED! due to error in domain %# with error code %u", error.domain, error.code);
}
};
[controller presentFromRect:rect inView:senderView animated:YES completionHandler:completionHandler];
}else {
NSLog(#"Couldn't get shared UIPrintInteractionController!");
}
}
Check the path of the pdf you fetched, I think you missed the '/' after the Documents directory. You try putting a break point and check the path, then check the |data| variable for the content from the path you specified. Try it and respond with ur comments. :)
I have a UIImagePickerController that saves the image to disk as a png. When I try to load the PNG and set a UIImageView's imageView.image to the file, it is not displaying.
Here is my code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImagePNGRepresentation(image);
// Create a file name for the image
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSString *imageName = [NSString stringWithFormat:#"photo-%#.png",
[dateFormatter stringFromDate:[NSDate date]]];
[dateFormatter release];
// Find the path to the documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Now we get the full path to the file
NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
// Write out the data.
[imageData writeToFile:fullPathToFile atomically:NO];
// Set the managedObject's imageLocation attribute and save the managed object context
[self.managedObject setValue:fullPathToFile forKey:#"imageLocation"];
NSError *error = nil;
[[self.managedObject managedObjectContext] save:&error];
[self dismissModalViewControllerAnimated:YES];
}
Then here is how I try to load it:
self.imageView.backgroundColor = [UIColor lightGrayColor];
self.imageView.frame = CGRectMake(10, 10, 72, 72);
if ([self.managedObject valueForKey:#"imageLocation"] != nil) {
NSLog(#"Trying to load the imageView with: %#", [self.managedObject valueForKey:#"imageLocation"]);
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[self.managedObject valueForKey:#"imageLocation"]];
self.imageView.image = image;
} else {
self.imageView.image = [UIImage imageNamed:#"no_picture_taken.png"];
}
I get the message that it's trying to load the imageView in the debugger, but the image is never displayed in the imageView. Can anyone tell me what I've got wrong here?
Thanks a bunch.
You're writing out an NSData object, not an image. You need to read the NSData object back in and convert to UIImage.
Assuming everything else is correct, try this:
NSData *data = [[NSData alloc] initWithContentsOfFile:[self.managedObject valueForKey:#"imageLocation"]];
UIImage *image = [[UIImage alloc] initWithData:data];
Using NSDateFormatterShortStyle will cause the date to be represented as (for example) 12/11/12, which will mess up your file name. It will also use a colon in the short time representation, which is illegal.
I would recommend using a more custom date format, such as:
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:#"yyyyMMdd-HHmmss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
I used this code on one of my test projects and simulated the bug. Jordan was right about converting it to NSData first before changing it again to UIImage but the real problem here is on your file naming method.
I noticed that you have used the current date and time as the file name of your image. This includes characters such as ":" and "/" which is not allowed to be used on file names. http://www.portfoliofaq.com/pfaq/FAQ00352.htm
As a solution, I made the date format as what is written below:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMddHHmm"];
NSString *imageName = [NSString stringWithFormat:#"photo-%#.png",
[dateFormatter stringFromDate:[NSDate date]]];
Hope this helps others as well. :)