iphone - 如何从我的 iPhone 应用程序中获取打印输出?

标签 iphone objective-c ipad printing uiscrollview

我的应用程序中有一个打印按钮。如果我单击按钮,它应该打印屏幕。 我的问题是屏幕是否有 ScrollView 并且内容超过屏幕大小。它应该打印屏幕的全部内容。打印输出可能需要 2 或 3 页。是否有任何不通过拍照或屏幕截图打印屏幕的示例代码或项目。

最佳答案

您需要将整个数据转换为 HTML 格式并使用 WebView 来显示数据。要从 iPhone 应用程序打印输出,您需要从您的 webview 内容创建一个 Pdf 文件,然后从该 pdf 文件中打印出来。我在这里为您提供示例代码,它在我的应用程序中正常工作。

1) 包含 QuartzCore 框架并在 ViewController.h 文件中导入“QuartzCore/QuartzCore.h”。

2) 在 ViewController.h 文件中使用以下代码

@interface ViewController : UIViewController<UIScrollViewDelegate, UIPrintInteractionControllerDelegate>
 {
       UIWebView *theWebView;
       int imageName;
       double webViewHeight;
 }

 -(void) takeSnapshot;
 - (void) drawPdf;
 -(void) printContent:(NSData *) data;

3) 在 vi​​ewDidLoad 方法中使用以下代码:

 - (void)viewDidLoad
 {
    [super viewDidLoad];

     NSString *htmlString=@"<html><body><img src=\"blue.png\" alt=\"Blue Image\" />  <h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p><h1>Hi, himanshu mahajan</h1><p> I am from Vinove Software service</p></body></html>";

     UIBarButtonItem *generate=[[UIBarButtonItem alloc] initWithTitle:@"Print" style:UIBarButtonItemStylePlain target:self action:@selector(printBtnPressed)];
     [self.navigationItem setRightBarButtonItem:generate];

      theWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
      [theWebView loadHTMLString:htmlString baseURL:nil];
      theWebView.scrollView.bounces=NO;
      [self.view addSubview:theWebView];

      imageName=0;
      webViewHeight=0.0;
 }

4) 方法定义

-(void) printBtnPressed
{
   [self takeSnapshot];

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDir=[paths objectAtIndex:0];
    NSString *filePath=[documentDir stringByAppendingPathComponent:@"Demo.pdf"];

    NSData *data=[[NSData alloc] initWithContentsOfFile:filePath];

    [self printContent:data];
}

-(void) takeSnapshot
{

   webViewHeight=[[theWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] integerValue];

   CGRect screenRect=theWebView.frame;
   double currentWebViewHeight = webViewHeight;

   while (currentWebViewHeight > 0)
   {
     imageName ++;

     UIGraphicsBeginImageContext(screenRect.size);
     CGContextRef ctx = UIGraphicsGetCurrentContext();
     [[UIColor blackColor] set];
     CGContextFillRect(ctx, screenRect);

     [theWebView.layer renderInContext:ctx];

     UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();

     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png",imageName]];

     if(currentWebViewHeight < 416)
     {
         CGRect lastImageRect = CGRectMake(0, 416 - currentWebViewHeight, theWebView.frame.size.width, currentWebViewHeight);
          CGImageRef imageRef = CGImageCreateWithImageInRect([newImage CGImage], lastImageRect);    

          newImage = [UIImage imageWithCGImage:imageRef];
          CGImageRelease(imageRef);
      }
      [UIImagePNGRepresentation(newImage) writeToFile:pngPath atomically:YES];

      [theWebView stringByEvaluatingJavaScriptFromString:@"window.scrollBy(0,416);"];
      currentWebViewHeight -= 416;
    }

   [self drawPdf];
  }


  - (void) drawPdf
  {
    CGSize pageSize = CGSizeMake(612, webViewHeight);
    NSString *fileName = @"Demo.pdf";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

    UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);

    // Mark the beginning of a new page.
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);

    double currentHeight = 0.0;
   for (int index = 1; index  <= imageName ; index++)
   {
      NSString *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.png", index]];
      UIImage *pngImage = [UIImage imageWithContentsOfFile:pngPath];

      [pngImage drawInRect:CGRectMake(0, currentHeight, pageSize.width, pngImage.size.height)];
      currentHeight += pngImage.size.height;
     }
     UIGraphicsEndPDFContext();
  }

   -(void) printContent:(NSData *) data
   {
      UIPrintInteractionController *print = [UIPrintInteractionController sharedPrintController];

       print.delegate = self;
       UIPrintInfo *printInfo = [UIPrintInfo printInfo];
       printInfo.outputType = UIPrintInfoOutputGeneral;
      //printInfo.jobName = @"Information";
       printInfo.duplex = UIPrintInfoDuplexLongEdge;
       print.printInfo = printInfo;
       print.showsPageRange = YES;
       print.printingItem = data;
       UIViewPrintFormatter *viewFormatter = [self.view viewPrintFormatter];
       viewFormatter.startPage = 0;
       print.printFormatter = viewFormatter;

       UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error) {};

       [print presentAnimated:YES completionHandler:completionHandler];
 }

关于iphone - 如何从我的 iPhone 应用程序中获取打印输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7076061/

相关文章:

objective-c - 将 JSON 响应字典复制到 plist

iphone - 当 iPhone 应用程序到达一天中的特定时间时,我如何收到通知?

iphone - 带有 View 的 TabBar 应用程序,该 View 上有一个 UIButton 以转到 TabBar 之外的另一个 View

jquery - 如何通过数字位置选择CSS中的某个元素

iphone - 阻塞主线程直到 UIWebView 完成加载

objective-c - NSNumberFormatter 不允许十进制输入

objective-c - 如何检查 UITextField 中的文本是否与特定模式匹配?

ios - iPhone/iPad 的唯一标识符除了 UUID/UDID?

iphone - 识别 UILabel 的调整字体大小

iphone - IOS,如何将自定义 SelectionIndicator 添加到 UIPickerView?