PDF文檔的幾種打開(kāi)方式

之前的老項(xiàng)目遇到了一個(gè)問(wèn)題:iOS9后PDF中文會(huì)顯示亂碼,而且試了各種方法都不行,還好最終找到了解決方法---MuPDF
在此總結(jié)一下解決該問(wèn)題過(guò)程中使用過(guò)的方法:

1.使用UIWebView加載

//代碼很簡(jiǎn)單
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:webView];
    
NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:path]];
[webView loadRequest:request];

2.使用QLPreviewController打開(kāi)

//初始化QLPreviewController對(duì)象
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
[self.navigationController pushViewController:pageController animated:YES];

//再實(shí)現(xiàn)QLPreviewControllerDataSource的兩個(gè)方法即可顯示
//顯示文件數(shù)量
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller;
//文件路徑URL
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index;

3.使用CGContextDrawPDFPage繪制

UIView- (void)drawRect:(CGRect)rect方法中繪制PDF內(nèi)容,代碼如下:

/*
Quartz2D              UIKit
 
        y               (0, 0)|----------x
        |                     |
        |                     |
        |                     |
        |                     |
 (0, 0) |---------x           y

*/
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //調(diào)整坐標(biāo)系
    CGContextTranslateCTM(context, 0.0, self.bounds.size.height);//先垂直下移height高度
    CGContextScaleCTM(context, 1.0, -1.0);//再垂直向上翻轉(zhuǎn)
    
    //繪制pdf內(nèi)容
    CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdfDocument, page);
    CGContextSaveGState(context);
    CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(pageRef, kCGPDFCropBox, self.bounds, 0, true);
    CGContextConcatCTM(context, pdfTransform);
    CGContextDrawPDFPage(context, pageRef);
    CGContextRestoreGState(context);
}

需要說(shuō)明的是:

  • Quartz 2D坐標(biāo)系的原點(diǎn)在界面左下角,而UIKit坐標(biāo)系的原點(diǎn)在左上角,所以需要對(duì)畫(huà)布進(jìn)行翻轉(zhuǎn),才能得到正確的視圖;
  • 方法中需要傳入page和pdfDocument,CGContextDrawPDFPage是按照頁(yè)碼來(lái)一張張繪制的;

再放入UIPageViewController中來(lái)展示,翻頁(yè)效果杠杠的(具體效果見(jiàn)最后Demo)。

順便說(shuō)說(shuō)UIPageViewController的使用

  • UIPageViewControllerde感覺(jué)就像是一個(gè)書(shū)夾,其中每一頁(yè)是一個(gè)UIViewController,頁(yè)面內(nèi)容在UIViewController的視圖中繪制,翻頁(yè)效果是自帶的,你只要繪制好要展示的畫(huà)面就好了;

  • 在初始化UIPageViewControllerde對(duì)象的時(shí)候,就要加載第一頁(yè),避免開(kāi)始時(shí)出現(xiàn)空白頁(yè);

  • 在下面方法中寫(xiě)上一頁(yè),下一頁(yè)的調(diào)用邏輯;

