1.把scrollView上面的內(nèi)容生成PDF文件娜谊,代碼如下:
- (NSString *)createPDF {
?NSDate*printTime = [NSDatedate];
NSString*fileName = [NSStringstringWithFormat:@"%@_%@_%@.pdf",self.user.real_name,self.title, [self.dateHourMinuteFormatterstringFromDate:printTime]];
? ? NSURL*storeURL = [selfbuildStoreURLForFileName:fileName];
? ? NSString*storePath = storeURL.path;
? ? CGFloatcontentViewWidth =CGRectGetWidth(self.scrollView.frame);
? ? CGFloatcontentViewHeight =CGRectGetHeight(self.scrollView.frame);
? ? CGFloatpageWidth = contentViewWidth;
? ? CGFloatpageHeight = contentViewHeight +143.f;
? ? UIGraphicsBeginPDFContextToFile(storePath,CGRectMake(0,0, pageWidth, pageHeight),nil);
NSString*dateText = [NSStringstringWithFormat:@"%@ (%d%@)",self.dataSelectView.label.text, (int)self.daysCount, (self.daysCount>1) ?MyLocalizedString(@"unit.days",nil) :MyLocalizedString(@"unit.day",nil)];
? ? NSDictionary*pdmSettings = (NSDictionary*)self.user.settings.pdmSettings;
? ? NSString*pdmSN =@"";
? ? if([pdmSettingsobjectForKey:@"PDMSN"]) {
? ? ? ? pdmSN = [AppDelegatetransformSerialString:[pdmSettingsobjectForKey:@"PDMSN"]];
? ? }
? ? NSString*pumpBaseSN =@"";
? ? if([pdmSettingsobjectForKey:@"PumpBaseSN"]) {
? ? ? ? pumpBaseSN = [AppDelegatetransformSerialString:[pdmSettingsobjectForKey:@"PumpBaseSN"]];
? ? }
? ? NSString*transmitterSN =@"";
? ? if([pdmSettingsobjectForKey:@"TransmitterSN"]) {
? ? ? ? transmitterSN = [AppDelegatetransformSerialString:[pdmSettingsobjectForKey:@"TransmitterSN"]];
? ? }
? ? NSUIntegerpageCount =0;
? ? CGFloatsumWidth = contentViewWidth;
? ? CGFloatsumHeight = contentViewHeight;
? ? CGFloatheaderViewHight =0;
? ? for(UIView*iteminself.scrollView.subviews) {
? ? ? ? if(![itemisKindOfClass:[UIImageViewclass]]) {
? ? ? ? ? ? for(UIView*viewinitem.subviews) {
? ? ? ? ? ? ? ? if(pageCount ==0||CGRectGetMaxX(view.frame) > sumWidth ||CGRectGetMaxY(view.frame) > sumHeight) {
? ? ? ? ? ? ? ? ? ? pageCount++;
? ? ? ? ? ? ? ? ? ? if(CGRectGetMaxX(view.frame) > sumWidth) {
? ? ? ? ? ? ? ? ? ? ? ? sumWidth += contentViewWidth;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if(CGRectGetMaxY(view.frame) > sumHeight) {
? ? ? ? ? ? ? ? ? ? ? ? sumHeight += contentViewHeight;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? UIGraphicsBeginPDFPage();
? ? ? ? ? ? ? ? ? ? headerViewHight = [selfcreateHeaderViewWithWidth:pageWidthdateText:dateTextname:self.user.real_namepdmSN:pdmSNpumpBaseSN:pumpBaseSNtransmitterSN:transmitterSN];
? ? ? ? ? ? ? ? ? ? [self createFooterViewWithWidth:pageWidth height:(pageHeight - 35) name:self.myDelegate.user.real_name ? self.myDelegate.user.real_name : self.myDelegate.user.username pageNumber:(unsigned int)pageCount printTime:printTime];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
? ? ? ? ? ? ? ? [view.layer renderInContext:UIGraphicsGetCurrentContext()];
? ? ? ? ? ? ? ? UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
? ? ? ? ? ? ? ? image = [selfadjustColor:image];
? ? ? ? ? ? ? ? UIGraphicsEndImageContext();
? ? ? ? ? ? ? ? [imagedrawInRect:CGRectMake(CGRectGetMinX(view.frame) - sumWidth + contentViewWidth,CGRectGetMinY(view.frame) + headerViewHight - sumHeight + contentViewHeight,CGRectGetWidth(view.frame),CGRectGetHeight(view.frame))];
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? UIGraphicsEndPDFContext();
? ??returnstorePath;
}
2.把tableView上面的內(nèi)容生成PDF文件殊轴,代碼如下:
#pragma mark- Create PDF
- (NSString *)createPDF {
? ? NSDate*printTime = [NSDatedate];
? ? NSString*fileName = [NSStringstringWithFormat:@"%@_%@_%@.pdf",self.user.real_name,self.title, [self.dateHourMinuteFormatterstringFromDate:printTime]];
? ? NSURL*storeURL = [selfbuildStoreURLForFileName:fileName];
? ? NSString*storePath = storeURL.path;
? ? CGFloatlayoutConstraintConstant =self.tableViewHeightConstraint.constant;
? ? self.tableViewHeightConstraint.constant=self.tableView.contentSize.height-CGRectGetHeight(self.tableView.frame) + layoutConstraintConstant;
? ? [self.tableView reloadData];
? ? CGFloatpageWidth =self.tableView.contentSize.width;
//? ? CGFloat pageHeight = self.tableView.contentSize.height + 143.f;
? ? CGFloatpageHeight =self.tableView.sectionHeaderHeight+31*self.tableView.rowHeight+143.f;
? ? UIGraphicsBeginPDFContextToFile(storePath,CGRectMake(0,0, pageWidth, pageHeight),nil);
? ? NSString*dateText = [NSStringstringWithFormat:@"%@ (%d%@)",self.dataSelectView.label.text, (int)self.daysCount, (self.daysCount>1) ?MyLocalizedString(@"unit.days",nil) :MyLocalizedString(@"unit.day",nil)];
? ? NSDictionary*pdmSettings = (NSDictionary*)self.user.settings.pdmSettings;
? ? NSString*pdmSN =@"";
? ? if([pdmSettingsobjectForKey:@"PDMSN"]) {
? ? ? ? pdmSN = [AppDelegatetransformSerialString:[pdmSettingsobjectForKey:@"PDMSN"]];
? ? }
? ? NSString*pumpBaseSN =@"";
? ? if([pdmSettingsobjectForKey:@"PumpBaseSN"]) {
? ? ? ? pumpBaseSN = [AppDelegatetransformSerialString:[pdmSettingsobjectForKey:@"PumpBaseSN"]];
? ? }
? ? NSString*transmitterSN =@"";
? ? if([pdmSettingsobjectForKey:@"TransmitterSN"]) {
? ? ? ? transmitterSN = [AppDelegatetransformSerialString:[pdmSettingsobjectForKey:@"TransmitterSN"]];
? ? }
? ? NSUIntegerpageCount =0;
? ? CGFloatcontentHeight =0;
? ? CGFloatoffsetY =0;
? ? do{
? ? ? ? pageCount ++;
? ? ? ? UIGraphicsBeginPDFPage();
? ? ? ? CGFloatheaderViewHight = [selfcreateHeaderViewWithWidth:pageWidthdateText:dateTextname:self.user.real_namepdmSN:pdmSNpumpBaseSN:pumpBaseSNtransmitterSN:transmitterSN];
? ? ? ? [self createFooterViewWithWidth:pageWidth height:(pageHeight - 35) name:self.myDelegate.user.real_name ? self.myDelegate.user.real_name : self.myDelegate.user.username pageNumber:(unsigned int)pageCount printTime:printTime];
? ? ? ? UIGraphicsBeginImageContextWithOptions(self.tableView.contentSize, NO, 0.0);
? ? ? ? [self.tableView.layer renderInContext:UIGraphicsGetCurrentContext()];
? ? ? ? UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
? ? ? ? CGFloatscale = [UIScreenmainScreen].scale;
? ? ? ? offsetY += contentHeight;
? ? ? ? if(pageCount ==1) {
? ? ? ? ? ? contentHeight =self.tableView.sectionHeaderHeight+31*self.tableView.rowHeight;
? ? ? ? }elseif(pageCount ==2) {
? ? ? ? ? ? contentHeight =32*self.tableView.rowHeight;
? ? ? ? }elseif(pageCount ==3) {
? ? ? ? ? ? contentHeight =30*self.tableView.rowHeight;
? ? ? ? }
? ? ? ? CGImageRefimageRef =CGImageCreateWithImageInRect(image.CGImage,CGRectMake(0, offsetY * scale, image.size.width* scale, contentHeight * scale));
? ? ? ? image = [UIImageimageWithCGImage:imageRef];
? ? ? ? CGImageRelease(imageRef);
? ? ? ? UIGraphicsEndImageContext();
? ? ? ? [imagedrawInRect:CGRectMake(CGRectGetMinX(self.tableView.frame), headerViewHight, image.size.width/ scale, image.size.height/ scale)];
? ? }while(pageCount *30<self.daysCount);
? ? UIGraphicsEndPDFContext();
? ? self.tableViewHeightConstraint.constant= layoutConstraintConstant;
? ? returnstorePath;
}
2.把UIView上面的內(nèi)容生成PDF文件指厌,代碼如下:
#pragma mark- Create PDF
- (NSString *)createPDF {
? ? if (self.contentView.subviews.lastObject == nil || self.contentView.subviews.lastObject.subviews.count == 0) {
? ? ? ? returnnil;
? ? }
? ? if (![self.contentView.subviews.lastObject.subviews.lastObject isKindOfClass:[UIScrollView class]]) {
? ? ? ? returnnil;
? ? }
? ? NSDate*printTime = [NSDatedate];
? ? NSString*fileName = [NSStringstringWithFormat:@"%@_%@_%@.pdf",self.patientRealName,self.title, [self.dateHourMinuteFormatterstringFromDate:printTime]];
? ? NSURL*storeURL = [selfbuildStoreURLForFileName:fileName];
? ? NSString*storePath = storeURL.path;
? ? NSDictionary*pdmSettings =self.pdmSettings;
? ? NSString*createdTimeStr =@"";
? ? if([pdmSettingsobjectForKey:@"CreatedTime"]) {
? ? ? ? createdTimeStr = [self.dateTimeFormatterstringFromDate:[pdmSettingsobjectForKey:@"CreatedTime"]];
? ? }
? ? NSString*pdmSN =@"";
? ? if([pdmSettingsobjectForKey:@"PDMSN"]) {
? ? ? ? pdmSN = [AppDelegatetransformSerialString:[pdmSettingsobjectForKey:@"PDMSN"]];
? ? }
? ? NSString*pumpBaseSN =@"";
? ? if([pdmSettingsobjectForKey:@"PumpBaseSN"]) {
? ? ? ? pumpBaseSN = [AppDelegatetransformSerialString:[pdmSettingsobjectForKey:@"PumpBaseSN"]];
? ? }
? ? NSString*transmitterSN =@"";
? ? if([pdmSettingsobjectForKey:@"TransmitterSN"]) {
? ? ? ? transmitterSN = [AppDelegatetransformSerialString:[pdmSettingsobjectForKey:@"TransmitterSN"]];
? ? }
? ? [self hideContentController:self.currentViewController];
? ? UIGraphicsBeginPDFContextToFile(storePath, CGRectZero, nil);
? ? NSUIntegerpageCount =0;
? ? for(inti =0; i <4; i++) {
? ? ? ? pageCount ++;
? ? ? ? UIViewController*printViewController;
? ? ? ? switch(i) {
? ? ? ? ? ? case1:
? ? ? ? ? ? ? ? printViewController =self.bolusSettingsViewController;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case2:
? ? ? ? ? ? ? ? printViewController =self.sensorSettingsViewController;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case3:
? ? ? ? ? ? ? ? printViewController =self.utilitiesSettingsViewController;
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? printViewController =self.basalSettingsViewController;
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? ? ? [selfdisplayContentController:printViewController];
? ? ? ? [printViewController.viewlayoutIfNeeded];
? ? ? ? UIScrollView *scrollView = self.contentView.subviews.lastObject.subviews.lastObject;
? ? ? ? CGFloatpageWidth = scrollView.contentSize.width;
? ? ? ? CGFloatpageHeight = scrollView.contentSize.height+143.f;
? ? ? ? UIGraphicsBeginPDFPageWithInfo(CGRectMake(0,0, pageWidth, pageHeight),nil);
? ? ? ? NSString*dateText = [NSStringstringWithFormat:@"%@ %@", [self.segmentedtitleForSegmentAtIndex:i], createdTimeStr];
? ? ? ? CGFloatheaderViewHight = [selfcreateHeaderViewWithWidth:pageWidthdateText:dateTextname:self.patientRealNamepdmSN:pdmSNpumpBaseSN:pumpBaseSNtransmitterSN:transmitterSN];
? ? ? ? [self createFooterViewWithWidth:pageWidth height:(pageHeight - 35) name:self.myDelegate.user.real_name ? self.myDelegate.user.real_name : self.myDelegate.user.username pageNumber:(unsigned int)pageCount printTime:printTime];
? ? ? ? for(UIView*viewinscrollView.subviews) {
? ? ? ? ? ? if(![viewisKindOfClass:[UIImageViewclass]]) {
? ? ? ? ? ? ? ? UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
? ? ? ? ? ? ? ? [view.layer renderInContext:UIGraphicsGetCurrentContext()];
? ? ? ? ? ? ? ? UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
? ? ? ? ? ? ? ? if(![viewisKindOfClass:[UILabelclass]] && ![viewisKindOfClass:[UITableViewclass]]) {
? ? ? ? ? ? ? ? ? ? image = [selfadjustColor:image];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? UIGraphicsEndImageContext();
? ? ? ? ? ? ? ? [imagedrawInRect:CGRectMake(CGRectGetMinX(view.frame), CGRectGetMinY(view.frame) + headerViewHight, CGRectGetWidth(view.frame), CGRectGetHeight(view.frame))];
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? [selfhideContentController:printViewController];
? ? }
? ? UIGraphicsEndPDFContext();
? ? [self displayContentController:self.currentViewController];
? ? returnstorePath;
}