加载PDF文件
UIWebView 加载本地或网络文件
1 | #import <WebKit/WebKit.h> |
QLPreviewController
1 | #import <QuickLook/QuickLook.h> |
第三方框架vfr/Reader加载pdf文档
github地址:vfr/Reader1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#import "ReaderViewController.h"
<ReaderViewControllerDelegate>
- (void)loadPDFFile{
NSString *path = [[NSBundle mainBundle] pathForResource:@"photos" ofType:@"pdf"];
ReaderDocument *doc = [[ReaderDocument alloc] initWithFilePath:path password:nil];
ReaderViewController *rvc = [[ReaderViewController alloc] initWithReaderDocument:doc];
rvc.delegate = self;
[self presentViewController:rvc animated:YES completion:nil];
}
#pragma mark - ReaderViewControllerDelegate
- (void)dismissReaderViewController:(ReaderViewController *)viewController {
[self dismissViewControllerAnimated:YES completion:nil];
}
下载PDF文件
data writeToFile
1 | - (void)downloadPDFFile{ |
AFNetworking
1 | - (void)downloadPDFFile{ |
生成PDF文件
各种格式文件生成PDF文件或生成图片文件
通过WebView可以加载各种格式文件,然后可以生成PDF文件或生成图片文件。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30- (void)convertToPDF{
NSData *pdfData = [self.webView convert2PDFData];
NSString *fileName = [NSString stringWithFormat:@"PDF_%.0f.pdf",[[NSDate date] timeIntervalSince1970]];
NSString *pdfPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]stringByAppendingPathComponent:fileName];
BOOL result = [pdfData writeToFile:pdfPath atomically:YES];
if (result) {
NSLog(@"%@",pdfPath);
NSLog(@"转换PDF成功");
}else{
NSLog(@"转换PDF失败");
}
}
- (void)convertToImage{
UIImage *image = [self.webView convert2Image];
NSData *imageData = UIImagePNGRepresentation(image);
// NSString *imagePath = [WYPDFConverter saveDirectory:[NSString stringWithFormat:@"%@_IMG.png",self.fileName]];
NSString *fileName = [NSString stringWithFormat:@"IMG_%.0f.jpg",[[NSDate date] timeIntervalSince1970]];
NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]stringByAppendingPathComponent:fileName];
BOOL result = [imageData writeToFile:imagePath atomically:YES];
if (result) {
NSLog(@"%@",imagePath);
NSLog(@"转换Image成功");
}else{
NSLog(@"转换Image失败");
}
}
UIWebView+PDFFile.h1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#import <UIKit/UIKit.h>
@interface UIWebView (PDFFile)
/**
webView转换为PDF数据
*/
- (NSData *)convert2PDFData;
/**
转换成image图片
*/
- (UIImage *)convert2Image;
@end
UIWebView+PDFFile.m1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117#import "UIWebView+PDFFile.h"
@implementation UIWebView (PDFFile)
- (NSData *)convert2PDFData{
// 返回视图的打印格式化
UIViewPrintFormatter *format = [self viewPrintFormatter];
UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
[render addPrintFormatter:format startingAtPageAtIndex:0];
// 设置PDF文件每页的尺寸
CGRect pageRect = CGRectMake(0, 0, 600, 768);
// 呈现每个页面的上下文的尺寸大小
CGRect printableRect = CGRectInset(pageRect, 50, 50);
[render setValue:[NSValue valueWithCGRect:pageRect] forKey:@"paperRect"];
[render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];
NSMutableData *pdfData = [NSMutableData data];
// 文档信息 可设置为nil
// CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(nil, 0,
// &kCFTypeDictionaryKeyCallBacks,
// &kCFTypeDictionaryValueCallBacks);
// CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
// CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));
UIGraphicsBeginPDFContextToData(pdfData, pageRect, NULL);
for (NSInteger i = 0; i < [render numberOfPages]; i++) {
UIGraphicsBeginPDFPage();
CGRect bounds = UIGraphicsGetPDFContextBounds();
[render drawPageAtIndex:i inRect:bounds];
}
UIGraphicsEndPDFContext();
return pdfData;
}
- (UIImage *)convert2Image{
/*
将 UIWebView 分屏截取,然后将截取的图片拼接成一张图片
将 UIWebView 从头,contentOffset = (0, 0),开始截取webView.bounds.size.height高度的图片,
*/
CGSize boundsSize = self.bounds.size;
CGFloat boundsWidth = self.bounds.size.width;
CGFloat boundsHeight = self.bounds.size.height;
CGPoint offset = self.scrollView.contentOffset;
[self.scrollView setContentOffset:CGPointMake(0, 0)];
CGFloat contentHeight = self.scrollView.contentSize.height;
NSMutableArray *images = [NSMutableArray array];
while (contentHeight > 0) {
UIGraphicsBeginImageContext(boundsSize);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[images addObject:image];
CGFloat offsetY = self.scrollView.contentOffset.y;
[self.scrollView setContentOffset:CGPointMake(0, offsetY + boundsHeight)];
contentHeight -= boundsHeight;
}
[self.scrollView setContentOffset:offset];
UIGraphicsBeginImageContext(self.scrollView.contentSize);
[images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL *stop) {
[image drawInRect:CGRectMake(0, boundsHeight * idx, boundsWidth, boundsHeight)];
}];
UIImage *fullImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return fullImage;
// CGFloat scale = [UIScreen mainScreen].scale;
// UIImage* image = nil;
//
// UIGraphicsBeginImageContextWithOptions(self.scrollView.contentSize, NO, scale);
// //保存现在的位置和尺寸
// CGPoint savedContentOffset = self.scrollView.contentOffset;
// CGRect savedFrame = self.frame;
// //设置尺寸和内容一样大
// self.scrollView.contentOffset = CGPointZero;
// self.frame = CGRectMake(0, 0, self.scrollView.contentSize.width, self.scrollView.contentSize.height);
//
// [self.layer renderInContext: UIGraphicsGetCurrentContext()];
// image = UIGraphicsGetImageFromCurrentImageContext();
//
// //恢复原来的位置和尺寸
// self.scrollView.contentOffset = savedContentOffset;
// self.frame = savedFrame;
//
// UIGraphicsEndImageContext();
//
// return image;
}
@end
多张图片生成PDF文件
1 | - (void)convertImagesToPDFFile{ |