Ios 将UIWebView转换为PDF/图像仅提供一英寸宽的输出

Ios 将UIWebView转换为PDF/图像仅提供一英寸宽的输出,ios,objective-c,pdf,uiwebview,uigraphicscontext,Ios,Objective C,Pdf,Uiwebview,Uigraphicscontext,我正在使用以下代码将UIWebView转换为pdf/image: NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"]; int height = [heightStr intValue]; CGFloat screenHeight = webView.bounds.size.height; int pages =

我正在使用以下代码将UIWebView转换为pdf/image:

    NSString *heightStr = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];

    int height = [heightStr intValue];
    CGFloat screenHeight = webView.bounds.size.height;
    int pages = ceil(height / screenHeight);

    NSMutableData *pdfData = [NSMutableData data];
    UIGraphicsBeginPDFContextToData(pdfData, webView.bounds, nil);
    CGRect frame = [webView frame];
    NSMutableArray *imageArray= [[NSMutableArray alloc]init];

    for (int i = 0; i < pages; i++)
    {
        // Check to screenHeight if page draws more than the height of the UIWebView
        if ((i+1) * screenHeight  > height)
        {
            CGRect f = [webView frame];
            f.size.height -= (((i+1) * screenHeight) - height);
            [webView setFrame: f];
        }
        UIGraphicsBeginImageContext(frame.size);

        UIGraphicsBeginPDFPage();
        CGContextRef currentContext = UIGraphicsGetCurrentContext();
        //CGContextTranslateCTM(currentContext, 72, 72); // Translate for 1" margins

        [[[webView subviews] lastObject] setContentOffset:CGPointMake(0, screenHeight * i) animated:NO];
        [webView.layer renderInContext:currentContext];


        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
        [imageArray insertObject:viewImage atIndex:imageArray.count] ;

        UIGraphicsEndImageContext();
    }

    UIGraphicsEndPDFContext();

    [webView setFrame:frame];

    //Send all images
    NSData *imgData = [self blendImages:imageArray];

  //Final Image
  UIImage *finalImage = [UIImage imageWithData:imgData];

-(NSData*)blendImages:(NSMutableArray *)arrImage{
    // get data from document

    CGFloat height=0 ,width=0  ;
    UIImage *tempImg = [arrImage objectAtIndex:0] ;
    width =tempImg.size.width ;

    for (int i = 0 ; i<arrImage.count ; i++){
        UIImage *img=  [arrImage objectAtIndex:i];
        height = img.size.height +height;
    }
    CGSize size = CGSizeMake(width, height) ;
    UIGraphicsBeginImageContext(size);
    CGFloat h = 0;
    for (int i=0; i<arrImage.count; i++) {
        UIImage* uiimage = [arrImage objectAtIndex:i];
        [uiimage drawAtPoint:CGPointMake(0, h) blendMode:kCGBlendModeNormal alpha:1.0];
        h = uiimage.size.height +h ;
    }
    NSData *imageData = UIImagePNGRepresentation(UIGraphicsGetImageFromCurrentImageContext());
    UIGraphicsEndImageContext();

    return imageData;
}
NSString*heightStr=[webView stringByEvaluatingJavaScriptFromString:@“document.body.scrollHeight;”;
int height=[heightStr intValue];
CGFloat screenHeight=webView.bounds.size.height;
int pages=ceil(高度/屏幕高度);
NSMutableData*pdfData=[NSMutableData];
UIGraphicsBeginPDFContextToData(pdfData,webView.bounds,nil);
CGRect帧=[webView帧];
NSMutableArray*imageArray=[[NSMutableArray alloc]init];
对于(int i=0;i高度)
{
CGRect f=[webView框架];
f、 尺寸.高度-=((i+1)*屏幕高度)-高度);
[webView设置框架:f];
}
UIGraphicsBeginImageContext(frame.size);
UIGraphicsBeginPDFPage();
CGContextRef currentContext=UIGraphicsGetCurrentContext();
//CgContextTranslateCm(currentContext,72,72);//翻译为1“边距
[[[webView子视图]lastObject]setContentOffset:CGPointMake(0,屏幕高度*i)动画:否];
[webView.layer renderInContext:currentContext];
UIImage*viewImage=UIGraphicsGetImageFromCurrentImageContext();
[imageArray insertObject:viewImage atIndex:imageArray.count];
UIGraphicsSendImageContext();
}
UIGraphicsSendPdfContext();
[webView设置框架:框架];
//发送所有图像
NSData*imgData=[自混合图像:imageArray];
//最终图像
UIImage*finalImage=[UIImage-imageWithData:imgData];
-(NSData*)混合图像:(NSMUTABLEARRY*)arrImage{
//从文档中获取数据
CGFloat高度=0,宽度=0;
UIImage*tempImg=[arrImage对象索引:0];
宽度=tempImg.size.width;

对于(int i=0;i这将创建网页截图的PDF格式-可能有助于您开始:)


我对你的代码玩了一些。我不清楚你想实现什么。你想创建多页的pdf,每页大小为A4?还是像你的代码当前的行为一样,试图从整个网页创建单个图像,但更改结果大小?@BorisVerebsky:我想创建多页。
  - (IBAction)makePDF:(id)sender
  {
      UIGraphicsBeginImageContextWithOptions(webView.scrollView.contentSize, webView.scrollView.opaque, 0.0);

      webView.scrollView.contentOffset = CGPointZero;
      webView.scrollView.frame = CGRectMake(0, 0, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height);
      [webView.scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
      UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();

      CGRect imageFrame = CGRectMake(0, 0, image.size.width, image.size.height);

      NSMutableData *pdfData = [NSMutableData data];
      UIGraphicsBeginPDFContextToData(pdfData, imageFrame, nil);

      //full screenshot
      UIGraphicsBeginPDFPage();
      UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
      [imageView setContentMode:UIViewContentModeScaleAspectFit];
      [imageView setImage:image];
      [imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
      UIGraphicsEndPDFContext();

      NSArray *documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
      NSString *documentDirectory = [documentDirectories objectAtIndex:0];
      NSString *fileName = [NSString stringWithFormat:@"%@.pdf",[NSDate date]];
      NSString *path = [documentDirectory stringByAppendingPathComponent:fileName];

      // save to disk
      [pdfData writeToFile:path atomically:YES];
      NSLog(@"pdf in %@", path);
  }