Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将webview内容创建为pdf文件ios sdk_Ios_Iphone_Uiwebview_Pdf Generation - Fatal编程技术网

如何将webview内容创建为pdf文件ios sdk

如何将webview内容创建为pdf文件ios sdk,ios,iphone,uiwebview,pdf-generation,Ios,Iphone,Uiwebview,Pdf Generation,如何将UIWebView内容创建为pdf文件。。? 我遵循了这个链接,但这个链接并没有创建整个webview内容(当我滚动时,webview上仍有一些数据),它只是创建了一个屏幕上显示的pdf 任何帮助你是对的,你需要一个网络视图 如果捆绑包中有.pdf文件,只需执行以下操作: @implementation{ UIWebView *documentWebView; } - (void)viewDidLoad{ //Or any other method that you

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


任何帮助

你是对的,你需要一个网络视图

如果捆绑包中有.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];

}

使用
UIPrintPageRenderer
UIWebView
执行以下步骤:

添加
UIPrintPageRenderer
类别
,以获取PDF数据 现在在
UIWebView的
webViewDidFinishLoad
delegate
中,使用
UIWebView
UIPrintPageRenderer
属性。

我很抱歉zil我刚刚编辑了我的问题,你能看看新编辑吗..哦,我一点都不懂:D好的,对不起!我不知道如何将视图保存为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
#define kPaperSizeA4 CGSizeMake(595.2,841.8)
- (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");
  }
}