RAPDFViewController.m 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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:^{
  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. }];
  228. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  229. //
  230. // NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60*5];
  231. //
  232. // //设置请求方式为get
  233. //
  234. // [request setHTTPMethod:@"GET"];
  235. //
  236. // //添加用户会话id
  237. //
  238. // [request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
  239. //
  240. // //连接发送请求
  241. //
  242. //
  243. // NSHTTPURLResponse* urlResponse = nil;
  244. //
  245. // NSError *error = nil;
  246. //
  247. // self.content= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
  248. //
  249. // if(self.filename.length<=0)
  250. // self.filename = [urlResponse suggestedFilename];
  251. //
  252. //
  253. // dispatch_async(dispatch_get_main_queue(), ^{
  254. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  255. //
  256. // [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
  257. // self.navigationItem.title = self.save_name;
  258. // UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  259. // // self.content=nil;
  260. // if(self.content!=nil&&self.content.length>0)
  261. // {
  262. // [reF removeFromSuperview];
  263. //
  264. //
  265. // // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  266. // NSString *path = NSTemporaryDirectory();
  267. // NSString *filePath = [path stringByAppendingPathComponent:self.filename];
  268. // // NSError *error = nil;
  269. // // BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
  270. // // NSAssert(bo,@"创建目录失败");
  271. // if (self.cachePath.length) {
  272. // filePath = self.cachePath;
  273. // }
  274. //
  275. // [self.content writeToFile:filePath atomically:YES];
  276. //
  277. // self.url = filePath;
  278. // self.isLocalfile = true;
  279. //
  280. //
  281. // self.btnshare.enabled =true;
  282. //
  283. //
  284. // self.btnemail.enabled =true;
  285. // self.btnsave.enabled =true;
  286. //
  287. //
  288. //
  289. // NSURL *url = [NSURL fileURLWithPath:filePath];
  290. // NSURLRequest *request = [NSURLRequest requestWithURL:url];
  291. // [self.wkWebview loadRequest:request];
  292. //
  293. //
  294. //
  295. //
  296. //
  297. //
  298. // // int a=0;
  299. // }
  300. // else
  301. // {
  302. // [RAUtils message_alert:nil title:@"Open PDF Failed" controller:self];
  303. //
  304. // if(self.autoPrint)
  305. // [self.navigationController popViewControllerAnimated:true];
  306. // // self.content = [self.url dataUsingEncoding:NSUTF8StringEncoding];
  307. // }
  308. //
  309. // ;
  310. // // DebugLog(@"%@",[[NSString alloc] initWithData:self.content encoding:NSASCIIStringEncoding]);
  311. // //
  312. //
  313. // // DebugLog(urlResponse.MIMEType);
  314. //
  315. // // [self.webView loadData:self.content MIMEType:urlResponse.MIMEType textEncodingName:nil baseURL:nil];
  316. //
  317. //
  318. //
  319. //
  320. // });
  321. // });
  322. }
  323. }
  324. -(void)changeCell
  325. {
  326. UIRefreshControl *reF = (UIRefreshControl *)[self.view viewWithTag:200];
  327. reF.attributedTitle = [[NSAttributedString alloc]initWithString:@"refreshing"];
  328. if ([self respondsToSelector:@selector(ReloadData)])
  329. [self performSelector:@selector(ReloadData) withObject:nil afterDelay:1];
  330. // DebugLog(@"refresh!!!!!!!!");
  331. }
  332. - (void)didReceiveMemoryWarning
  333. {
  334. [super didReceiveMemoryWarning];
  335. // Dispose of any resources that can be recreated.
  336. }
  337. -(void) viewWillAppear:(BOOL)animated
  338. {
  339. [super viewWillAppear:animated];
  340. [[self navigationController] setNavigationBarHidden:self.hidenavi animated:NO];
  341. }
  342. - (IBAction)onPrintClick:(id)sender {
  343. // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
  344. UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  345. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  346. printInfo.outputType = UIPrintInfoOutputGeneral;
  347. printInfo.orientation = UIPrintInfoOrientationPortrait;
  348. printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  349. if(self.filename.length>0)
  350. printInfo.jobName = self.filename;
  351. airPrinterController.printInfo = printInfo;
  352. airPrinterController.printingItem = self.content;
  353. airPrinterController.delegate = self;
  354. airPrinterController.showsNumberOfCopies=true;
  355. // airPrinterController.showsPageRange = true;
  356. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  357. [airPrinterController presentFromBarButtonItem:self.btnPrint
  358. animated:YES
  359. completionHandler:
  360. ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
  361. }
  362. ];
  363. //
  364. // void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  365. // if(completed && error)
  366. // 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);
  367. // };
  368. //
  369. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  370. //
  371. // UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  372. //
  373. //
  374. //
  375. //
  376. // [[UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL]] contactPrinter:^(BOOL available)
  377. // {
  378. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  379. //
  380. // if (available&& appDelegate.printerURL.length>0)
  381. // {
  382. //
  383. //
  384. // [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  385. // DebugLog(@"AIRPRINTER AVAILABLE");
  386. // }
  387. // else
  388. // {
  389. //
  390. // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  391. //
  392. //
  393. //
  394. // // [printPicker presentAnimated:YES completionHandler:
  395. // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  396. // {
  397. // if (userDidSelect)
  398. // {
  399. // NSString* sid= printerController.printInfo.printerID;
  400. // UIPrintPaper*paper= printerController.printPaper;
  401. //
  402. // }
  403. // }];
  404. //
  405. //
  406. // DebugLog(@"AIRPRINTER NOT AVAILABLE");
  407. //
  408. //
  409. //
  410. //// if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  411. //// {
  412. //// UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  413. //// // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  414. ////
  415. ////
  416. ////
  417. //// [ printPicker presentFromBarButtonItem:self.btnPrint animated:YES completionHandler:
  418. ////
  419. ////
  420. //// // [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  421. ////
  422. ////
  423. ////
  424. //// // [printPicker presentAnimated:YES completionHandler:
  425. //// ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  426. //// {
  427. //// if (userDidSelect)
  428. //// {
  429. //// //User selected the item in the UIPrinterPickerController and got the printer details.
  430. ////
  431. //// [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  432. ////
  433. //// //Here you will get the printer and printer details.ie,
  434. //// // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  435. ////
  436. ////
  437. ////
  438. ////
  439. //// appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  440. ////
  441. //// [self startAirPrintWithData:self.content];
  442. ////
  443. //// }
  444. //// else
  445. //// {
  446. ////
  447. ////
  448. //// // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  449. //// // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  450. //// // {
  451. //// //
  452. //// // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  453. //// // }
  454. //// // [self prepareReturn:nil];
  455. ////
  456. //// //[self.navigationController popViewControllerAnimated:true];
  457. //// }
  458. //// }];
  459. //// }
  460. // }
  461. // }];
  462. //
  463. }
  464. - (IBAction)onActionClick:(id)sender {
  465. //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  466. // NSString *filePath = nil;
  467. // if(!self.isLocalfile)
  468. // {
  469. // NSString *path = NSTemporaryDirectory();
  470. // NSString* filename =self.filename;
  471. //
  472. // filePath= [path stringByAppendingPathComponent:filename];
  473. //
  474. // }
  475. // else
  476. // filePath = self.url;
  477. [self openFile:self.url];
  478. }
  479. - (IBAction)onSaveClick:(id)sender {
  480. //UIAlertControllerStyle两种类型UIAlertControllerStyleAlert类似UIAlertView
  481. //UIAlertControllerStyleActionSheet类似UIActionSheet
  482. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Save pdf" message:nil preferredStyle:UIAlertControllerStyleAlert];
  483. //block代码块取代了delegate
  484. [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  485. textField.text = self.save_name;
  486. }];
  487. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  488. UITextField *name = alertControl.textFields.firstObject;
  489. if(name.text.length==0)
  490. {
  491. [RAUtils message_alert:@"Cannot save without name. Save canceled." title:@"Name is missing" controller:self];
  492. }
  493. else
  494. {
  495. self.save_name = name.text;
  496. self.btnsave.enabled =false;
  497. // NSMutableDictionary * info =
  498. if(self.onSavePDF)
  499. self.onSavePDF(self.save_name,self.filename);
  500. }
  501. }];
  502. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  503. DebugLog(@"Cancel");
  504. }];
  505. [alertControl addAction:actionOne];
  506. [alertControl addAction:alertthree];
  507. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  508. [self presentViewController:alertControl animated:YES completion:nil];
  509. }
  510. - (IBAction)onEmailClick:(id)sender {
  511. //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  512. // NSString *path = NSTemporaryDirectory();
  513. NSString* filename =self.filename;
  514. // NSString *filePath = [path stringByAppendingPathComponent:filename];
  515. NSString *filePath = self.url;
  516. MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
  517. if(mc==nil)
  518. {
  519. //[RAUtils message_alert:@"You must create an email account first." title:@"Send Email" controller:self];
  520. return;
  521. }
  522. mc.mailComposeDelegate = self;
  523. [mc setSubject:self.mail_subject];
  524. [mc setToRecipients:self.mail_to];
  525. if(self.mail_content==nil)
  526. self.mail_content=@"";
  527. NSString* htmlbody = [NSString stringWithFormat:@"<html><p>%@</p></html>",self.mail_content];
  528. [mc setMessageBody:htmlbody
  529. isHTML:YES];
  530. NSData *data = [NSData dataWithContentsOfFile:filePath];
  531. // if(self.attachment_name.length>0)
  532. // filename = self.attachment_name;
  533. [mc addAttachmentData:data mimeType:@"application/pdf" fileName:filename];
  534. // [self presentModalViewController:mc animated:YES];
  535. [self presentViewController:mc animated:YES completion:nil];
  536. return;
  537. }
  538. -(void)openFile: (NSString *) file_url {
  539. NSURL *file_URL = [NSURL fileURLWithPath:file_url];
  540. if (file_URL != nil) {
  541. if (self.fileInteractionController == nil) {
  542. //_fileInteractionController = [[UIDocumentInteractionController alloc] init];
  543. _fileInteractionController = [UIDocumentInteractionController interactionControllerWithURL:file_URL];
  544. _fileInteractionController.delegate = self;
  545. // [_fileInteractionController retain];//不适用ARC,记得RETAIN 使用ARC注释这一行
  546. }
  547. else
  548. {
  549. self.fileInteractionController.URL = file_URL;
  550. }
  551. // [self.fileInteractionController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
  552. [self.fileInteractionController setName:self.mail_subject];
  553. // [self.fileInteractionController setAnnotation:<#(id _Nullable)#>;
  554. [self.fileInteractionController presentOptionsMenuFromBarButtonItem:self.btnshare animated:YES];
  555. // [self.fileInteractionController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  556. // [self.fileInteractionController presentPreviewAnimated:YES];
  557. }
  558. }
  559. //- (void) sendEmail:(NSString *)to cc:(NSString*)cc subject:(NSString*)subject body:(NSString*)body
  560. //{
  561. // NSString* str = [NSString stringWithFormat:@"mailto:%@?cc=%@&subject=%@&body=%@",
  562. // to, cc, subject, body];
  563. // str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  564. //
  565. // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
  566. //
  567. //
  568. //}
  569. #pragma mark methods for the UIDocumentInteractionControllerDelegate
  570. - (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *)controller
  571. {
  572. // if (controller == self.fileInteractionController) {
  573. // self.fileInteractionController = nil;
  574. // }
  575. }
  576. - (void)previewDocumentWithURL:(NSURL*)url
  577. {
  578. UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
  579. preview.delegate = self;
  580. [preview presentPreviewAnimated:YES];
  581. }
  582. - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
  583. {
  584. }
  585. - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
  586. return self;
  587. }
  588. - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
  589. return self.view.frame;
  590. }
  591. - (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
  592. return self.view;
  593. }
  594. - (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(nullable NSString *)application // bundle ID
  595. {
  596. }
  597. #pragma mark - WKNavigationDelegate
  598. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
  599. {
  600. self.mum.hidden=true;
  601. if(self.onLoadSuccess)
  602. self.onLoadSuccess(self.url);
  603. if(self.autoPrint)
  604. [self startAirPrintWithData:self.content];
  605. }
  606. #pragma mark -MFMailComposeViewControllerDelegate delegate
  607. - (void)mailComposeController:(MFMailComposeViewController*)controller
  608. didFinishWithResult:(MFMailComposeResult)result
  609. error:(NSError*)error {
  610. switch (result)
  611. {
  612. case MFMailComposeResultCancelled:
  613. DebugLog(@"Mail send canceled...");
  614. break;
  615. case MFMailComposeResultSaved:
  616. DebugLog(@"Mail saved...");
  617. break;
  618. case MFMailComposeResultSent:
  619. DebugLog(@"Mail sent...");
  620. break;
  621. case MFMailComposeResultFailed:
  622. DebugLog(@"Mail send errored: %@...", [error localizedDescription]);
  623. break;
  624. default:
  625. break;
  626. }
  627. // [self dismissModalViewControllerAnimated:YES];
  628. [self dismissViewControllerAnimated:YES completion:nil];
  629. }
  630. /*
  631. #pragma mark - Navigation
  632. In a storyboard-based application, you will often want to do a little preparation before navigation
  633. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  634. {
  635. Get the new view controller using [segue destinationViewController].
  636. Pass the selected object to the new view controller.
  637. }
  638. */
  639. //- (void)startAirPrintWithData:(id )data
  640. //{
  641. //
  642. //
  643. // UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
  644. //
  645. // UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  646. // UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  647. // printInfo.outputType = UIPrintInfoOutputGeneral;
  648. // printInfo.orientation = UIPrintInfoOrientationPortrait;
  649. // printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  650. // if(self.filename.length>0)
  651. // printInfo.jobName = self.filename;
  652. // airPrinterController.printInfo = printInfo;
  653. // airPrinterController.printingItem = data;
  654. // airPrinterController.delegate = self;
  655. //
  656. //
  657. // airPrinterController.showsNumberOfCopies=true;
  658. // airPrinterController.showsPageRange = true;
  659. // airPrinterController.showsPaperSelectionForLoadedPapers=true;
  660. //
  661. // void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  662. // if(completed && error)
  663. // 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);
  664. // };
  665. //
  666. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  667. //
  668. // UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  669. //
  670. //
  671. //
  672. //
  673. // [[UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL]] contactPrinter:^(BOOL available)
  674. // {
  675. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  676. //
  677. // if (available&& appDelegate.printerURL.length>0)
  678. // {
  679. //
  680. //
  681. // [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  682. // DebugLog(@"AIRPRINTER AVAILABLE");
  683. // }
  684. // else
  685. // {
  686. //
  687. // // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  688. // //
  689. // //
  690. // //
  691. // // // [printPicker presentAnimated:YES completionHandler:
  692. // // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  693. // // {
  694. // // if (userDidSelect)
  695. // // {
  696. // // NSString* sid= printerController.printInfo.printerID;
  697. // // UIPrintPaper*paper= printerController.printPaper;
  698. // //
  699. // // }
  700. // // }];
  701. //
  702. //
  703. // DebugLog(@"AIRPRINTER NOT AVAILABLE");
  704. //
  705. //
  706. //
  707. // if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  708. // {
  709. // UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  710. // // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  711. //
  712. //
  713. //
  714. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  715. //
  716. //
  717. // // [printPicker presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  718. //
  719. //
  720. //
  721. // // [printPicker presentAnimated:YES completionHandler:
  722. // ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  723. // {
  724. // if (userDidSelect)
  725. // {
  726. // //User selected the item in the UIPrinterPickerController and got the printer details.
  727. //
  728. // [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  729. //
  730. // //Here you will get the printer and printer details.ie,
  731. // // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  732. //
  733. //
  734. //
  735. //
  736. // appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  737. //
  738. // [self startAirPrintWithData:data];
  739. //
  740. // }
  741. // else
  742. // {
  743. //
  744. //
  745. //// AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  746. //// if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  747. //// {
  748. ////
  749. //// [((MainViewController*)appDelegate.main_vc) Loginout:false];
  750. //// }
  751. //// [self prepareReturn:nil];
  752. //
  753. // //[self.navigationController popViewControllerAnimated:true];
  754. // }
  755. // }];
  756. // }
  757. // }
  758. // }];
  759. //
  760. //
  761. //
  762. //}
  763. - (void)startAirPrintWithData:(id )data
  764. {
  765. UIAlertController * waitalert = [RAUtils waiting_alert:self title:@"preparing print..."];
  766. UIPrintInteractionController* airPrinterController=[UIPrintInteractionController sharedPrintController];
  767. UIPrintInfo *printInfo = [UIPrintInfo printInfo];
  768. printInfo.outputType = UIPrintInfoOutputGeneral;
  769. printInfo.orientation = UIPrintInfoOrientationPortrait;
  770. printInfo.jobName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
  771. if(self.filename.length>0)
  772. printInfo.jobName = self.filename;
  773. airPrinterController.printInfo = printInfo;
  774. airPrinterController.printingItem = data;
  775. airPrinterController.delegate = self;
  776. airPrinterController.showsNumberOfCopies=true;
  777. // airPrinterController.showsPageRange = true;
  778. airPrinterController.showsPaperSelectionForLoadedPapers=true;
  779. void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
  780. if(completed && error)
  781. {
  782. // 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);
  783. }
  784. };
  785. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  786. if(appDelegate.printerURL==nil)
  787. {
  788. [waitalert dismissViewControllerAnimated:YES completion:^{
  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. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  822. }
  823. else
  824. {
  825. UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:appDelegate.printerURL] ];///*self.printer;*/
  826. [airPrinter contactPrinter:^(BOOL available)
  827. {
  828. [waitalert dismissViewControllerAnimated:YES completion:^{
  829. if (available&& appDelegate.printerURL.length>0)
  830. {
  831. [airPrinterController printToPrinter:airPrinter completionHandler:completionHandler];
  832. DebugLog(@"AIRPRINTER AVAILABLE");
  833. }
  834. else
  835. {
  836. appDelegate.printerURL = nil;
  837. // [airPrinterController presentFromRect:CGRectMake(0, 0, 300, 500) inView:self.view animated:YES completionHandler:
  838. //
  839. //
  840. //
  841. // // [printPicker presentAnimated:YES completionHandler:
  842. // ^(UIPrintInteractionController *printerController, BOOL userDidSelect, NSError *error)
  843. // {
  844. // if (userDidSelect)
  845. // {
  846. // NSString* sid= printerController.printInfo.printerID;
  847. // UIPrintPaper*paper= printerController.printPaper;
  848. //
  849. // }
  850. // }];
  851. DebugLog(@"AIRPRINTER NOT AVAILABLE");
  852. if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  853. {
  854. UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
  855. // [printPicker presentFromBarButtonItem:self.btnSubmitOrder animated:yes completionHandler:
  856. // [ printPicker presentFromBarButtonItem:self.btnshare animated:YES completionHandler:
  857. [printPicker presentFromRect:CGRectMake(100, 100, 300, 500) inView:self.view animated:YES completionHandler:
  858. // [printPicker presentAnimated:YES completionHandler:
  859. ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error)
  860. {
  861. if (userDidSelect)
  862. {
  863. //User selected the item in the UIPrinterPickerController and got the printer details.
  864. [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];
  865. //Here you will get the printer and printer details.ie,
  866. // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.
  867. appDelegate.printerURL =printerPicker.selectedPrinter.URL.absoluteString;
  868. [self startAirPrintWithData:data];
  869. }
  870. else
  871. {
  872. appDelegate.printerURL = nil;
  873. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  874. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  875. // {
  876. //
  877. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  878. // }
  879. // [self prepareReturn:nil];
  880. //[self.navigationController popViewControllerAnimated:true];
  881. }
  882. }];
  883. }
  884. }
  885. }];
  886. // [waitalert dismissViewControllerAnimated:YES completion:nil];
  887. }];
  888. }
  889. }
  890. #pragma mark - UIPrintInteractionControllerDelegate
  891. - (UIPrintPaper *)printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray<UIPrintPaper *> *)paperList
  892. {
  893. ;
  894. // (int) width = 29700
  895. // Printing description of (*( (int *)0x7caa3a40)):
  896. // (int) height = 42000
  897. //设置纸张大小
  898. CGSize paperSize=[PDFUtils QueryPDFSizeFromData:self.content];
  899. for(UIPrintPaper* paper in paperList)
  900. {
  901. if (CGSizeEqualToSize(paper.paperSize, paperSize))
  902. return paper;
  903. }
  904. // UIPrintPaper * pp=[[UIPrintPaper alloc] init];
  905. // CGSize paperSize = CGSizeMake(612, 792);
  906. UIPrintPaper * p=[UIPrintPaper bestPaperForPageSize:paperSize withPapersFromArray:paperList];
  907. return p;
  908. }
  909. - (void)printInteractionControllerWillPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  910. {
  911. }
  912. - (void)printInteractionControllerDidPresentPrinterOptions:(UIPrintInteractionController *)printInteractionController
  913. {
  914. }
  915. - (void)printInteractionControllerWillDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  916. {
  917. }
  918. - (void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
  919. {
  920. }
  921. - (void)printInteractionControllerWillStartJob:(UIPrintInteractionController *)printInteractionController
  922. {
  923. }
  924. - (void)printInteractionControllerDidFinishJob:(UIPrintInteractionController *)printInteractionController
  925. {
  926. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  927. // if([appDelegate.mode isEqualToString:@"Trade Show Mode"]&& appDelegate.submit_order_logout)
  928. // {
  929. //
  930. // [((MainViewController*)appDelegate.main_vc) Loginout:false];
  931. // }
  932. //
  933. // [self prepareReturn:nil];
  934. if(self.autoPrint)
  935. [self.navigationController popViewControllerAnimated:true];
  936. }
  937. @end