//加載某一頁(yè)調(diào)用方法
- (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion;

//上一頁(yè)視圖控制器
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
//下一頁(yè)視圖控制器
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
  • UIPageViewControllerde顯示的內(nèi)容沒(méi)有縮放的功能,所以需要自己加。我是在UIViewController視圖里面嵌了一層UIScrollView,設(shè)置其縮放系數(shù)minimumZoomScale, maximumZoomScale,然后實(shí)現(xiàn)下面的代理方法來(lái)實(shí)現(xiàn)縮放的。
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

當(dāng)然,這也可以用其他方法來(lái)完成,比如:UIPinchGestureRecognizer

4.MuPDF類庫(kù)的使用

MuPDF官網(wǎng):http://www.mupdf.com/

4.1 生成依賴包

  1. 源碼下載:git clone --recursive git://git.ghostscript.com/mupdf.git
  2. 下載完成后,進(jìn)入mupdf/platform/ios目錄下;
  3. 打開(kāi)MuPDF工程;
  4. 修改工程的Build Configuration為Release;
  5. 選擇iPhone模擬器,運(yùn)行這個(gè)工程,這將生成i386或x86_64結(jié)構(gòu)的.a包(根據(jù)你選擇的模擬器類型);
  6. 連接你的iPhone,修改你自己的bundle identifier, certificate和 provisioning profile,在真機(jī)環(huán)境下運(yùn)行該程序,這將會(huì)生成armv7,arm64架構(gòu)的.a包;
  7. 同理,修改工程的Build Configuration為Debug,可以生成Debug對(duì)應(yīng)的.a包;
  8. 進(jìn)入mupdf/build/,你會(huì)找到各個(gè)架構(gòu)下的文件夾,里面包含了所有編譯之后生成的包:
  9. 現(xiàn)在,你可以使用lipo命令去合并些架構(gòu)下的依賴包,這將會(huì)生成比較大的包;

使用lipo命令合并.a包:

Last login: Mon Feb 29 20:43:50 on console
promote:~ mac$ cd /Users/mac/Desktop/lib 
promote:lib mac$ lipo -create libmupdf_arm64.a libmupdf_x86_64.a -output libmupdf.a
promote:lib mac$ lipo -info libmupdf.a
Architectures in the fat file: libmupdf.a are: x86_64 arm64 
promote:lib mac$ lipo -create libmupdfthird_arm64.a libmupdfthird_x86_64.a -output libmupdfthird.a
promote:lib mac$ lipo -info libmupdfthird.a
Architectures in the fat file: libmupdfthird.a are: x86_64 arm64 
promote:lib mac$ 

lipo之后的包
  • 我只合并了debug中的arm64和x86_64的包,要是再合并i386就太大了。

4.2 開(kāi)始集成

  1. 添加所需要的源碼文件:
    mupdf/include/mupdf路徑下的所有頭文件;
    mupdf/platform/ios/classes路徑下的所有obj-c類;
    mupdf/platform/ios路徑下的common.h,common .m
  2. 添加之前生成好的依賴包到你的工程中;
  3. 配置你工程的Library Search Path,添加依賴包的路徑,
    比如:$(inherited) $(PROJECT_DIR)/External/MuPDF/lib/
  4. 由于庫(kù)中引用文件#include "mupdf/fitz.h",使用include編譯指令,所以頭文件絕對(duì)路徑=搜索路徑+相對(duì)路徑,所以需要配置搜索路徑Header Search Paths"$(SRCROOT)/OpenPFDemo/ThirdLib/include"

現(xiàn)在,你應(yīng)該就可以運(yùn)行你的程序了(具體的文件目錄見(jiàn)Demo)。

4.3 提示:

  1. 由于合并之后的依賴包比較大,所以為了減少你App的大小,你可以配置兩個(gè)路徑:
    在release文件夾下添加mupdf/build/release-ios-armv7-arm64路徑下的依賴包;
    在debug文件夾下添加mupdf/build/debug-ios-arm64和debug-ios-x86_64路徑下的合并后的包;
  • 在你的工程配置Library Search Path中,分別配置Debug和Release的路徑;

這樣你在模擬器和真機(jī)Debug環(huán)境下使用的就是Debug對(duì)應(yīng)的依賴包,而在打包的時(shí)候使用的就是Release對(duì)應(yīng)的依賴包,而不會(huì)包含其他依賴包。

4.4 注意

  • 按照 stackoverflow上面的這個(gè)問(wèn)答的集成步驟在當(dāng)前最新的MuPDF版本上集成時(shí)會(huì)有些出入,所以我修改了一下;
  • 由于這個(gè)庫(kù)包含了很多平臺(tái)(見(jiàn)platform文件夾下),所以比較大,要等完全下載完成之后再使用;
  • 下載完成之后,運(yùn)行其中的iOS代碼,發(fā)現(xiàn)居然報(bào)錯(cuò)了(我用的xcode7.0)
    原因是找不到pdf_write_document(ctx, idoc, tmp, &opts)方法。

解決方法:將MuDocumentController.m中的pdf_write_document(ctx, idoc, tmp, &opts);改為pdf_create_document(ctx);即可;

4.6 使用打開(kāi)PDF

  • 首先,在程序啟動(dòng)后,初始化相關(guān)參數(shù):
enum
{
    ResourceCacheMaxSize = 128<<20  /**< use at most 128M for resource cache */
};

queue = dispatch_queue_create("com.artifex.mupdf.queue", NULL);
ctx = fz_new_context(NULL, NULL, ResourceCacheMaxSize);
fz_register_document_handlers(ctx);
  • 然后調(diào)用下面方法打開(kāi):
- (void)openMuPDF:(NSString *)path name:(NSString *)name
{
    _filePath = malloc(strlen([path UTF8String])+1);
    
    strcpy(_filePath, [path UTF8String]);
    
    dispatch_sync(queue, ^{});
    
    printf("open document '%s'\n", _filePath);
    
    _filename = [name retain];
    
    doc = [[MuDocRef alloc] initWithFilename:_filePath];
    if (!doc) {
        
        return;
    }
    
    if (fz_needs_password(ctx, doc->doc))
    {
        [self askForPassword: @"'%@' needs a password:"];
    }
    else
    {
        [self onPasswordOkay];
    }
}

- (void)askForPassword: (NSString*)prompt
{
    UIAlertView *passwordAlertView = [[UIAlertView alloc]
                                      initWithTitle: @"Password Protected"
                                      message: [NSString stringWithFormat: prompt, [_filename lastPathComponent]]
                                      delegate: self
                                      cancelButtonTitle: @"Cancel"
                                      otherButtonTitles: @"Done", nil];
    [passwordAlertView setAlertViewStyle: UIAlertViewStyleSecureTextInput];
    [passwordAlertView show];
    [passwordAlertView release];
}

- (void)onPasswordOkay
{
    MuDocumentController *document = [[MuDocumentController alloc] initWithFilename:_filename path:_filePath document:doc];
    if (document) {
        [self setTitle: @"Library"];
        [[self navigationController] pushViewController:document animated:YES];
        [document release];
    }
    [_filename release];
    free(_filePath);
}

Demo下載鏈接:OpenPDFDemo

@Kangqj

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,991評(píng)論 19 139
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,255評(píng)論 4 61
  • Swift版本點(diǎn)擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 25,573評(píng)論 7 249
  • 環(huán)境 操作系統(tǒng):Centos 6.* ( 雙核4G、硬盤(pán)至少20G)zabbix server :192.168...
    WFF_FFW閱讀 1,025評(píng)論 0 4
  • 今天回家看見(jiàn)小小不點(diǎn)兒,似乎比在醫(yī)院的時(shí)候瘦了,全身是紅黑紅黑的,臉上還有一些小小眼兒。媽媽說(shuō)那是正常的。
    煙澀寒閱讀 151評(píng)論 0 0