前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WKWebView接入PDF.js过程记录处理总结

WKWebView接入PDF.js过程记录处理总结

作者头像
freesan44
发布2023-02-14 13:51:17
2.7K0
发布2023-02-14 13:51:17
举报
文章被收录于专栏:freesan44freesan44

问题

最近用WKWebView读取PDF文件出现字体异常、电子图章不显示的问题,后来查找很多解决方案,最后决定用PDF.js的方式来实现

解决方案

  1. 参考https://www.jianshu.com/p/ded81b392d4d 写了demo能接入PDF,但部分字体在真机上还是接入异常,后来使用
代码语言:javascript
复制
gulp generic-legacy

生成generic-legacy稳定包之后,对Safari进行兼容后,终于能修复字体异常的问题

  1. PDF.js自带顶部工具类功能,如果想要去掉,只能通过修改viewer.css来实现,添加如下代码
代码语言:javascript
复制
div.toolbar {
  display: none;
}
#outerContainer #mainContainer div.toolbar {
    display: none !important; /* hide PDF viewer toolbar */
    opacity: 0.5 !important;
}
#outerContainer #mainContainer #viewerContainer {
    top: 0 !important; /* move doc up into empty bar space */
}
  1. 读取本地PDF文件的方式有两个,一个是初始化接入参数,一个是通过bytes方式动态加载读取 初始化接入参数:
代码语言:javascript
复制
    NSString *viwerPath = [[NSBundle mainBundle] pathForResource:@"viewer" ofType:@"html" inDirectory:@"generic/web"];
    NSString *urlStr = [NSString stringWithFormat:@"file://%@?file=%@#page=1",viwerPath,filePath];
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [self loadRequest:request];

bytes方式动态加载读取:

代码语言:javascript
复制
SBundle mainBundle] pathForResource:@"viewer" ofType:@"html" inDirectory:@"generic/web"];
    NSURL * viwerPathURL = [NSURL fileURLWithPath:viwerPath];
    NSURL * dir = viwerPathURL.URLByDeletingLastPathComponent;
    [self loadFileURL:viwerPathURL allowingReadAccessToURL:dir];
    
    //动态加载的写法
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSData *data = [NSData dataWithContentsOfURL:filePath];
        NSUInteger len = [data length];
        uint8_t myArray[len];
        [data getBytes:&myArray length:len];

        NSMutableArray<NSString *> *bytes = [NSMutableArray array];
        const uint8_t *rawBytes = data.bytes;
        for (NSUInteger i = 0; i < data.length; i++) {
            [bytes addObject:[NSString stringWithFormat:@"%d", (int)rawBytes[i]]];
        }

        NSString *javaScriptArray = [bytes componentsJoinedByString:@","];

        NSString *strForEvaluate = [NSString stringWithFormat:
                                    @"PDFViewerApplication.open(new Uint8Array([%@]));", javaScriptArray];

        [self evaluateJavaScript:strForEvaluate completionHandler:^(id Result, NSError * _Nullable error) {
            if (error)
            {
                NSLog(@"This is error....%@",error.description);
            }
            else if(Result)
            {
                NSLog(@"+++%@",Result);
            }
        }];

    });

demo地址:https://github.com/freesan44/PDFJSReader

参考: https://github.com/mozilla/pdf.js https://www.jianshu.com/p/fd5f248a8158 https://www.jianshu.com/p/ded81b392d4d https://github.com/mozilla/pdf.js/issues/2784

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2023-01-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题
  • 解决方案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档