PDF文檔生成兩種方式:
1.通過上下文繪制
2.通過UIPrintPageRenderer生成
- 上下文繪制
這個(gè)繪制方法比較麻煩湿酸,要去計(jì)算文本圖片內(nèi)容的位置大小,然后再繪制灭美。
- UIPrintPageRenderer
給render設(shè)置printformatter推溃,然后直接繪制到PDF上下文中。
printformatter可以添加文本届腐,富文本,html字符串硬萍,也有直接將view轉(zhuǎn)化成UIViewPrintFormatter的類別围详。
這樣子就好辦了,寫一個(gè)html助赞,設(shè)置其中的樣式,加載到webview中畜普,最后webview調(diào)用viewPrintFormatter生成printFormatter群叶,渲染PDF钝荡。
代碼:
YHPDFTool.h
#import <UIKit/UIKit.h>
@interface YHPDFTool : UIView
/** 添加一行標(biāo)題*/
- (void)addHtmlTitle:(NSString *)title;
/** 添加一個(gè)表格*/
-(void)addHtmlTableSegList:(NSArray <NSString *>*)titleTitles andContentList:(NSArray <NSArray <NSDictionary *>*>*)contentList;
+ (void)creatPDFOnVC:(UIViewController *)vc andConfigBlock:(void(^)(YHPDFTool * tool))configBlock andFinishBlock:(void(^)(BOOL isSuccess, NSString * filepath))finishblock;
@end
YHPDFTool.m
#import "YHPDFTool.h"
#import "UIView+YH.h"
#import <NSString+YYAdd.h>
@interface YHPDFTool()<UIWebViewDelegate>
@property (retain, nonatomic) UIWebView * webV;
@property (retain, nonatomic) NSMutableString * htmlStr;
@property (copy, nonatomic) void(^finishblock)(BOOL isSuccess, NSString * filepath);
- (void)creatPDF;
@end
@implementation YHPDFTool
+ (void)creatPDFOnVC:(UIViewController *)vc andConfigBlock:(void (^)(YHPDFTool *))configBlock andFinishBlock:(void (^)(BOOL, NSString *))finishblock
{
YHPDFTool * tool = [[YHPDFTool alloc] initWithFrame:CGRectZero];
tool.htmlStr = [[NSMutableString alloc] init];
if(configBlock)
{
configBlock(tool);
}
tool.finishblock = finishblock;
[vc.view addSubview:tool];
[tool creatPDF];
}
- (void)creatPDF
{
self.webV = [[UIWebView alloc] initWithFrame:self.bounds];
self.webV.delegate = self;
[self addSubview:self.webV];
[self.webV loadHTMLString:self.htmlStr baseURL:[[NSBundle mainBundle] bundleURL]];
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
if(self.finishblock)
{
self.finishblock(NO,nil);
}
if(self.superview)
{
[self removeFromSuperview];
}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
UIPrintPageRenderer *render = [[UIPrintPageRenderer alloc] init];
render.headerHeight = 50;
render.footerHeight = 50;
[render addPrintFormatter:[webView viewPrintFormatter] startingAtPageAtIndex:0];
CGRect page;
page.origin.x=0;
page.origin.y=0;
page.size.width=600;
page.size.height=612;
CGRect printable=CGRectInset( page, 0, 0 );
[render setValue:[NSValue valueWithCGRect:page] forKey:@"paperRect"];
[render setValue:[NSValue valueWithCGRect:printable] forKey:@"printableRect"];
// NSLog(@"number of pages %zd",[render numberOfPages]);
NSMutableData * pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData( pdfData, CGRectZero, nil );
for (NSInteger i=0; i < [render numberOfPages]; i++)
{
UIGraphicsBeginPDFPage();
CGRect bounds = UIGraphicsGetPDFContextBounds();
[render drawPageAtIndex:i inRect:bounds];
}
UIGraphicsEndPDFContext();
[pdfData writeToFile:[self pathPDF] atomically:YES];
if(self.finishblock)
{
self.finishblock(YES,[self pathPDF]);
}
if(self.superview)
{
[self removeFromSuperview];
}
}
- (void)addHtmlTitle:(NSString *)title
{
[self.htmlStr appendFormat:@"<br /><p>\
<br />\
<br />\
</p>\
<h2 style=\"text-align:center;\">\
%@\
</h2>\
<p>\
</p>\
",title];
}
-(void)addHtmlTableSegList:(NSArray<NSString *> *)titleTitles andContentList:(NSArray<NSArray<NSDictionary *> *> *)contentList
{
[self.htmlStr appendString:@"<br /><p>\
<table style=\"width:100%;\" cellpadding=\"2\" cellspacing=\"0\" border=\"1\" bordercolor=\"#000000\">\
<tbody>"];
if(titleTitles)
{
[self.htmlStr appendString:@"<tr>"];
for(NSString * str in titleTitles)
{
[self.htmlStr appendFormat:@"<td>%@</td><br />",str];
}
[self.htmlStr appendString:@"</tr>"];
}
if(contentList)
{
for(NSArray * titleList in contentList)
{
[self.htmlStr appendString:@"<tr>"];
for(NSDictionary * dataDic in titleList)
{
[self.htmlStr appendString:@"<td>"];
NSString * str = dataDic[@"title"];
NSString * img = dataDic[@"image"];
if([img isNotBlank])
{
NSString * imagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@2x",img] ofType:@"png"];
[self.htmlStr appendFormat:@"<img src=\"%@\" border=\"0\" width=\"15\" height=\"15\" alt=\"\" /> ",imagePath];
}
if([str isNotBlank])
{
[self.htmlStr appendString:str];
}
[self.htmlStr appendString:@"<br /></td>"];
}
[self.htmlStr appendString:@"</tr>"];
}
}
[self.htmlStr appendString:@" </tbody>\
</table>\
</p>"];
}
- (NSString *)pathPDF
{
NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
path = [path stringByAppendingPathComponent:@"yhpdf.pdf"];
// NSLog(@"====\n%@",path);
return path;
}
@end
如果html不是寫的話 有在線的html編輯器约炎,點(diǎn)這里
已在簡記-快速記賬本中使用蟹瘾,謝謝大家支持。