iOS 解决展示PDF电子签章不显示问题

项目中要显示PDF协议,文件里面有电子签章,原本用的WKWebView直接把PDF文件的URL扔过去就好了,觉得很方便,后来才发现,红红的印章没出来。在网上找了很多资料,最终选择QLPreviewController,实现分为两步:

1.下载PDF文件保存在本地

2.用QLPreviewController打开

主要代码:

-(void)loadPdfResource:(NSString *)url{
    NSURL *Url =  [NSURL URLWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    pdfFilePath = [self getFullPathWithLastPathComponent:Url.lastPathComponent];
    NSFileManager *fileManager = [NSFileManager defaultManager];
   
    if(![fileManager fileExistsAtPath:pdfFilePath])
    {
//          下载pdf数据
//            [SVProgressHUD showWithStatus:@"loading..."];
        self.pdfData=[[NSMutableData alloc]init];
        NSURLSessionConfiguration *config =[NSURLSessionConfiguration defaultSessionConfiguration];
        NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
        NSURLSessionDataTask *task = [session dataTaskWithURL:Url];
        [task resume];
    }else{
        // NSLog(@"文件存在");
        [self pushQLPreviewController];
    }

}


-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler{
    //允许继续响应
    completionHandler(NSURLSessionResponseAllow);
    //获取文件的总大小
    // NSInteger totalLength = response.expectedContentLength;
}

#pragma mark  --- 接收到数据调用

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
   didReceiveData:(NSData *)data
{
    
    //将每次接受到的数据拼接起来
    [self.pdfData  appendData:data];
    //计算当前下载的长度
    //  NSInteger nowlength = self.pdfData .length;
    //  CGFloat value = nowlength1.0/self.totalLength;
}

#pragma mark ---下载完成调用


-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(nullable NSError *)error{
    NSLog(@"self.pdfData:\n%@",self.pdfData);
//    NSString  *filename =[self getFullPath];
    [self.pdfData writeToFile:pdfFilePath atomically:YES];
    //    [SVProgressHUD dismiss];
    if (self.pdfData) {
        NSLog(@"OK");
        [self showPDFWebView:pdfFilePath];
    }else{
        NSLog(@"Sorry");
    }

    
}

#pragma mark --- 返回加载文件个数
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}
#pragma mark --- 返回加载路径
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    return  [NSURL fileURLWithPath:pdfFilePath]; 
}
-(void)showPDFWebView:(NSString *)filename{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if(![fileManager fileExistsAtPath:pdfFilePath])
    {
        NSLog(@"文件不存在");
    }else{
        NSLog(@"文件存在");
        [self pushQLPreviewController];
    }
    
}

-(void)pushQLPreviewController{
    
    UINavigationController *root = (UINavigationController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
    [root pushViewController:self.QLPVC animated:YES];
    
}

- (NSString *)getFullPathWithLastPathComponent:(NSString *)lastPathComponent
{
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
   
    path = [NSString stringWithFormat:@"%@/%@",path,@"借款协议.pdf"];
    NSLog(@"filePath:%@",path);
    
   return  path;
}

欢迎指正,Demo地址gitHub

推荐阅读更多精彩内容

  • 今天周四,平凡的一天,却因为尝试了一种不同安排而变得美好。 因为下午放学后爷爷没带钥匙,儿子被送到了单位,本打算带...
    敏兄阅读 91评论 0 4
  • 亮了 西厢孤檠 瘦了 荼蘼纤颊 老了 秦淮西州 青袂云衣 断了 司马卓君 吟哦绿绮 挂上红楼 辗转床...
    溢棠阅读 100评论 0 3
  • 提交App Store失败,验证的时候出错。 An error occurred. The operation c...
    工匠良辰阅读 787评论 0 1