pdfCreator.m 4.8 KB

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