RAPDFViewController.m 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. //
  2. // RAPDFViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-8-7.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RAPDFViewController.h"
  9. #import "RAUtils.h"
  10. #import "const.h"
  11. #import "AppDelegate.h"
  12. #import "PDFUtils.h"
  13. #import "TextUtils.h"
  14. #import "RANetwork.h"
  15. @interface RAPDFViewController ()
  16. @end
  17. @implementation RAPDFViewController
  18. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  19. {
  20. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  21. if (self) {
  22. // Custom initialization
  23. }
  24. return self;
  25. }
  26. - (void)setFilename:(NSString *)filename {
  27. _filename = [TextUtils legalFilename:filename];
  28. }
  29. - (void)viewDidLoad
  30. {
  31. NSLog(@"pdf viewer loaded");
  32. [super viewDidLoad];
  33. // Do any additional setup after loading the view.
  34. UIRefreshControl *ref = [[UIRefreshControl alloc]init];
  35. ref.tag = 200 ;
  36. ref.attributedTitle = [[NSAttributedString alloc]initWithString:@"drag to refresh"];
  37. ref.tintColor = [UIColor colorWithRed:0.10 green:0.68 blue:0.94 alpha:0.7];
  38. // ref.hidden = true;
  39. [ref addTarget:self action:@selector(changeCell) forControlEvents:UIControlEventValueChanged];
  40. [self.wkWebview.scrollView addSubview:ref];
  41. // self.webView.dataDetectorTypes=UIDataDetectorTypeAll;
  42. self.wkWebview.navigationDelegate = self;
  43. if (@available(iOS 11, *)) {
  44. self.wkWebview.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  45. }
  46. // else {
  47. //
  48. //
  49. //
  50. // self.automaticallyAdjustsScrollViewInsets = NO;
  51. // }
  52. // self.btnsave.tintColor = UIColorFromRGB(0x996633);
  53. if(self.defaultStyle)
  54. {
  55. self.btnshare =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
  56. target:self
  57. action:@selector( onActionClick:)];
  58. self.btnPrint =[[UIBarButtonItem alloc] initWithTitle:@"Print"
  59. style:UIBarButtonItemStylePlain
  60. target:self
  61. action:@selector( onPrintClick:)];
  62. NSMutableArray * items = [[NSMutableArray alloc]init];
  63. [items addObject:self.btnshare];
  64. [items addObject:self.btnPrint];
  65. self.navigationItem.rightBarButtonItems =items;
  66. if (self.navigationController.viewControllers[0]==self )
  67. {
  68. UIBarButtonItem *closeButton =nil;
  69. closeButton=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"close"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  70. style:UIBarButtonItemStylePlain
  71. target:self
  72. action:@selector( onCloseClick:)];
  73. //[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
  74. // closeButton.title = @"Close";
  75. self.navigationItem.leftBarButtonItem = closeButton;
  76. }
  77. }
  78. else
  79. {
  80. UIBarButtonItem *closeButton =nil;
  81. closeButton=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  82. style:UIBarButtonItemStylePlain
  83. target:self
  84. action:@selector( onBackClick:)];
  85. //[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCloseClick:)];
  86. // closeButton.title = @"Close";
  87. self.navigationItem.leftBarButtonItem = closeButton;
  88. self.btnshare = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"share"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  89. style:UIBarButtonItemStylePlain
  90. target:self
  91. action:@selector(onActionClick:)];//[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onOpenOrderClick:)];
  92. // self.btnshare.tintColor = UIColorFromRGB(0x996633);
  93. self.btnemail =[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"mail"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  94. style:UIBarButtonItemStylePlain
  95. target:self
  96. action:@selector(onEmailClick:)];// [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Commit", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onCommitOrderClick:)];
  97. // self.btnemail.tintColor = UIColorFromRGB(0x996633);
  98. // self.btnSend = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Email", nil) style:UIBarButtonItemStylePlain target:self action:@selector(onSendMailClick:)];
  99. self.btnsave = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  100. style:UIBarButtonItemStylePlain
  101. target:self
  102. action:@selector(onSaveClick:)];
  103. UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  104. fixedItem.width = 20.0f;
  105. NSMutableArray * items = [[NSMutableArray alloc]init];
  106. [items addObject:self.btnshare];
  107. [items addObject:fixedItem];
  108. [items addObject:self.btnemail];
  109. if(self.canSave)
  110. {
  111. [items addObject:fixedItem];
  112. [items addObject:self.btnsave];
  113. }
  114. self.navigationItem.rightBarButtonItems=items;
  115. self.btnshare.enabled =false;
  116. self.btnemail.enabled =false;
  117. self.btnsave.enabled =false;
  118. }
  119. // self.url=@"http://192.168.1.3:8080/npd/test.pdf";
  120. // NSURL* url = [NSURL URLWithString:self.url];
  121. //
  122. // NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:2000];
  123. //
  124. //
  125. // [self.webView loadRequest:request];
  126. [self loadcontent];
  127. }
  128. - (void)onBackClick:(UIButton *)sender {
  129. [self.navigationController popViewControllerAnimated:FALSE];
  130. }
  131. - (void)onCloseClick:(UIButton *)sender {
  132. [self dismissViewControllerAnimated:false completion:nil];
  133. //[self.navigationController popViewControllerAnimated:FALSE];
  134. }
  135. -(void)ReloadData
  136. {
  137. // Count ++ ;
  138. // [mytabelview reloadData];
  139. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  140. [reF endRefreshing];
  141. [self loadcontent];
  142. }
  143. - (void)copyTmpLocalFile:(NSString *)localPath {
  144. NSString *path = NSTemporaryDirectory();
  145. NSString *filePath = [path stringByAppendingPathComponent:self.filename];
  146. NSFileManager *fileManager = [NSFileManager defaultManager];
  147. if ([fileManager fileExistsAtPath:filePath]) { // 文件存在就不会拷贝
  148. [fileManager removeItemAtPath:filePath error:nil];
  149. }
  150. [fileManager copyItemAtPath:localPath toPath:filePath error:nil];
  151. self.url = filePath;
  152. }
  153. -(void) loadcontent
  154. {
  155. UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"Loading..."];
  156. self.mum.hidden=false;
  157. if(self.isLocalfile)
  158. {
  159. /*
  160. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  161. NSString *cachefolder = [paths objectAtIndex:0];
  162. NSString *pdfFolder = [cachefolder stringByAppendingPathComponent:@"pdf_cache"];
  163. self.url = [pdfFolder stringByAppendingPathComponent:self.url];
  164. */
  165. self.content= [NSData dataWithContentsOfFile:self.url];
  166. [waitalert dismissViewControllerAnimated:YES completion:nil];
  167. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  168. if (self.filename.length <= 0) {
  169. self.filename=[self.url lastPathComponent];
  170. } else {
  171. [self copyTmpLocalFile:self.url];
  172. }
  173. self.btnshare.enabled =true;
  174. self.btnemail.enabled =true;
  175. self.btnsave.enabled =true;
  176. // if(self.onLoadSuccess)
  177. // self.onLoadSuccess(self.url);
  178. //
  179. // if(self.autoPrint)
  180. // [self startAirPrintWithData:self.content];
  181. // NSLog(@"put file to webview");
  182. // [self.webView loadData:self.content MIMEType:@"application/pdf" textEncodingName:nil baseURL:nil];
  183. NSURL *url = [NSURL fileURLWithPath:self.url];
  184. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  185. [self.wkWebview loadRequest:request];
  186. }
  187. else
  188. {
  189. [RANetwork request_fastdownload:nil url:self.url completionHandler:^(NSMutableDictionary *result) {
  190. self.content= result[@"data"];
  191. if(self.filename.length<=0)
  192. self.filename = result[@"suggestedFilename"];
  193. [waitalert dismissViewControllerAnimated:YES completion:nil];
  194. [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
  195. self.navigationItem.title = self.save_name;
  196. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  197. // self.content=nil;
  198. if(self.content!=nil&&self.content.length>0)
  199. {
  200. [reF removeFromSuperview];
  201. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  202. NSString *path = NSTemporaryDirectory();
  203. NSString *filePath = [path stringByAppendingPathComponent:self.filename];
  204. // NSError *error = nil;
  205. // BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
  206. // NSAssert(bo,@"创建目录失败");
  207. if (self.cachePath.length) {
  208. filePath = self.cachePath;
  209. }
  210. [self.content writeToFile:filePath atomically:YES];
  211. self.url = filePath;
  212. self.isLocalfile = true;
  213. self.btnshare.enabled =true;
  214. self.btnemail.enabled =true;
  215. self.btnsave.enabled =true;
  216. NSURL *url = [NSURL fileURLWithPath:filePath];
  217. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  218. [self.wkWebview loadRequest:request];
  219. }
  220. else
  221. {
  222. [RAUtils message_alert:nil title:@"Open PDF Failed" controller:self];
  223. if(self.autoPrint)
  224. [self.navigationController popViewControllerAnimated:true];
  225. }
  226. }];
  227. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  228. //
  229. // NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60*5];
  230. //
  231. // //设置请求方式为get
  232. //
  233. // [request setHTTPMethod:@"GET"];
  234. //
  235. // //添加用户会话id
  236. //
  237. // [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
  238. //
  239. // //连接发送请求
  240. //
  241. //
  242. // NSHTTPURLResponse* urlResponse = nil;
  243. //
  244. // NSError *error = nil;
  245. //
  246. // self.content= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
  247. //
  248. // if(self.filename.length<=0)
  249. // self.filename = [urlResponse suggestedFilename];
  250. //
  251. //
  252. // dispatch_async(dispatch_get_main_queue(), ^{
  253. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  254. //
  255. // [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
  256. // self.navigationItem.title = self.save_name;
  257. // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  258. // // self.content=nil;
  259. // if(self.content!=nil&&self.content.length>0)
  260. // {
  261. // [reF removeFromSuperview];
  262. //
  263. //
  264. // // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  265. // NSString *path = NSTemporaryDirectory();
  266. // NSString *filePath = [path stringByAppendingPathComponent:self.filename];
  267. // // NSError *error = nil;
  268. // // BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
  269. // // NSAssert(bo,@"创建目录失败");
  270. // if (self.cachePath.length) {
  271. // filePath = self.cachePath;
  272. // }
  273. //
  274. // [self.content writeToFile:filePath atomically:YES];
  275. //
  276. // self.url = filePath;
  277. // self.isLocalfile = true;
  278. //
  279. //
  280. // self.btnshare.enabled =true;
  281. //
  282. //
  283. // self.btnemail.enabled =true;
  284. // self.btnsave.enabled =true;
  285. //
  286. //
  287. //
  288. // NSURL *url = [NSURL fileURLWithPath:filePath];
  289. // NSURLRequest *request = [NSURLRequest requestWithURL:url];
  290. // [self.wkWebview loadRequest:request];
  291. //
  292. //
  293. //
  294. //
  295. //
  296. //
  297. // // int a=0;
  298. // }
  299. // else
  300. // {
  301. // [RAUtils message_alert:nil title:@"Open PDF Failed" controller:self];
  302. //
  303. // if(self.autoPrint)
  304. // [self.navigationController popViewControllerAnimated:true];
  305. // // self.content = [self.url dataUsingEncoding:NSUTF8StringEncoding];
  306. // }
  307. //
  308. // ;
  309. // // DebugLog(@"%@",[[NSString alloc] initWithData:self.content encoding:NSASCIIStringEncoding]);
  310. // //
  311. //
  312. // // DebugLog(urlResponse.MIMEType);
  313. //
  314. // // [self.webView loadData:self.content MIMEType:urlResponse.MIMEType textEncodingName:nil baseURL:nil];
  315. //
  316. //
  317. //
  318. //
  319. // });
  320. // });
  321. }
  322. }
  323. -(void)changeCell
  324. {
  325. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  326. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
  327. if ([self respondsToSelector:@selector(ReloadData)])
  328. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  329. // DebugLog(@"refresh!!!!!!!!");
  330. }
  331. - (void)didReceiveMemoryWarning
  332. {
  333. [super didReceiveMemoryWarning];
  334. // Dispose of any resources that can be recreated.
  335. }
  336. -(void) viewWillAppear:(BOOL)animated
  337. {
  338. [super viewWillAppear:animated];
  339. [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
  340. }
  341. - (IBAction)onPrintClick:(id)sender {
  342. // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
  343. UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  344. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  345. printInfo.outputType = UIPrintInfoOutputGeneral;
  346. printInfo.orientation = UIPrintInfoOrientationPortrait;
  347. printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  348. if(self.filename.length>0)
  349. printInfo.jobName = self.filename;
  350. airPrinterController.printInfo = printInfo;
  351. airPrinterController.printingItem = self.content;
  352. airPrinterController.delegate = self;
  353. airPrinterController.showsNumberOfCopies=true;
  354. // airPrinterController.showsPageRange = true;
  355. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  356. [airPrinterController presentFromBarButtonItem:self.btnPrint
  357. animated:YES
  358. completionHandler:
  359. ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
  360. }
  361. ];
  362. //
  363. // void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  364. // if(completed && error)
  365. // 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);
  366. // };
  367. //
  368. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  369. //
  370. // UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  371. //
  372. //
  373. //
  374. //
  375. // [[UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL]] contactPrinter:^(BOOL available)
  376. // {
  377. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  378. //
  379. // if (available&& appDelegate.printerURL.length>0)
  380. // {
  381. //
  382. //
  383. // [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  384. // DebugLog(@"AIRPRINTER AVAILABLE");
  385. // }
  386. // else
  387. // {
  388. //
  389. // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  390. //
  391. //
  392. //
  393. // // [printPicker presentAnimated:YES completionHandler:
  394. // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  395. // {
  396. // if (userDidSelect)
  397. // {
  398. // NSString* sid= printerController.printInfo.printerID;
  399. // UIPrintPaper*paper= printerController.printPaper;
  400. //
  401. // }
  402. // }];
  403. //
  404. //
  405. // DebugLog(@"AIRPRINTER NOT AVAILABLE");
  406. //
  407. //
  408. //
  409. //// if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  410. //// {
  411. //// UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  412. //// // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  413. ////
  414. ////
  415. ////
  416. //// [ printPicker presentFromBarButtonItem:self.btnPrint animated:YES completionHandler:
  417. ////
  418. ////
  419. //// // [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  420. ////
  421. ////
  422. ////
  423. //// // [printPicker presentAnimated:YES completionHandler:
  424. //// ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  425. //// {
  426. //// if (userDidSelect)
  427. //// {
  428. //// //User selected the item in the UIPrinterPickerController and got the printer details.
  429. ////
  430. //// [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  431. ////
  432. //// //Here you will get the printer and printer details.ie,
  433. //// // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  434. ////
  435. ////
  436. ////
  437. ////
  438. //// appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  439. ////
  440. //// [self startAirPrintWithData:self.content];
  441. ////
  442. //// }
  443. //// else
  444. //// {
  445. ////
  446. ////
  447. //// // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  448. //// // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  449. //// // {
  450. //// //
  451. //// // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  452. //// // }
  453. //// // [self prepareReturn:nil];
  454. ////
  455. //// //[self.navigationController popViewControllerAnimated:true];
  456. //// }
  457. //// }];
  458. //// }
  459. // }
  460. // }];
  461. //
  462. }
  463. - (IBAction)onActionClick:(id)sender {
  464. //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  465. // NSString *filePath = nil;
  466. // if(!self.isLocalfile)
  467. // {
  468. // NSString *path = NSTemporaryDirectory();
  469. // NSString* filename =self.filename;
  470. //
  471. // filePath= [path stringByAppendingPathComponent:filename];
  472. //
  473. // }
  474. // else
  475. // filePath = self.url;
  476. [self openFile:self.url];
  477. }
  478. - (IBAction)onSaveClick:(id)sender {
  479. //UIAlertControllerStyle两种类型UIAlertControllerStyleAlert类似UIAlertView
  480. //UIAlertControllerStyleActionSheet类似UIActionSheet
  481. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Save pdf" message:nil preferredStyle:UIAlertControllerStyleAlert];
  482. //block代码块取代了delegate
  483. [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  484. textField.text = self.save_name;
  485. }];
  486. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  487. UITextField *name = alertControl.textFields.firstObject;
  488. if(name.text.length==0)
  489. {
  490. [RAUtils message_alert:@"Cannot save without name. Save canceled." title:@"Name is missing" controller:self];
  491. }
  492. else
  493. {
  494. self.save_name = name.text;
  495. self.btnsave.enabled =false;
  496. // NSMutableDictionary * info =
  497. if(self.onSavePDF)
  498. self.onSavePDF(self.save_name,self.filename);
  499. }
  500. }];
  501. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  502. DebugLog(@"Cancel");
  503. }];
  504. [alertControl addAction:actionOne];
  505. [alertControl addAction:alertthree];
  506. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  507. [self presentViewController:alertControl animated:YES completion:nil];
  508. }
  509. - (IBAction)onEmailClick:(id)sender {
  510. //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  511. // NSString *path = NSTemporaryDirectory();
  512. NSString* filename =self.filename;
  513. // NSString *filePath = [path stringByAppendingPathComponent:filename];
  514. NSString *filePath = self.url;
  515. MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
  516. if(mc==nil)
  517. {
  518. //[RAUtils message_alert:@"You must create an email account first." title:@"Send Email" controller:self];
  519. return;
  520. }
  521. mc.mailComposeDelegate = self;
  522. [mc setSubject:self.mail_subject];
  523. [mc setToRecipients:self.mail_to];
  524. if(self.mail_content==nil)
  525. self.mail_content=@"";
  526. NSString* htmlbody = [NSString stringWithFormat:@"<html><p>%@</p></html>",self.mail_content];
  527. [mc setMessageBody:htmlbody
  528. isHTML:YES];
  529. NSData *data = [NSData dataWithContentsOfFile:filePath];
  530. // if(self.attachment_name.length>0)
  531. // filename = self.attachment_name;
  532. [mc addAttachmentData:data mimeType:@"application/pdf" fileName:filename];
  533. // [self presentModalViewController:mc animated:YES];
  534. [self presentViewController:mc animated:YES completion:nil];
  535. return;
  536. }
  537. -(void)openFile: (NSString *) file_url {
  538. NSURL *file_URL = [NSURL fileURLWithPath:file_url];
  539. if (file_URL != nil) {
  540. if (self.fileInteractionController == nil) {
  541. //_fileInteractionController = [[UIDocumentInteractionController alloc] init];
  542. _fileInteractionController = [UIDocumentInteractionController interactionControllerWithURL:file_URL];
  543. _fileInteractionController.delegate = self;
  544. // [_fileInteractionController retain];//不适用ARC,记得RETAIN 使用ARC注释这一行
  545. }
  546. else
  547. {
  548. self.fileInteractionController.URL = file_URL;
  549. }
  550. // [self.fileInteractionController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
  551. [self.fileInteractionController setName:self.mail_subject];
  552. // [self.fileInteractionController setAnnotation:<#(id _Nullable)#>;
  553. [self.fileInteractionController presentOptionsMenuFromBarButtonItem:self.btnshare animated:YES];
  554. // [self.fileInteractionController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  555. // [self.fileInteractionController presentPreviewAnimated:YES];
  556. }
  557. }
  558. //- (void) sendEmail:(NSString *)to cc:(NSString*)cc subject:(NSString*)subject body:(NSString*)body
  559. //{
  560. // NSString* str = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@",
  561. // to, cc, subject, body];
  562. // str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  563. //
  564. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
  565. //
  566. //
  567. //}
  568. #pragma mark methods for the UIDocumentInteractionControllerDelegate
  569. - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
  570. {
  571. // if (controller == self.fileInteractionController) {
  572. // self.fileInteractionController = nil;
  573. // }
  574. }
  575. - (void)previewDocumentWithURL:(NSURL*)url
  576. {
  577. UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
  578. preview.delegate = self;
  579. [preview presentPreviewAnimated:YES];
  580. }
  581. - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
  582. {
  583. }
  584. - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
  585. return self;
  586. }
  587. - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
  588. return self.view.frame;
  589. }
  590. - (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
  591. return self.view;
  592. }
  593. - (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application // bundle ID
  594. {
  595. }
  596. #pragma mark - WKNavigationDelegate
  597. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
  598. {
  599. self.mum.hidden=true;
  600. if(self.onLoadSuccess)
  601. self.onLoadSuccess(self.url);
  602. if(self.autoPrint)
  603. [self startAirPrintWithData:self.content];
  604. }
  605. #pragma mark -MFMailComposeViewControllerDelegate delegate
  606. - (void)mailComposeController:(MFMailComposeViewController*)controller
  607. didFinishWithResult:(MFMailComposeResult)result
  608. error:(NSError*)error {
  609. switch (result)
  610. {
  611. case MFMailComposeResultCancelled:
  612. DebugLog(@"Mail send canceled...");
  613. break;
  614. case MFMailComposeResultSaved:
  615. DebugLog(@"Mail saved...");
  616. break;
  617. case MFMailComposeResultSent:
  618. DebugLog(@"Mail sent...");
  619. break;
  620. case MFMailComposeResultFailed:
  621. DebugLog(@"Mail send errored: %@...", [error localizedDescription]);
  622. break;
  623. default:
  624. break;
  625. }
  626. // [self dismissModalViewControllerAnimated:YES];
  627. [self dismissViewControllerAnimated:YES completion:nil];
  628. }
  629. /*
  630. #pragma mark - Navigation
  631. In a storyboard-based application, you will often want to do a little preparation before navigation
  632. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  633. {
  634. Get the new view controller using [segue destinationViewController].
  635. Pass the selected object to the new view controller.
  636. }
  637. */
  638. //- (void)startAirPrintWithData:(id )data
  639. //{
  640. //
  641. //
  642. // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
  643. //
  644. // UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  645. // UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  646. // printInfo.outputType = UIPrintInfoOutputGeneral;
  647. // printInfo.orientation = UIPrintInfoOrientationPortrait;
  648. // printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  649. // if(self.filename.length>0)
  650. // printInfo.jobName = self.filename;
  651. // airPrinterController.printInfo = printInfo;
  652. // airPrinterController.printingItem = data;
  653. // airPrinterController.delegate = self;
  654. //
  655. //
  656. // airPrinterController.showsNumberOfCopies=true;
  657. // airPrinterController.showsPageRange = true;
  658. // airPrinterController.showsPaperSelectionForLoadedPapers=true;
  659. //
  660. // void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  661. // if(completed && error)
  662. // 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);
  663. // };
  664. //
  665. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  666. //
  667. // UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  668. //
  669. //
  670. //
  671. //
  672. // [[UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL]] contactPrinter:^(BOOL available)
  673. // {
  674. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  675. //
  676. // if (available&& appDelegate.printerURL.length>0)
  677. // {
  678. //
  679. //
  680. // [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  681. // DebugLog(@"AIRPRINTER AVAILABLE");
  682. // }
  683. // else
  684. // {
  685. //
  686. // // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  687. // //
  688. // //
  689. // //
  690. // // // [printPicker presentAnimated:YES completionHandler:
  691. // // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  692. // // {
  693. // // if (userDidSelect)
  694. // // {
  695. // // NSString* sid= printerController.printInfo.printerID;
  696. // // UIPrintPaper*paper= printerController.printPaper;
  697. // //
  698. // // }
  699. // // }];
  700. //
  701. //
  702. // DebugLog(@"AIRPRINTER NOT AVAILABLE");
  703. //
  704. //
  705. //
  706. // if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  707. // {
  708. // UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  709. // // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  710. //
  711. //
  712. //
  713. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  714. //
  715. //
  716. // // [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  717. //
  718. //
  719. //
  720. // // [printPicker presentAnimated:YES completionHandler:
  721. // ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  722. // {
  723. // if (userDidSelect)
  724. // {
  725. // //User selected the item in the UIPrinterPickerController and got the printer details.
  726. //
  727. // [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  728. //
  729. // //Here you will get the printer and printer details.ie,
  730. // // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  731. //
  732. //
  733. //
  734. //
  735. // appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  736. //
  737. // [self startAirPrintWithData:data];
  738. //
  739. // }
  740. // else
  741. // {
  742. //
  743. //
  744. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  745. //// if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  746. //// {
  747. ////
  748. //// [((MainViewController*)appDelegate.main_vc) Loginout:false];
  749. //// }
  750. //// [self prepareReturn:nil];
  751. //
  752. // //[self.navigationController popViewControllerAnimated:true];
  753. // }
  754. // }];
  755. // }
  756. // }
  757. // }];
  758. //
  759. //
  760. //
  761. //}
  762. - (void)startAirPrintWithData:(id )data
  763. {
  764. UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
  765. UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  766. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  767. printInfo.outputType = UIPrintInfoOutputGeneral;
  768. printInfo.orientation = UIPrintInfoOrientationPortrait;
  769. printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  770. if(self.filename.length>0)
  771. printInfo.jobName = self.filename;
  772. airPrinterController.printInfo = printInfo;
  773. airPrinterController.printingItem = data;
  774. airPrinterController.delegate = self;
  775. airPrinterController.showsNumberOfCopies=true;
  776. // airPrinterController.showsPageRange = true;
  777. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  778. void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  779. if(completed && error)
  780. {
  781. // 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);
  782. }
  783. };
  784. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  785. if(appDelegate.printerURL==nil)
  786. {
  787. [waitalert dismissViewControllerAnimated:YES completion:nil];
  788. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  789. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  790. {
  791. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  792. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  793. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  794. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  795. // [printPicker presentAnimated:YES completionHandler:
  796. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  797. {
  798. if (userDidSelect)
  799. {
  800. //User selected the item in the UIPrinterPickerController and got the printer details.
  801. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  802. //Here you will get the printer and printer details.ie,
  803. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  804. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  805. [self startAirPrintWithData:data];
  806. }
  807. else
  808. {
  809. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  810. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  811. // {
  812. //
  813. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  814. // }
  815. // [self prepareReturn:nil];
  816. //[self.navigationController popViewControllerAnimated:true];
  817. }
  818. }];
  819. }
  820. }
  821. else
  822. {
  823. UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  824. [airPrinter contactPrinter:^(BOOL available)
  825. {
  826. [waitalert dismissViewControllerAnimated:YES completion:nil];
  827. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  828. if (available&& appDelegate.printerURL.length>0)
  829. {
  830. [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  831. DebugLog(@"AIRPRINTER AVAILABLE");
  832. }
  833. else
  834. {
  835. appDelegate.printerURL = nil;
  836. // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  837. //
  838. //
  839. //
  840. // // [printPicker presentAnimated:YES completionHandler:
  841. // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  842. // {
  843. // if (userDidSelect)
  844. // {
  845. // NSString* sid= printerController.printInfo.printerID;
  846. // UIPrintPaper*paper= printerController.printPaper;
  847. //
  848. // }
  849. // }];
  850. DebugLog(@"AIRPRINTER NOT AVAILABLE");
  851. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  852. {
  853. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  854. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  855. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  856. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  857. // [printPicker presentAnimated:YES completionHandler:
  858. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  859. {
  860. if (userDidSelect)
  861. {
  862. //User selected the item in the UIPrinterPickerController and got the printer details.
  863. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  864. //Here you will get the printer and printer details.ie,
  865. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  866. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  867. [self startAirPrintWithData:data];
  868. }
  869. else
  870. {
  871. appDelegate.printerURL = nil;
  872. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  873. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  874. // {
  875. //
  876. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  877. // }
  878. // [self prepareReturn:nil];
  879. //[self.navigationController popViewControllerAnimated:true];
  880. }
  881. }];
  882. }
  883. }
  884. }];
  885. }
  886. }
  887. #pragma mark - UIPrintInteractionControllerDelegate
  888. - (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray<UIPrintPaper *> *)paperList
  889. {
  890. ;
  891. // (int) width = 29700
  892. // Printing description of (*( (int *)0x7caa3a40)):
  893. // (int) height = 42000
  894. //设置纸张大小
  895. CGSize paperSize=[PDFUtils QueryPDFSizeFromData:self.content];
  896. for(UIPrintPaper* paper in paperList)
  897. {
  898. if (CGSizeEqualToSize(paper.paperSize, paperSize))
  899. return paper;
  900. }
  901. // UIPrintPaper * pp=[[UIPrintPaper alloc] init];
  902. // CGSize paperSize = CGSizeMake(612, 792);
  903. UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList];
  904. return p;
  905. }
  906. - (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  907. {
  908. }
  909. - (void)printInteractionControllerDidPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  910. {
  911. }
  912. - (void)printInteractionControllerWillDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  913. {
  914. }
  915. - (void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  916. {
  917. }
  918. - (void)printInteractionControllerWillStartJob:(UIPrintInteractionController *)printInteractionController
  919. {
  920. }
  921. - (void)printInteractionControllerDidFinishJob:(UIPrintInteractionController *)printInteractionController
  922. {
  923. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  924. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  925. // {
  926. //
  927. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  928. // }
  929. //
  930. // [self prepareReturn:nil];
  931. if(self.autoPrint)
  932. [self.navigationController popViewControllerAnimated:true];
  933. }
  934. @end