iOS之PDF相关实现

PDF的阅读功能几种方法实现:https://www.jianshu.com/p/93ec03564b4c

 

https://www.jianshu.com/p/ba08f8832528

集成第三方SDK

  • 福昕Foxit PDF SD 国内付费SDK,可对PDF做标注等更多操作功能;
  • PlugPDF 国外付费SDK,可对PDF做标注等更多操作功能;
  • PSPDFKit 国外付费SDK,可对PDF做标注等更多操作功能;印象笔记采用的SDK;

GitHub开源资源

  • ILPDFKit:采用CoreGraphics框架,通过处理PDF数据流,识别PDF表单form格式,高亮并可以编辑;
  • PDFKitten: 采用CoreGraphics框架,通过处理PDF数据流,可进行文本搜索并高亮,但并不适用于所有PDF;
  • vfr/Reade: 采用CoreGraphics框架绘制功能,只能提供PDF阅读;
  • pdf2htmlEX: 提供服务器端pdf转html功能;
  • pdf.js:火狐浏览器PDF开源框架,采用webView,浏览pdf时转为html,可同时在iOS和安卓客户端进行处理,https://github.com/mozilla/pdf.js

****PDF转成word:

PDF转换成Word的原理是:先将PDF文档中的文档元素提取出来,然后再将这些文档元素一个一个的复制到新生成的Word文档中,并将原PDF文档中的排版信息也引用到Word文档中。这样,PDF文档中的文字,图片,表格,注释等等文档元素就能转换成Word文档中相对应的元素。

****word转成PDF:

****PDF转成HTML:

mozilla/pdf.js-------https://github.com/mozilla/pdf.js

****HTML转成PDF:

思路:https://www.jianshu.com/p/2775404d011f?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

iOS-htmltopdf-------https://github.com/iclems/iOS-htmltopdf

****图片转PDF:

Quartz 2D是Core Grapgics下的2D绘图引擎,适用于iOS和Mac OS X。 它具有强大的绘图功能,提供视角效果渲染,反锯齿渲染,颜色管理,绘制PDF等等。

开发者无需考虑不同设备不同分别率的显示效果,因为这一切适配操作Quartz 全都自己包了。

****wkwebview加载pdf

WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds];

        [self.view addSubview:webView];

    NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:@"fp.PDF" withExtension:nil];

NSURLRequest *request = [NSURLRequest requestWithURL:pdfURL];

  [webView loadRequest:request];

***UIDocumentInteractionController实现pdf的显示

@property(nonatomic,strong) UIDocumentInteractionController *documentController;

  //获取本地的pdf文件

    NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:@"fp.PDF" withExtension:nil];

    UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:pdfURL];

    self.documentController=documentController;

    documentController.delegate = self;

  [self.documentController presentPreviewAnimated:YES]; // 预览文件,默认使用时QuickLook​​​​​

//    [self.documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES]; // 菜单操作,可以分享,用第三方的打开,或存储到别的地方

如果用  [self.documentController presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES]; 页面如下:

QuickLook可以加载视频mov、pptx、xlsx、pdf、doc都可以加载。

//必须要实现的代理方法  UIDocumentInteractionControllerDelegate

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller

{

    NSLog(@"1 %s", __func__);

    return self;

}

***QLPreviewController加载pdf文件;       

引入#import <QuickLook/QuickLook.h>

数据源和代理QLPreviewControllerDelegate,QLPreviewControllerDataSource

@property(nonatomic,strong)QLPreviewController *previewController;

_previewController = [[QLPreviewController alloc] init];

           _previewController.delegate = self;

           _previewController.dataSource = self;

  [self presentViewController:_previewController animated:YES completion:nil];

实现协议

// 返回文件数量

- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {

    return 1;

}

- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {

    NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"fp" ofType:@"PDF"];

    return [NSURL fileURLWithPath:pdfPath];

}

***从服务器下载pdf后查看

//下载
-(void)videonormaldownloadWithUrl:(NSString *)url
                      params:(NSDictionary *)params
                successBlock:(SuccessBlock)successBlock
                failureBlock:(FailureBlock)failureBlock{
    AFHTTPSessionManager *manager = [QMWNHttpM sharehttpsessionmanager];
   
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
/*第一个参数:请求对象
     第二个参数:进度回调 downloadProgress(获得进度信息)
     第三个参数:destination
                targetPath:文件的临时存储路径
                response:响应头信息
                返回值:NSURL(AFN内部已经实现了文件剪切的过程,但是需要我们告诉他应该把文件剪切到哪里)
     第四个参数:completionHandler 请求完成的时候调用
                response:响应头信息
                filePath==fullPath 文件的最终目录
                error:错误信息
*/
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
//进度
            SLog(@"%f",1.0 *downloadProgress.completedUnitCount /downloadProgress.totalUnitCount);

        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
       //targetpath是临时存储目录,需要保存到其他地方
            NSString *fullPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];

            NSFileManager *fileManger = [NSFileManager defaultManager];
             //视频存放到这个位置
             [fileManger moveItemAtURL:targetPath  toURL:[NSURL fileURLWithPath:fullPath] error:nil];
            
           //把路径回调出去
            if(targetPath){
                if(successBlock){
                    successBlock([[NSURLSessionDataTask alloc]init],fullPath);
                }
            }

            SLog(@"视频下载---%@---\n%@---%@---%@",targetPath, fullPath,response,response.suggestedFilename);

            //NSLog(@"%@--%@",[NSURL URLWithString:fullPath],[NSURL fileURLWithPath:fullPath]);
            return [NSURL fileURLWithPath:fullPath];

        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

            SLog(@"下载完成");
        }];

        //4.执行方法
        [downloadTask resume];
}


//下载pdf
    
    NSString *docname= @“”;
    NSString *url=@“”;
    NSDictionary *paramas=@{};
    [[QMWNHttpRequestManager sharedManager]videonormaldownloadWithUrl:url params:paramas successBlock:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {

        //从网络下载文件后,要处理一下,要不然没办法查看
        NSString *path=responseObject;
        NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
        NSError *error;
        [fileCoordinator coordinateReadingItemAtURL:[NSURL URLWithString:path] options:0 error:&error byAccessor:^(NSURL *newURL) {
            
            NSData *fileData = [NSData dataWithContentsOfURL:newURL];//文档数据 fileData.length文档数据长度
            NSString *fileName = [newURL lastPathComponent];//文档名
            NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentPath = [arr lastObject];
            NSString *desFileName = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@%@",fileName,@".",docname]];
            [fileData writeToFile:desFileName atomically:YES];
            SLog(@"文档格式---%@",desFileName);
            //从沙盒取
            NSURL *pdfURL= [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:fileName];
            weakSelf.pathurl=pdfURL;
        }];
       
        SLog(@"路径---%@",path);
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf initviews];
        });
       
        } failureBlock:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
            
        }];

***wx或者qq中的pdf、word等保存到手机的文件中步骤:

在wx中打开pdf,然后弹出quicklook,选择“存储到文件”,弹出iCloud云盘和我的iphone,选择一个文件夹,点击右上角“添加”即可;手机上的文件app可以添加文件夹,删除文件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值