pdfCreator.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // pdfCreator.m
  3. // iSales-NPD
  4. //
  5. // Created by Ray on 10/21/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "pdfCreator.h"
  9. #import "UIKit/UIDocument.h"
  10. #import "RAUtils.h"
  11. #import "RAPDFPage.h"
  12. @implementation pdfCreator
  13. +(void) drawPage:(CGContextRef)context template:(NSMutableDictionary*) page size:(CGRect) rect dataSource:(NSMutableDictionary*)data
  14. {
  15. RAPDFPage* pdfPage = [[RAPDFPage alloc] init:page size:rect];
  16. [pdfPage Draw:context dataSource:data];
  17. //NSMutableDictionary* header=page[@"header"] ;
  18. // double margin_left=[page[@"margin_left"] doubleValue];
  19. // double margin_left=[page[@"margin_left"] doubleValue];
  20. }
  21. +(int) QueryPageCount:(CGContextRef)context template:(NSMutableDictionary*) page size:(CGRect) rect dataSource:(NSMutableDictionary*)data
  22. {
  23. RAPDFPage* pdfPage = [[RAPDFPage alloc] init:page size:rect];
  24. return [pdfPage QueryPageCount:context dataSource:data];
  25. //NSMutableDictionary* header=page[@"header"] ;
  26. // double margin_left=[page[@"margin_left"] doubleValue];
  27. // double margin_left=[page[@"margin_left"] doubleValue];
  28. }
  29. +(int)QueryTotalPage:(CGContextRef)pdfContext template:(NSMutableDictionary*) template dataSource:(NSMutableDictionary*)data
  30. {
  31. int count=0;
  32. //获取路径
  33. // NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//坐标
  34. //
  35. // NSString *saveDirectory=[paths objectAtIndex:0];
  36. //
  37. // NSString *saveFileName=@"myPDF.pdf";
  38. //
  39. // NSString *newFilePath=[saveDirectory stringByAppendingPathComponent:saveFileName];
  40. //
  41. // const char *filename=[newFilePath UTF8String];
  42. //
  43. // DebugLog(@"%@",newFilePath);
  44. //设置页面大小 Letter纸
  45. CGRect papersize=CGRectMake(0, 0, [template[@"paper"][@"width"] intValue], [template[@"paper"][@"height"] intValue]);
  46. //关联上下文的对象
  47. // CGContextRef pdfContext;
  48. // CFStringRef path;
  49. //
  50. // CFURLRef url;
  51. //
  52. // path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
  53. //
  54. // url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
  55. //
  56. //
  57. // pdfContext=CGPDFContextCreateWithURL(url, &papersize, nil);
  58. //
  59. NSMutableDictionary* pages=template[@"pages"];
  60. for(int i=0;i<[pages[@"count"] intValue];i++)
  61. {
  62. NSMutableDictionary* page=pages[[ NSString stringWithFormat:@"page_%d",i ] ];
  63. count+=[self QueryPageCount:pdfContext template:page size:papersize dataSource:data];
  64. }
  65. //开始画pdf
  66. // NSString *temtext=[[NSString alloc]init];
  67. //
  68. // const char *text=(char *)[temtext UTF8String];
  69. //
  70. // int width;
  71. //
  72. // int height;
  73. //
  74. // [self newpage:pdfContext size:papersize];
  75. // CGContextRelease(pdfContext);
  76. return count;
  77. }
  78. +(NSString*)CreatePdf:(NSMutableDictionary*) template dataSource:(NSMutableDictionary*)data
  79. {
  80. //获取路径
  81. NSString* tempDir = NSTemporaryDirectory();
  82. NSString *saveFileName=[NSString stringWithFormat:@"%@.pdf",[[NSUUID UUID ] UUIDString] ] ;
  83. NSString *newFilePath=[tempDir stringByAppendingPathComponent:saveFileName];
  84. const char *filename=[newFilePath UTF8String];
  85. DebugLog(@"%@",newFilePath);
  86. //设置页面大小 Letter纸
  87. CGRect papersize=CGRectMake(0, 0, [template[@"paper"][@"width"] intValue], [template[@"paper"][@"height"] intValue]);
  88. //关联上下文的对象
  89. CGContextRef pdfContext;
  90. CFStringRef path;
  91. CFURLRef url;
  92. path=CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
  93. url=CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0);
  94. CFBridgingRelease(path);
  95. pdfContext=CGPDFContextCreateWithURL(url, &papersize, nil);
  96. CFBridgingRelease(url);
  97. data[@"TOTAL_PAGE"]= [NSNumber numberWithInt:[self QueryTotalPage:pdfContext template:template dataSource:data]];
  98. NSMutableDictionary* pages=template[@"pages"];
  99. for(int i=0;i<[pages[@"count"] intValue];i++)
  100. {
  101. NSMutableDictionary* page=pages[[ NSString stringWithFormat:@"page_%d",i ] ];
  102. data[@"CURRENT_PAGE"]=[NSNumber numberWithInt:[data[@"CURRENT_PAGE"] intValue]+1];
  103. [self drawPage:pdfContext template:page size:papersize dataSource:data];
  104. }
  105. //开始画pdf
  106. // NSString *temtext=[[NSString alloc]init];
  107. //
  108. // const char *text=(char *)[temtext UTF8String];
  109. //
  110. // int width;
  111. //
  112. // int height;
  113. //
  114. // [self newpage:pdfContext size:papersize];
  115. CGContextRelease(pdfContext);
  116. // NSURL* nsurl=CFBridgingRelease(url);
  117. return newFilePath;
  118. }
  119. @end