Просмотр исходного кода

170209
Add pdf direct print
Change Signature.
Modify PDFUtils, add some function.

Ray Zhang 9 лет назад
Родитель
Сommit
5cdc3805e5

BIN
RedAnt ERP Mobile/RedAnt ERP Mobile.xcworkspace/xcuserdata/Ray.xcuserdatad/UserInterfaceState.xcuserstate


+ 1 - 0
RedAnt ERP Mobile/common/Functions/PDF+Web/PDFViewController.h

@@ -28,6 +28,7 @@
 @property (strong,nonatomic) NSString* save_note;
 @property (strong,nonatomic) NSString* save_note;
 
 
 @property (strong, nonatomic)  UIBarButtonItem *btnshare;
 @property (strong, nonatomic)  UIBarButtonItem *btnshare;
+@property (strong, nonatomic)  UIBarButtonItem *btnPrint;
 @property (strong, nonatomic)  UIBarButtonItem *btnemail;
 @property (strong, nonatomic)  UIBarButtonItem *btnemail;
 @property (strong, nonatomic)  UIBarButtonItem *btnsave;
 @property (strong, nonatomic)  UIBarButtonItem *btnsave;
 
 

+ 153 - 4
RedAnt ERP Mobile/common/Functions/PDF+Web/PDFViewController.m

@@ -10,6 +10,8 @@
 #import "RAUtils.h"
 #import "RAUtils.h"
 #import "const.h"
 #import "const.h"
 #import "AppDelegate.h"
 #import "AppDelegate.h"
+#import "PDFUtils.h"
+
 
 
 @interface PDFViewController ()
 @interface PDFViewController ()
 
 
@@ -55,9 +57,21 @@
         self.btnshare =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
         self.btnshare =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                                                                      target:self
                                                                      target:self
                                                                      action:@selector( onActionClick:)];
                                                                      action:@selector( onActionClick:)];
