【发布时间】:2014-12-04 01:07:45
【问题描述】:
如何将 UIWebView 内容创建为 pdf 文件..? 我点击了这个链接,但是这个链接没有创建整个 webview 内容(当我滚动时,webview 上仍然有一些数据)它只是创建一个 pdf 屏幕显示的内容..
How to Convert UIView to PDF within iOS?
任何帮助
【问题讨论】:
标签: ios iphone uiwebview pdf-generation
如何将 UIWebView 内容创建为 pdf 文件..? 我点击了这个链接,但是这个链接没有创建整个 webview 内容(当我滚动时,webview 上仍然有一些数据)它只是创建一个 pdf 屏幕显示的内容..
How to Convert UIView to PDF within iOS?
任何帮助
【问题讨论】:
标签: ios iphone uiwebview pdf-generation
使用UIWebView 中的UIPrintPageRenderer 按照以下步骤操作:
Category 的UIPrintPageRenderer 以获取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)
UIWebView's webViewDidFinishLoad delegate 使用UIPrintPageRenderer property 的UIWebView。- (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");
}
}
【讨论】:
你是对的,你需要一个 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];
}
【讨论】: