I need to view a form on a webpage, through a UIWebView on an iPad app. After the user submits the form, I need to send a print job from the form on the webpage through to an AirPrint enabled printer on the same wifi network as the iPad. I have access to do anything I need to on the website. I have read that the UIWebView cannot recognise the window.print() JavaScript command.
What can I do to get the iPad app to receive a print job and print via AirPrint?
I have seen that other people on this website have asked similar questions, but none of them have been answered with much detail.
There is a sample project by Apple on printing UIwebView content.
Print Web View Content
- (IBAction)printWebPage:(id)sender
{
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
if(!controller){
NSLog(#"Couldn't get shared UIPrintInteractionController!");
return;
}
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);
}
};
// Obtain a printInfo so that we can set our printing defaults.
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
// This application produces General content that contains color.
printInfo.outputType = UIPrintInfoOutputGeneral;
// We'll use the URL as the job name.
printInfo.jobName = [self.urlField text];
// Set duplex so that it is available if the printer supports it. We are
// performing portrait printing so we want to duplex along the long edge.
printInfo.duplex = UIPrintInfoDuplexLongEdge;
// Use this printInfo for this print job.
controller.printInfo = printInfo;
// Be sure the page range controls are present for documents of > 1 page.
controller.showsPageRange = YES;
// This code uses a custom UIPrintPageRenderer so that it can draw a header and footer.
APLPrintPageRenderer *myRenderer = [[APLPrintPageRenderer alloc] init];
// The APLPrintPageRenderer class provides a jobtitle that it will label each page with.
myRenderer.jobTitle = printInfo.jobName;
// To draw the content of each page, a UIViewPrintFormatter is used.
UIViewPrintFormatter *viewFormatter = [self.myWebView viewPrintFormatter];
#if SIMPLE_LAYOUT
/*
For the simple layout we simply set the header and footer height to the height of the
text box containing the text content, plus some padding.
To do a layout that takes into account the paper size, we need to do that
at a point where we know that size. The numberOfPages method of the UIPrintPageRenderer
gets the paper size and can perform any calculations related to deciding header and
footer size based on the paper size. We'll do that when we aren't doing the simple
layout.
*/
UIFont *font = [UIFont fontWithName:#"Helvetica" size:HEADER_FOOTER_TEXT_HEIGHT];
CGSize titleSize = [myRenderer.jobTitle sizeWithFont:font];
myRenderer.headerHeight = myRenderer.footerHeight = titleSize.height + HEADER_FOOTER_MARGIN_PADDING;
#endif
[myRenderer addPrintFormatter:viewFormatter startingAtPageAtIndex:0];
// Set our custom renderer as the printPageRenderer for the print job.
controller.printPageRenderer = myRenderer;
/*
The method we use to present the printing UI depends on the type of UI idiom that is currently executing. Once we invoke one of these methods to present the printing UI, our application's direct involvement in printing is complete. Our custom printPageRenderer will have its methods invoked at the appropriate time by UIKit.
*/
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[controller presentFromBarButtonItem:self.printButton animated:YES completionHandler:completionHandler]; // iPad
}
else {
[controller presentAnimated:YES completionHandler:completionHandler]; // iPhone
}
}
Related
I have an old iOS app that uses UIKit and Objective C which I am currecntly porting to SwiftUI and Swift. Everything was going great and I am loving Swift and SwiftUI. The app is pretty much done but the app relies on the user being able to print and/or save the main view as a PDF. I just can't figure out how to access a view in swiftui in order to convert it to a PDF. Here is my existing/working objective-c code.
- (IBAction)actionPrint:(id)sender {
// CREATE CLEAR BACKGROUND
[legMain setBackgroundColor:[UIColor clearColor]];
// SCROLL TO BASE POSITION
[legMain scrollRectToVisible:CGRectMake(1, 1, 1, 1) animated:NO];
// RESET ZOOM
SnapPanel *myObject = [self fGetObject];
myObject.zoom = [NSNumber numberWithDouble:1.0];
[self fSave];
// RECORD FRAME SIZE AND SET TO CONTENT SIZE
double dWidth = legMain.frame.size.width;
double dHeight = legMain.frame.size.height;
[legMain setFrame:CGRectMake(legMain.frame.origin.x, legMain.frame.origin.y, legMain.contentSize.width, legMain.contentSize.height)];
// GET VIEW AS NSDATA FOR PDF
NSMutableData *pdfData = [NSMutableData data];
CGRect pageSize = CGRectMake(0.0, 0.0, 8.5 * 72.0, 11.0 * 72.0);
UIGraphicsBeginPDFContextToData(pdfData, pageSize, nil);
CGContextRef pdfContext = UIGraphicsGetCurrentContext();
// CREATE A SINGLE PAGE PDF
UIGraphicsBeginPDFPage();
[legMain.layer renderInContext:pdfContext];
UIGraphicsEndPDFContext();
// CREATE PRINT CONTROLLER
UIPrintInteractionController *pc = [UIPrintInteractionController sharedPrintController];
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
if (!completed && error){
NSLog(#"Print error: %#", error);
}
};
// SETUP PRINT CONTROLLER
[pc setShowsNumberOfCopies:YES];
[pc setShowsPageRange:YES];
[pc setShowsPaperSelectionForLoadedPapers:YES];
pc.printingItem = pdfData;
// DISPLAY CONTROLLER DIALOG
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[pc presentFromRect:texName.frame inView:viewMain.superview
animated:YES completionHandler:completionHandler];
} else {
[pc presentAnimated:YES completionHandler:completionHandler];
}
// RESET BACKGROUND COLOUR AND FRAME SIZE
[legMain setBackgroundColor:[UIColor colorWithRed:.95 green:.95 blue:.95 alpha:1.0]];
[legMain setFrame:CGRectMake(legMain.frame.origin.x, legMain.frame.origin.y, dWidth, dHeight)];
}
legMain is the view that I am adjusting and eventually converting to a PDF. I have managed to port most of this code untill I reach line 26 where I need to render the view. I don't even know where to begin to get an instance of my view.
Having access to hosting window as described in How to access own window within SwiftUI view? you can get main UIView from any of your SwiftUI view as
let mainView = hostingWindow?.rootViewController.view
but for printing I would probably use dedicated view as below (to avoid affecting UI view)
if let hostingController = hostingWindow?.rootController as UIHostingController {
let printingView = UIHostingController(rootView: hostingController.rootView).view
// ... do anything with printingView here, because it will have
// a copy of SwiftUI rootView
}
Update: to the details in comment (hosting window is not needed in this case)
VStack {
PrintableView(arg: value) // << need to be made undependable, so all
// parameters should be passed via arguments
}
then on Print action it could be like
Button("Print") {
let printingView = UIHostingController(rootView: PrintableView(arg: value)).view
// do anything printing related with `printingView` here
}
i've got a ZBarReaderView created from storyboard with 216x20 px which is shown as roughly 230x50 px because ZBarReaderView doesn't take it's size too serious...
It all works very well, however it behaves really strange when I call start on that readerView. It starts the cam but then in maybe half a second the readerView zooms a bit and the camera picture inside the readerView moves down and then up again.
It's not terrible but it look kinda bad. Anyone got any ideas what might be causing this and how to solve it? Maybe the sdk has some sort of hidden callback for the readiness of the scanner, i could hide it until the scanner says it's ready and then show the scanner like .5 seconds later...
barcodeReader is the iboutlet to the ZBarReaderView and scannerLoading is an iboutlet to a uiactivityindicatorview which is animating until the scanner is loaded.
These are the only settings which are changed from default, except the frame which is set in the storyboard of course.
[barcodeReader setReaderDelegate:self];
[barcodeReader setAllowsPinchZoom:false];
[barcodeReader start];
/* this works because [barcodeReader start] blocks ui updates until the scanner
is running, i know it's not a good solution but since there doesn't seem to
be a callback or delegate method like scannerDidStart or something it seems
to be the only way... */
[scannerLoading stopAnimating];
Thanks for your help!
I just posted an answer to a retaliated question:
ZBarReadview with custom size from StoryBoard,but when it's called,it's size is not I set
Maybe the answer also solves your problem.
In short:
When using Interface Builder or Storyboard to create a view and assign the ZBarReaderView to it, you have to check "Clip Subviews" in the properties for the camera image to keep the size of the view.
Just add another view to make it as cameraoverlayview with an image view having the image which is having required part of it as transparent.then in the button action `
// ADD: present a barcode reader that scans from the camera feed
ZBarReaderViewController *reader = [ZBarReaderViewController new];
reader.readerDelegate = self;
reader.supportedOrientationsMask = ZBarOrientationMaskAll;
reader.sourceType=UIImagePickerControllerSourceTypeCamera;
//reader.cameraDevice = UIImagePickerControllerCameraDeviceFront;
reader.cameraOverlayView=cameraOverlay;
if( [UIImagePickerController isCameraDeviceAvailable: UIImagePickerControllerCameraDeviceFront ])
{
reader.cameraDevice = UIImagePickerControllerCameraDeviceFront;
}
ZBarImageScanner *scanner = reader.scanner;
reader.wantsFullScreenLayout = YES;
// TODO: (optional) additional reader configuration here
// EXAMPLE: disable rarely used I2/5 to improve performance
[scanner setSymbology: ZBAR_I25
config: ZBAR_CFG_ENABLE
to: 0];
reader.showsZBarControls = NO;
// present and release the controller
[self presentModalViewController:reader animated:YES];
//[appdel.navigationController pushViewController:reader animated:YES];
//[reader.view addSubview:collect];
[reader release];add this and then also add
`- (void) imagePickerController: (UIImagePickerController*) reader
didFinishPickingMediaWithInfo: (NSDictionary*) info
{
// ADD: get the decode results
id results =
[info objectForKey: ZBarReaderControllerResults];
ZBarSymbol *symbol = nil;
for(symbol in results)
// EXAMPLE: just grab the first barcode
break;
[self rewards:symbol.data];
}
`
as a method .hope this will solve your issue
We're creating a photo app that lets the user take a number of photos in series. For some reason the camera seems to die with no obvious trigger. This typically appears to happen if the camera is running and the device is idle (screensaver/locking) or if the main iPhone button is pressed and the app is minimized. So we need to find a way to check if the camera is still running or not. Can this be polled somehow? Have someone experienced a similar issue?
If you can get a camera view on self.view you can say that camera is active or present.. here is how you can check if camera view is available or not -
UIView *cameraView = [self findCamControlsLayerView:self.view];
if (cameraView)
// camera is present
else
// camera is not present
// Find the view that contains the camera controls (buttons)
- (UIView*)findCamControlsLayerView:(UIView*)view {
Class cl = [view class];
NSString *desc = [cl description];
if ([desc compare:#"PLCropOverlay"] == NSOrderedSame)
return view;
for (NSUInteger i = 0; i < [view.subviews count]; i++)
{
UIView *subView = [view.subviews objectAtIndex:i];
subView = [self findCamControlsLayerView:subView];
if (subView)
return subView;
}
return nil;
}
I found an example code for showing PDF file on a UIScrollView with horizontal scrolling. It works fine, but problem is it shows only 2 pages of PDF. I tried in my best to figure out the issue, but I couldn't figure it. Can you please give me help?
I looked at that sample project you pointed to in this question and like you, I can see it only displays two pages of whatever PDF file it's given to display.
The problem with the sample code comes in the PDFViewController.m file. For these lines:
PDFScrollView *page = [self dequeueRecycledPage];
if (page == nil) {
page = [[[PDFScrollView alloc] initWithPage:index + 1 frame:[self frameForPageAtIndex:index]] autorelease];
}
I added
else {
[page setPage: index inFrame:[self frameForPageAtIndex: index]];
}
And also these new lines into PDFScrollView.h
- (void) setPage: (NSInteger) onPage inFrame:(CGRect)frame;
And PDFScrollView.m
- (void) setPage: (NSInteger) onPage inFrame:(CGRect) frame
{
if(pdfView)
{
[pdfView removeFromSuperview];
[pdfView release];
}
self.frame = frame;
self.index = onPage;
pdfView = [[PDFViewTiled alloc] initWithPage:onPage frame:self.frame];
[self addSubview:pdfView];
}
This isn't a perfect fix. You'll see the drawing isn't proper, especially when backing up pages. I'll leave that to you as an exercise to take care of, but hopefully this is a nice start.
And I hope it helps you out.
I am trying to modify Apple's PhotoScroller example and I encountered a problem that I couldn't solve.
Basically, the PhotoScroller originally loaded a bunch of image in an array locally. I try to modify this and change to it request an image file dynamically from an URL. Once the user scroll to the next page, it will fetch the next image from a new URL.
In order to improve the performance, I wanted to preload the next page so user doesn't need to wait for the image being downloaded while scrolling to the next page. Once the next page is on current page, the page after that will be loaded and so on...
I'm not quite sure how I can achieve this and I hope someone can show me what to do.
Here is my custom code: (Please refer the full code from Apple's PhotoScroller example)
tilePage method: (It will be call at the beginning and every time when user did scroll the scrollView)
- (void)tilePages
{
// Calculate which pages are visible
CGRect visibleBounds = pagingScrollView.bounds;
int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
int lastNeededPageIndex = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
lastNeededPageIndex = MIN(lastNeededPageIndex, [self imageCount] - 1);
// Recycle no-longer-visible pages
for (ImageScrollView *page in visiblePages) {
if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
[recycledPages addObject:page];
[page removeFromSuperview];
}
}
[visiblePages minusSet:recycledPages];
// add missing pages
for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
if (![self isDisplayingPageForIndex:index]) {
ImageScrollView *page = [self dequeueRecycledPage];
//ImageScrollView *nextpage = [self dequeueRecycledPage];
if (page == nil) {
page = [[[ImageScrollView alloc] init] autorelease];
}
[self configurePage:page forIndex:index];
[pagingScrollView addSubview:page];
[visiblePages addObject:page];
}
}
}
To configure page index and content:
- (void)configurePage:(ImageScrollView *)page forIndex:(NSUInteger)index
{
//set page index
page.index = index;
//set page frame
page.frame = [self frameForPageAtIndex:index];
//Actual method to call image to display
[page displayImage:[self imageAtIndex:index]];
NSLog(#"index: %i", index);
}
To fetch image from URL:
- (UIImage *)imageAtIndex:(NSUInteger)index {
NSString *string1 = [NSString stringWithString:#"http://abc.com/00"];
NSString *string2 = [NSString stringWithFormat:#"%d",index+1];
NSString *string3 = [NSString stringWithString:#".jpg"];
NSString *finalString = [NSString stringWithFormat:#"%#%#%#",string1,string2,string3];
NSLog(#"final string is: %#", finalString);
NSURL *imgURL = [NSURL URLWithString: finalString];
NSData *imgData = [NSData dataWithContentsOfURL:imgURL];
return [UIImage imageWithData:imgData];
}
Thanks for helping!
Lawrence
You can use concurrent operations to fetch images one after another. Using NSOperations, you can set a dependency chain so images are loaded in a serial fashion in the background, or you can have them all downloaded concurrently.
The problem with large images is that even though you save them in the file system, there is a "startup" time to get the images rendered. Also, in PhotoScroller, Apple "cheats" by having all the images pre tiled for each level of detail. Apple provides no way to render just a Plain ole JPEG in PhotoScroller, so unless you can pre tile it will be of no use to you.
If you are curious, you can see how to both download multiple images and pre tile them into a temporary file by perusing the PhotoScrollerNetwork project on github.