2017-04-17 3 views
1

私は、アプリケーションのUIWebViewからibookにpdfを保存したいので、UIWebViewのhtmlは複数ページにすることができます。
私は目的のcのuiwebviewから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使用してみてください:のUIWebViewは、複数のページが含まれている、といくつかの時間の出力フォーマットはうまくPDFで印刷でない場合

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では、ちょうど1ページを作成

ありがとうございます。

答えて

0

文書が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"); 
    } 
});} 
でロードが終了した後、法の下に呼び出し、その後、すべての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 

の第

#define kPaperSizeA4 CGSizeMake(595.2,841.8) 

のUIWebViewにロードされたすべてのMicrosoftの文書からPDFを作成します。

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

行をコメントアウトしてコードを実行してみてください。 –

関連する問題