【问题标题】:how to create webview content as pdf file ios sdk [duplicate]如何将webview内容创建为pdf文件ios sdk [重复]
【发布时间】:2014-12-04 01:07:45
【问题描述】:

如何将 UIWebView 内容创建为 pdf 文件..? 我点击了这个链接,但是这个链接没有创建整个 webview 内容(当我滚动时,webview 上仍然有一些数据)它只是创建一个 pdf 屏幕显示的内容..

How to Convert UIView to PDF within iOS?

任何帮助

【问题讨论】:

    标签: ios iphone uiwebview pdf-generation


    【解决方案1】:

    使用UIWebView 中的UIPrintPageRenderer 按照以下步骤操作:

    添加CategoryUIPrintPageRenderer 以获取PDF 数据

    @interface UIPrintPageRenderer (PDF)
    - (NSData*) printToPDF;
    @end
    
    @implementation UIPrintPageRenderer (PDF)
    - (NSData*) printToPDF
    {
      NSMutableData *pdfData = [NSMutableData data];
      UIGraphicsBeginPDFContextToData( pdfData, self.paperRect, nil );
      [self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)];
      CGRect bounds = UIGraphicsGetPDFContextBounds();
      for ( int i = 0 ; i < self.numberOfPages ; i++ )
      {
        UIGraphicsBeginPDFPage();
        [self drawPageAtIndex: i inRect: bounds];
      }
      UIGraphicsEndPDFContext();
      return pdfData;
    }
    @end
    

    为 A4 尺寸添加这些定义

    #define kPaperSizeA4 CGSizeMake(595.2,841.8)
    

    现在在UIWebView's webViewDidFinishLoad delegate 使用UIPrintPageRenderer propertyUIWebView

    - (void)webViewDidFinishLoad:(UIWebView *)awebView
    {
      if (awebView.isLoading)
        return;
    
      UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
      [render addPrintFormatter:awebView.viewPrintFormatter startingAtPageAtIndex:0];
      //increase these values according to your requirement
      float topPadding = 10.0f;
      float bottomPadding = 10.0f;
      float leftPadding = 10.0f;
      float rightPadding = 10.0f;
      CGRect printableRect = CGRectMake(leftPadding,
                                      topPadding,
                                      kPaperSizeA4.width-leftPadding-rightPadding,
                                      kPaperSizeA4.height-topPadding-bottomPadding);
      CGRect paperRect = CGRectMake(0, 0, kPaperSizeA4.width, kPaperSizeA4.height);
      [render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
      [render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];
      NSData *pdfData = [render printToPDF];
      if (pdfData) {
        [pdfData writeToFile:[NSString stringWithFormat:@"%@/tmp.pdf",NSTemporaryDirectory()] atomically: YES];
      }
      else
      {
        NSLog(@"PDF couldnot be created");
      }
    }
    

    【讨论】:

      【解决方案2】:

      你是对的,你需要一个 Webview。

      如果您在捆绑包中有 .pdf 文件,只需执行以下操作:

      @implementation{
           UIWebView *documentWebView;
      }
      
      - (void)viewDidLoad{     //Or any other method that you want to create the webview with.
      
          //Getting the full screen size
          UIWindow* currentWindow = [UIApplication sharedApplication].keyWindow;
          CGRect fullScreenRect = currentWindow.bounds;
      
          //Getting the pdf from the bundle ; change filepath with URL if its from the internet
          filePath = [[NSBundle mainBundle] pathForResource:@"YourPDFFileName" ofType:@"pdf"];
          NSURL *fileURL = [NSURL fileURLWithPath:filePath];
          NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
      
          //Creating the view programatically, because I'm like that.
          documentWebView = [[UIWebView alloc] initWithFrame:CGRectMake(fullScreenRect.origin.x, fullScreenRect.origin.y, fullScreenRect.size.width, fullScreenRect.size.height)];
          documentWebView.scalesPageToFit = YES;
      
          //Setting a back button to dismiss the view.
          UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(fullScreenRect.size.width-115, 10, 100, 30)];
          backButton.backgroundColor = [UIColor grayColor];
          backButton.alpha = 0.8;
          [backButton setTitle:@"Back" forState:UIControlStateNormal];
      
          //Linking the action & the button
          [backButton addTarget:self action:@selector(closeWebView:) forControlEvents:UIControlEventTouchUpInside];
      
          //Loading the pdf into the actual view
          [documentWebView loadRequest:request];
      
          //Adding the views to the main view
          [currentWindow addSubview:documentWebView];
          [documentWebView addSubview:backButton];
      }
      
      //Dismissing the view
      - (IBAction)closeWebView:(id)sender{
          [documentWebView removeFromSuperview];
          [documentWebView release];
      
      }
      

      【讨论】:

      • 对不起,我刚刚编辑了我的问题,你能看看新的编辑吗..
      • 哦,我完全不明白:D 好的,抱歉!我不知道如何将视图保存到 pdf 中:≤
      猜你喜欢
      • 2013-07-28
      • 2015-08-08
      • 2018-07-08
      • 2011-04-01
      • 2020-05-27
      • 2011-07-07
      • 2018-07-30
      • 2014-03-18
      • 1970-01-01
      相关资源
      最近更新 更多