-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBBPdfGenerator.m
217 lines (177 loc) · 7.99 KB
/
BBPdfGenerator.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// BBPdfGenerator.m
// iCare2
//
// Created by Bogdan Marinescu on 9/16/14.
// Copyright (c) 2014 Spartan Systems Inc. All rights reserved.
//
#import <CoreText/CoreText.h>
#import "BBPdfGenerator.h"
#import "Form.h"
#import "Operation.h"
#import "Patient.h"
#import "FormSection.h"
#import "FormGroup.h"
#import "FormSection.h"
#import "BooleanFormElement.h"
#import "StringListElement.h"
#import "BBPdfSectionBuilder.h"
#import "BBUtil.h"
#import "IntraOp.h"
#define PAGE_WIDTH 612
#define PAGE_HEIGHT 792
@implementation BBPdfGenerator
+(NSString*)getPDFFileNameForForm:(Form*)form
{
NSString* fileNameToPassToPDFRenderer = [NSString stringWithFormat:@"%@%@%@%@.pdf",
form.title,
form.operation.patient.lastName,
form.operation.name,
form.operation.preOpDate];
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileNameToPassToPDFRenderer];
NSLog(@"NAME:%@", pdfFileName);
return pdfFileName;
}
+(NSString*)getPDFFileNameForIntraOp:(IntraOp*)form
{
NSString* fileNameToPassToPDFRenderer = [NSString stringWithFormat:@"%@%@%@%@.pdf",
@"IntraOp",
form.operation.patient.lastName,
form.operation.name,
form.anesthesiaStart];
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileNameToPassToPDFRenderer];
NSLog(@"NAME:%@", pdfFileName);
return pdfFileName;
}
+ (bool) generatePdfForForm:(Form*)form
{
UIGraphicsBeginPDFContextToFile([BBPdfGenerator getPDFFileNameForForm:form], CGRectZero, nil);
UIGraphicsBeginPDFPage();
NSString *formTitle = form.title;
NSString *patientName = [NSString stringWithFormat:@"%@, %@", form.operation.patient.lastName, form.operation.patient.firstName];
NSString *operation = form.operation.name;
NSString *operationDate = [BBUtil formatDate:form.operation.preOpDate];
CGPoint recordLabelPoint = CGPointMake((PAGE_WIDTH - [BBPdfGenerator getStringSize:formTitle withFont:[UIFont boldSystemFontOfSize:16.0f]].width)/2.0, 20.0f);
[BBPdfGenerator drawText:formTitle atLocation:recordLabelPoint withFont:[UIFont boldSystemFontOfSize:16.0f]];
[BBPdfGenerator drawText:patientName atLocation:CGPointMake(20.0, 15*3) isBold:YES];
[BBPdfGenerator drawText:operation atLocation:CGPointMake(20.0, 15*4) isBold:YES];
[BBPdfGenerator drawText:operationDate atLocation:CGPointMake(20.0, 15*5) isBold:YES];
CGPoint location = CGPointMake(20, 15*7.0);
float sectionPadding = 8.0f;
for ( FormSection* section in form.sections ){
int sizeLeftInPage = PAGE_HEIGHT - location.y;
if (sizeLeftInPage < [BBPdfGenerator measureSection:section].height) {
UIGraphicsBeginPDFPage();
location.y = 20.0;
}
CGSize size = [BBPdfSectionBuilder drawSection:(FormSection*)section atLocation:location];
[BBUtil drawLineFromPoint:CGPointMake(20.0, location.y + size.height + sectionPadding) toPoint:CGPointMake(592.0, location.y + size.height + sectionPadding)];
//[BBUtil drawRect:CGRectMake(location.x, location.y, size.width, size.height)];
location.y += size.height + sectionPadding*2;
}
UIGraphicsEndPDFContext();
return true;
}
+(CGSize)measureSection:(FormSection*)section
{
return [BBPdfSectionBuilder drawSection:(FormSection*)section atLocation:CGPointMake(2000, 2000)];
}
+(CGSize)getStringSize:(NSString*)text withFont:(UIFont*)font
{
CGSize size = [text sizeWithAttributes:
@{NSFontAttributeName:
font}];
return size;
}
+(CGSize)drawCheckBoxChecked:(BOOL)checked atLocation:(CGPoint)location
{
UIImage* logo = checked ? [UIImage imageNamed:@"checkbox_selected.png"] : [UIImage imageNamed:@"checkbox_unselected.png"];
CGRect frame = CGRectMake(location.x,location.y, 14, 14);
[logo drawInRect:frame];
return frame.size;
}
+(CGSize)drawUpArrow:(BOOL)checked atLocation:(CGPoint)location
{
UIImage* logo = checked ? [UIImage imageNamed:@"up_arrow_selected.png"] : [UIImage imageNamed:@"up_arrow_unselected.png"];
CGRect frame = CGRectMake(location.x,location.y, 14, 14);
[logo drawInRect:frame];
return frame.size;
}
+(CGSize)drawDownArrow:(BOOL)checked atLocation:(CGPoint)location
{
UIImage* logo = checked ? [UIImage imageNamed:@"down_arrow_selected.png"] : [UIImage imageNamed:@"down_arrow_unselected.png"];
CGRect frame = CGRectMake(location.x,location.y, 14, 14);
[logo drawInRect:frame];
return frame.size;
}
+(CGSize)drawText:(id)text atLocation:(CGPoint)location
{
return [BBPdfGenerator drawText:text atLocation:location isBold:NO];
}
+(CGSize)drawText:(NSString*)text atLocation:(CGPoint)location withFont:(UIFont*)font withAttribs:(NSDictionary*)attr
{
CGSize size = [text sizeWithAttributes:
@{NSFontAttributeName:
font}];
CGRect frameRect = CGRectMake(location.x, location.y, size.width, size.height);
[text drawInRect:frameRect withAttributes:attr];
return size;
}
+(CGSize)drawText:(NSString*)text atLocation:(CGPoint)location withFont:(UIFont*)font
{
NSDictionary *attr = [[NSDictionary alloc] initWithObjectsAndKeys: font, NSFontAttributeName,
nil];
return [self drawText:text atLocation:location withFont:font withAttribs:attr];
}
+(CGSize)drawText:(id)text atLocation:(CGPoint)location isBold:(BOOL)bold
{
UIFont *font;
if (!bold) {
font = [UIFont systemFontOfSize:12.0f];
} else {
font = [UIFont boldSystemFontOfSize:12.0f];
}
NSDictionary *attr = [[NSDictionary alloc] initWithObjectsAndKeys: font, NSFontAttributeName,
nil];
return [BBPdfGenerator drawText:text atLocation:location withFont:font withAttribs:attr];
}
+(CGSize)drawText:(id)text atLocation:(CGPoint)location isUnderlined:(BOOL)underline
{
UIFont *font = [UIFont systemFontOfSize:12.0f];;
NSDictionary *attr;
if (underline) {
attr = [[NSDictionary alloc] initWithObjectsAndKeys: font, NSFontAttributeName,@(NSUnderlineStyleSingle),NSUnderlineStyleAttributeName, nil];
} else {
attr = [[NSDictionary alloc] initWithObjectsAndKeys: font, NSFontAttributeName, nil];
}
return [BBPdfGenerator drawText:text atLocation:location withFont:font withAttribs:attr];
}
+(CGSize)drawTextBox:(NSString*)text atLocation:(CGPoint)location width:(int)width
{
UIFont *font = [UIFont systemFontOfSize:12.0f];
CGRect frame = [text boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName: font}
context:nil];
NSDictionary *attr = [[NSDictionary alloc] initWithObjectsAndKeys: font, NSFontAttributeName,
nil];
frame.origin.x = location.x+4;
frame.origin.y = location.y+4;
[text drawInRect:frame withAttributes:attr];
frame = CGRectMake(frame.origin.x-4, frame.origin.y-4, frame.size.width + 8, frame.size.height + 8);
[BBUtil drawRect:frame];
return frame.size;
}
@end