【问题标题】:how to save pdf from uiwebview in objective c如何在目标c中从uiwebview保存pdf
【发布时间】:2017-09-12 18:09:48
【问题描述】:

我想在应用程序中将 pdf 从 UIWebView 保存到 ibook,UIWebView 中的 html 可以是多页的。
我尝试使用

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");

}

导出HTMLContentToPDF:

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 中

提前致谢。

【问题讨论】:

    标签: ios objective-c pdf uiwebview ibooks


    【解决方案1】:

    从 UIWebview 中加载的任何 Microsoft 文档创建 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。

    【讨论】:

    • 应用挂在这一行 "[self prepareForDrawingPages: NSMakeRange(0, self.numberOfPages)];"并且在 xcode 中没有错误或崩溃显示,只是挂起
    【解决方案2】:
    #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");
    } });}
    

    参考:https://medium.com/@javedmultani16/create-pdf-from-any-microsoft-document-loaded-in-uiwebview-in-ios-15229671060b

    【讨论】:

      【解决方案3】:

      【讨论】:

        猜你喜欢
        • 2015-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-16
        • 1970-01-01
        相关资源
        最近更新 更多