+
+        
         
         
-        self.navigationItem.rightBarButtonItem =self.btnshare;
+        self.btnPrint =[[UIBarButtonItem alloc] initWithTitle:@"Print"
+                                                                    style:UIBarButtonItemStylePlain
+                                                                    target:self
+                                                                    action:@selector( onPrintClick:)];
+        
+        NSMutableArray * items = [[NSMutableArray alloc]init];
+        [items addObject:self.btnshare];
+        [items addObject:self.btnPrint];
 
 
+        self.navigationItem.rightBarButtonItems =items;
+        
+        
     }
     }
     else
     else
     {
     {
@@ -296,6 +310,138 @@
     [super viewWillAppear:animated];
     [super viewWillAppear:animated];
   
   
     [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
     [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
+}
+- (IBAction)onPrintClick:(id)sender {
+    
+    
+  //  UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"preparing print..."];
+    
+    UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
+    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
+    printInfo.outputType = UIPrintInfoOutputGeneral;
+    
+    printInfo.orientation = UIPrintInfoOrientationPortrait;
+    printInfo.jobName = @"CoolVisitAirPrint";
+    airPrinterController.printInfo = printInfo;
+    airPrinterController.printingItem = self.content;
+    airPrinterController.delegate = self;
+    
+    
+    airPrinterController.showsNumberOfCopies=true;
+    airPrinterController.showsPageRange = true;
+    airPrinterController.showsPaperSelectionForLoadedPapers=true;
+    
+    
+    [airPrinterController presentFromBarButtonItem:self.btnPrint
+                                      animated:YES
+                             completionHandler:
+     ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
+     }
+     ];
+    
+    
+//    
+//    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
+//        if(completed && error)
+//            DebugLog(@"Printing failed due to error in domain %@ with error code %lu. Localized description: %@, and failure reason: %@", error.domain, (long)error.code, error.localizedDescription, error.localizedFailureReason);
+//    };
+//    
+//    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
+//    
+//    UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
+//    
+//    
+//    
+//    
+//    [[UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL]] contactPrinter:^(BOOL available)
+//     {
+//         [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
+//         
+//         if (available&& appDelegate.printerURL.length>0)
+//         {
+//             
+//             
+//             [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
+//             DebugLog(@"AIRPRINTER AVAILABLE");
+//         }
+//         else
+//         {
+//             
+//                          [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
+//             
+//             
+//             
+//                           //        [printPicker presentAnimated:YES completionHandler:
+//                           ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
+//                           {
+//                               if (userDidSelect)
+//                               {
+//                                   NSString* sid= printerController.printInfo.printerID;
+//                                   UIPrintPaper*paper=  printerController.printPaper;
+//             
+//                               }
+//                           }];
+//             
+//             
+//             DebugLog(@"AIRPRINTER NOT AVAILABLE");
+//             
+//             
+//             
+////             if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
+////             {
+////                 UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
+////                 //   [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
+////                 
+////                 
+////                 
+////                 [ printPicker presentFromBarButtonItem:self.btnPrint animated:YES completionHandler:
+////                  
+////                  
+////                  // [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
+////                  
+////                  
+////                  
+////                  //        [printPicker presentAnimated:YES completionHandler:
+////                  ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
+////                  {
+////                      if (userDidSelect)
+////                      {
+////                          //User selected the item in the UIPrinterPickerController and got the printer details.
+////                          
+////                          [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
+////                          
+////                          //Here you will get the printer and printer details.ie,
+////                          // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
+////                          
+////                          
+////                          
+////                          
+////                          appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
+////                          
+////                          [self startAirPrintWithData:self.content];
+////                          
+////                      }
+////                      else
+////                      {
+////                          
+////                          
+////                          //                          AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
+////                          //                          if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
+////                          //                          {
+////                          //
+////                          //                              [((MainViewController*)appDelegate.main_vc) Loginout:false];
+////                          //                          }
+////                          //                          [self prepareReturn:nil];
+////                          
+////                          //[self.navigationController popViewControllerAnimated:true];
+////                      }
+////                  }];
+////             }
+//         }
+//     }];
+//    
+    
+    
 }
 }
 - (IBAction)onActionClick:(id)sender {
 - (IBAction)onActionClick:(id)sender {
    
    
@@ -671,14 +817,17 @@
     //    (int) height = 42000
     //    (int) height = 42000
     //设置纸张大小
     //设置纸张大小
 
 
-
+    CGSize paperSize=[PDFUtils QueryPDFSizeFromData:self.content];
     for(UIPrintPaper* paper in paperList)
     for(UIPrintPaper* paper in paperList)
     {
     {
-        if (CGSizeEqualToSize(paper.paperSize, CGSizeMake(612, 792)))
+        if (CGSizeEqualToSize(paper.paperSize, paperSize))
             return paper;
             return paper;
 
 
     }
     }
-    CGSize paperSize = CGSizeMake(612, 792);
+    
+    UIPrintPaper * pp=[[UIPrintPaper alloc] init];
+    
+//    CGSize paperSize = CGSizeMake(612, 792);
     UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList];
     UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList];
     return p;
     return p;
 }
 }

+ 3 - 2
RedAnt ERP Mobile/common/Functions/signature/SignatureViewController.h

@@ -16,10 +16,11 @@
 @end
 @end
 @interface SignatureViewController : UIViewController
 @interface SignatureViewController : UIViewController
 //@property (strong, nonatomic) IBOutlet UIView *debugView;
 //@property (strong, nonatomic) IBOutlet UIView *debugView;
-@property (strong, nonatomic) IBOutlet SignatureView *signatureView;
+//@property (strong, nonatomic) IBOutlet SignatureView *signatureView;
+@property (strong, nonatomic)  SignatureView *signatureView;
 @property (strong,nonatomic) NSIndexPath *indexPath;
 @property (strong,nonatomic) NSIndexPath *indexPath;
 @property (strong,nonatomic) UIImage *existSignature;
 @property (strong,nonatomic) UIImage *existSignature;
 @property (nonatomic, weak) id<SignatureViewControllerDelegate> delegate;
 @property (nonatomic, weak) id<SignatureViewControllerDelegate> delegate;
 @property (nonatomic , copy) void (^onReturnImg)(UIImage* img);
 @property (nonatomic , copy) void (^onReturnImg)(UIImage* img);
