iOS PDF文件生成

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;

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末窗宦,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖究西,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異勘天,居然都是意外死亡怔揩,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門脯丝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來商膊,“玉大人,你說我怎么就攤上這事宠进≡尾穑” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵材蹬,是天一觀的道長实幕。 經(jīng)常有香客問我,道長堤器,這世上最難降的妖魔是什么昆庇? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮闸溃,結(jié)果婚禮上整吆,老公的妹妹穿的比我還像新娘。我一直安慰自己辉川,他們只是感情好表蝙,可當(dāng)我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著乓旗,像睡著了一般府蛇。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上屿愚,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天汇跨,我揣著相機(jī)與錄音务荆,去河邊找鬼。 笑死扰法,一個胖子當(dāng)著我的面吹牛蛹含,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播塞颁,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼浦箱,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了祠锣?” 一聲冷哼從身側(cè)響起酷窥,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎伴网,沒想到半個月后蓬推,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡澡腾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年沸伏,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片动分。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡毅糟,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出澜公,到底是詐尸還是另有隱情姆另,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布坟乾,位于F島的核電站迹辐,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏甚侣。R本人自食惡果不足惜明吩,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望殷费。 院中可真熱鬧印荔,春花似錦、人聲如沸宗兼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽殷绍。三九已至,卻和暖如春鹊漠,著一層夾襖步出監(jiān)牢的瞬間主到,已是汗流浹背茶行。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留登钥,地道東北人畔师。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像牧牢,于是被迫代替她去往敵國和親看锉。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容