2017-04-17 26 views
1

我想在应用程序中将pdf保存为UIWebView中的ibook,UIWebView中的html可以是多页。
我尝试使用如何从uiwebview中保存目标c中的pdf

NSString *page = [self.lowWebview stringByEvaluatingJavaScriptFromString:@"document.body.outerHTML"]; 
NSString *path = [self exportHTMLContentToPDF:page]; 

NSURL *targetURL = [NSURL fileURLWithPath:path]; 

self.docController = [UIDocumentInteractionController interactionControllerWithURL:targetURL]; 

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"itms-books:"]]) 
{ 
    [self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]; 
    NSLog(@"iBooks installed"); 

} else { 

    NSLog(@"iBooks not installed"); 

} 

exportHTMLContentToPDF:

CustomPrintPageRenderer *printPageRenderer = [[CustomPrintPageRenderer alloc] init]; 

UIMarkupTextPrintFormatter *printFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:HTMLContent]; 

[printPageRenderer addPrintFormatter:printFormatter startingAtPageAtIndex:0];//.addPrintFormatter(printFormatter, startingAtPageAtIndex: 0) 

NSData *pdfData = [self drawPDFUsingPrintPageRenderer:printPageRenderer]; 

NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
NSString *fileName = [docDirPath stringByAppendingPathComponent:@"ticket.pdf"]; 

[pdfData writeToFile:fileName atomically:true];//.writeToFile(fileName, atomically: true) 

NSLog(@"%@", fileName); 
return fileName; 

问题是exportHTMLContentToPDF只创建一个页面,如果UIWebView中包含多个页面,并在一段时间的输出格式不能很好地在PDF

打印

在此先感谢。

回答

0

从UIWebView中加载的任何微软文档创建PDF

#define kPaperSizeA4 CGSizeMake(595.2,841.8) 

首先实现UIPrintPageRenderer协议

@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 

然后,拨打以下方法文档完成了UIWebView

-(void)createPDF:(UIWebView *)webView { 

UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init]; 
[render addPrintFormatter:webView.viewPrintFormatter startingAtPageAtIndex:0]; 

float padding = 10.0f; 
CGRect paperRect = CGRectMake(0, 0, kPaperSizeA4.width, kPaperSizeA4.height); 
CGRect printableRect = CGRectMake(padding, padding, kPaperSizeA4.width-(padding * 2), kPaperSizeA4.height-(padding * 2)); 

[render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"]; 
[render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"]; 

NSData *pdfData = [render printToPDF]; 

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    if (pdfData) { 
     [pdfData writeToFile:directoryPath atomically: YES]; 
    } 
    else 
    { 
     NSLog(@"PDF couldnot be created"); 
    } 
});} 
样之后,

摘自PDF Creation in iOS - Create PDF from any Microsoft Document loaded in UIWebview。原作者为Mansi Panchal。您可以在contributor page上找到署名详情。信息来源根据CC BY-SA 3.0获得许可,可在Documentation archive中找到。参考主题ID:2416和实例ID:28437.

+0

应用程序在此行中挂“[自我prepareForDrawingPages:NSMakeRange(0,self.numberOfPages) ];”并没有错误或崩溃在xcode显示,只是挂 –

+0

尝试注释掉该行并运行代码。 –

相关问题