-
+@property CGSize signature_ratio;
 @end
 @end

+ 72 - 1
RedAnt ERP Mobile/common/Functions/signature/SignatureViewController.m

@@ -32,6 +32,31 @@
     [super viewDidLoad];
     [super viewDidLoad];
     self.navigationItem.title = self.title;
     self.navigationItem.title = self.title;
     
     
+
+    
+    if(self.signature_ratio.width<=0||self.signature_ratio.height<=0)
+    {
+        self.signatureView= [[SignatureView alloc] initWithFrame:self.view.frame];
+    }
+    else
+    {
+        
+        CGSize signatureView_size=CGSizeMake(self.view.frame.size.width, self.view.frame.size.width*self.signature_ratio.height/self.signature_ratio.width);
+        
+        if(signatureView_size.height>self.view.frame.size.height)
+            signatureView_size=CGSizeMake(self.view.frame.size.height*self.signature_ratio.width/self.signature_ratio.height,self.view.frame.size.height);
+        
+        self.signatureView= [[SignatureView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-signatureView_size.width)/2, (self.view.frame.size.height-signatureView_size.height)/2, signatureView_size.width, signatureView_size.height)];
+        
+    }
+    
+    self.signatureView.backgroundColor = [UIColor yellowColor];
+    
+            self.signatureView.layer.borderColor = [UIColor darkGrayColor].CGColor;
+            self.signatureView.layer.borderWidth = 1.0;
+    
+    [self.view addSubview:self.signatureView];
+    
     
     
     UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
     UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
                                                                     style:UIBarButtonItemStylePlain
                                                                     style:UIBarButtonItemStylePlain
@@ -60,7 +85,11 @@
     //  self.navigationItem.rightBarButtonItem = searchButton;
     //  self.navigationItem.rightBarButtonItem = searchButton;
     
     
     
     
-    self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:doneBtn,clearBtn , nil];
+    UIBarButtonItem * settingBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
+                                                                 target:self
+                                                                 action:@selector( onSetting:)];
+    
+    self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:doneBtn,clearBtn,settingBtn , nil];
     
     
     
     
 
 
@@ -98,6 +127,48 @@
     
     
     return resultingImage;
     return resultingImage;
 }
 }
+- (void)onSetting:(UIButton *)sender {
+    
+    UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Change line width" message:nil preferredStyle:UIAlertControllerStyleAlert];
+    //block代码块取代了delegate
+    
+    
+    [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
+        textField.text = @"5";
+        
+        
+    }];
+    
+    UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
+            [self.signatureView setLineWidth:5.0];
+//        UITextField *name = alertControl.textFields.firstObject;
+//        if(name.text.length==0)
+//        {
+//            [RAUtils message_alert:@"Cannot save without name. Save canceled." title:@"Name is missing" controller:self];
+//        }
+//        else
+//        {
+//            self.save_name = name.text;
+//            self.btnsave.enabled =false;
+//            //  NSMutableDictionary * info =
+//            if(self.onSavePDF)
+//                self.onSavePDF(self.save_name,self.filename);
+//        }
+    }];
+    
+    UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
+       // DebugLog(@"Cancel");
+    }];
+    [alertControl addAction:actionOne];
+    
+    [alertControl addAction:alertthree];
+    
+    //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
+    
+    
+    [self presentViewController:alertControl animated:YES completion:nil];
+    
+}
 - (void)onDone:(UIButton *)sender {
 - (void)onDone:(UIButton *)sender {
     
     
     
     

+ 1 - 4
RedAnt ERP Mobile/common/Functions/signature/signature.storyboard

@@ -20,7 +20,7 @@
                         <rect key="frame" x="0.0" y="0.0" width="768" height="1024"/>
                         <rect key="frame" x="0.0" y="0.0" width="768" height="1024"/>
                         <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                         <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
                         <subviews>
                         <subviews>
-                            <imageView userInteractionEnabled="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="1eC-0c-wj1" customClass="SignatureView">
+                            <imageView hidden="YES" userInteractionEnabled="NO" contentMode="center" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="1eC-0c-wj1" customClass="SignatureView">
                                 <rect key="frame" x="0.0" y="0.0" width="768" height="1024"/>
                                 <rect key="frame" x="0.0" y="0.0" width="768" height="1024"/>
                                 <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES" flexibleMaxY="YES"/>
                                 <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES" flexibleMaxY="YES"/>
                             </imageView>
                             </imageView>
@@ -28,9 +28,6 @@
                         <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                         <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                     </view>
                     </view>
                     <navigationItem key="navigationItem" title="Title" id="8c7-vT-omW"/>
                     <navigationItem key="navigationItem" title="Title" id="8c7-vT-omW"/>
-                    <connections>
-                        <outlet property="signatureView" destination="1eC-0c-wj1" id="Wu9-3Q-fXt"/>
-                    </connections>
                 </viewController>
                 </viewController>
                 <placeholder placeholderIdentifier="IBFirstResponder" id="jvi-8Y-CLK" userLabel="First Responder" sceneMemberID="firstResponder"/>
                 <placeholder placeholderIdentifier="IBFirstResponder" id="jvi-8Y-CLK" userLabel="First Responder" sceneMemberID="firstResponder"/>
             </objects>
             </objects>

+ 3 - 0
RedAnt ERP Mobile/common/PDFUtils.h

@@ -18,4 +18,7 @@
 +(NSMutableDictionary*) loadControl:(NSString*)templateName;
 +(NSMutableDictionary*) loadControl:(NSString*)templateName;
 +(CGRect)WindowRect2PDFRect:(CGRect)rect pdf_rect:(CGRect)pdf_rect window_size:(CGSize)window_size;
 +(CGRect)WindowRect2PDFRect:(CGRect)rect pdf_rect:(CGRect)pdf_rect window_size:(CGSize)window_size;
 +(CGRect)PDFRect2WindowRect:(CGRect)rect pdf_rect:(CGRect)pdf_rect window_size:(CGSize)window_size;
 +(CGRect)PDFRect2WindowRect:(CGRect)rect pdf_rect:(CGRect)pdf_rect window_size:(CGSize)window_size;
++(CGPDFDocumentRef)OpenPDFFromData:(NSData *)pdfData;
++(CGRect) QuerySize:(CGPDFDocumentRef)document;
++(CGSize) QueryPDFSizeFromData:(NSData *)pdfData;
 @end
 @end

+ 27 - 0
RedAnt ERP Mobile/common/PDFUtils.m

@@ -74,6 +74,33 @@
     NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
     NSMutableDictionary *ret = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil] mutableCopy];
     return ret;
     return ret;
 }
 }
++(CGSize) QueryPDFSizeFromData:(NSData *)pdfData
+{
+    CGPDFDocumentRef document = [self OpenPDFFromData:pdfData];
+    CGRect rect= [self QuerySize:document];
+     CFRelease(document);
+    return rect.size;
+}
+
++(CGRect) QuerySize:(CGPDFDocumentRef)document
+{
+    CGPDFPageRef page= CGPDFDocumentGetPage (document , 1);
+    //        CGPDFPageRef page1= CGPDFDocumentGetPage (document , i);
+    CGRect papersize=CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
+    return papersize;
+
+}
++ (CGPDFDocumentRef)OpenPDFFromData:(NSData *)pdfData {
+         CFDataRef dataRef = (__bridge_retained CFDataRef)(pdfData);
+    
+         CGDataProviderRef proRef = CGDataProviderCreateWithCFData(dataRef);
+         CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithProvider(proRef);
+    
+         CGDataProviderRelease(proRef);
+         CFRelease(dataRef);
+    
+         return pdfRef;
+}
 +(CGPDFDocumentRef)OpenPDF:(NSString*) file
 +(CGPDFDocumentRef)OpenPDF:(NSString*) file
 {
 {
     //获取路径
     //获取路径