iOS PDF文件生成

1.把scrollView上面的内容生成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上面的内容生成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上面的内容生成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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值