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

1.使用UIWebView加載
//代碼很簡單
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打開
//初始化QLPreviewController對象
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
[self.navigationController pushViewController:pageController animated:YES];
//再實現(xiàn)QLPreviewControllerDataSource的兩個方法即可顯示
//顯示文件數(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);
}
需要說明的是:
- Quartz 2D坐標(biāo)系的原點在界面左下角,而UIKit坐標(biāo)系的原點在左上角,所以需要對畫布進(jìn)行翻轉(zhuǎn),才能得到正確的視圖;
- 方法中需要傳入page和pdfDocument,CGContextDrawPDFPage是按照頁碼來一張張繪制的;
再放入UIPageViewController中來展示,翻頁效果杠杠的(具體效果見最后Demo)。
順便說說UIPageViewController的使用
UIPageViewControllerde感覺就像是一個書夾,其中每一頁是一個UIViewController,頁面內(nèi)容在UIViewController的視圖中繪制,翻頁效果是自帶的,你只要繪制好要展示的畫面就好了;在初始化
UIPageViewControllerde對象的時候,就要加載第一頁,避免開始時出現(xiàn)空白頁;在下面方法中寫上一頁,下一頁的調(diào)用邏輯;
//加載某一頁調(diào)用方法
- (void)setViewControllers:(nullable NSArray<UIViewController *> *)viewControllers direction:(UIPageViewControllerNavigationDirection)direction animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion;
//上一頁視圖控制器
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
//下一頁視圖控制器
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
-
UIPageViewControllerde顯示的內(nèi)容沒有縮放的功能,所以需要自己加。我是在UIViewController視圖里面嵌了一層UIScrollView,設(shè)置其縮放系數(shù)minimumZoomScale, maximumZoomScale,然后實現(xiàn)下面的代理方法來實現(xiàn)縮放的。
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
當(dāng)然,這也可以用其他方法來完成,比如:UIPinchGestureRecognizer
4.MuPDF類庫的使用
MuPDF官網(wǎng):http://www.mupdf.com/
4.1 生成依賴包
- 源碼下載:
git clone --recursive git://git.ghostscript.com/mupdf.git; - 下載完成后,進(jìn)入
mupdf/platform/ios目錄下; - 打開MuPDF工程;
- 修改工程的Build Configuration為Release;
- 選擇iPhone模擬器,運行這個工程,這將生成i386或x86_64結(jié)構(gòu)的.a包(根據(jù)你選擇的模擬器類型);
- 連接你的iPhone,修改你自己的bundle identifier, certificate和 provisioning profile,在真機環(huán)境下運行該程序,這將會生成armv7,arm64架構(gòu)的.a包;
- 同理,修改工程的Build Configuration為Debug,可以生成Debug對應(yīng)的.a包;
- 進(jìn)入
mupdf/build/,你會找到各個架構(gòu)下的文件夾,里面包含了所有編譯之后生成的包: - 現(xiàn)在,你可以使用
lipo命令去合并些架構(gòu)下的依賴包,這將會生成比較大的包;
使用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$

- 我只合并了debug中的arm64和x86_64的包,要是再合并i386就太大了。
4.2 開始集成
- 添加所需要的源碼文件:
mupdf/include/mupdf路徑下的所有頭文件;
mupdf/platform/ios/classes路徑下的所有obj-c類;
mupdf/platform/ios路徑下的common.h,common .m; - 添加之前生成好的依賴包到你的工程中;
- 配置你工程的
Library Search Path,添加依賴包的路徑,
比如:$(inherited) $(PROJECT_DIR)/External/MuPDF/lib/ - 由于庫中引用文件
#include "mupdf/fitz.h",使用include編譯指令,所以頭文件絕對路徑=搜索路徑+相對路徑,所以需要配置搜索路徑Header Search Paths為"$(SRCROOT)/OpenPFDemo/ThirdLib/include";
現(xiàn)在,你應(yīng)該就可以運行你的程序了(具體的文件目錄見Demo)。
4.3 提示:
- 由于合并之后的依賴包比較大,所以為了減少你App的大小,你可以配置兩個路徑:
在release文件夾下添加mupdf/build/release-ios-armv7-arm64路徑下的依賴包;
在debug文件夾下添加mupdf/build/debug-ios-arm64和debug-ios-x86_64路徑下的合并后的包;
- 在你的工程配置
Library Search Path中,分別配置Debug和Release的路徑;
這樣你在模擬器和真機Debug環(huán)境下使用的就是Debug對應(yīng)的依賴包,而在打包的時候使用的就是Release對應(yīng)的依賴包,而不會包含其他依賴包。
4.4 注意
- 按照 stackoverflow上面的這個問答的集成步驟在當(dāng)前最新的MuPDF版本上集成時會有些出入,所以我修改了一下;
- 由于這個庫包含了很多平臺(見
platform文件夾下),所以比較大,要等完全下載完成之后再使用; - 下載完成之后,運行其中的iOS代碼,發(fā)現(xiàn)居然報錯了(我用的xcode7.0)
原因是找不到pdf_write_document(ctx, idoc, tmp, &opts)方法。
解決方法:將MuDocumentController.m中的pdf_write_document(ctx, idoc, tmp, &opts);改為pdf_create_document(ctx);即可;
4.6 使用打開PDF
- 首先,在程序啟動后,初始化相關(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)用下面方法打開:
- (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