PDF文档生成两种方式:
1.通过上下文绘制
2.通过UIPrintPageRenderer生成
- 上下文绘制
这个绘制方法比较麻烦,要去计算文本图片内容的位置大小,然后再绘制。
- UIPrintPageRenderer
给render设置printformatter,然后直接绘制到PDF上下文中。
printformatter可以添加文本,富文本,html字符串,也有直接将view转化成UIViewPrintFormatter的类别。
这样子就好办了,写一个html,设置其中的样式,加载到webview中,最后webview调用viewPrintFormatter生成printFormatter,渲染PDF。
代码:
YHPDFTool.h
#import <UIKit/UIKit.h>
@interface YHPDFTool : UIView
/** 添加一行标题*/
- (void)addHtmlTitle:(NSString *)title;
/** 添加一个表格*/
-(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编辑器,点这里
已在简记-快速记账本中使用,谢谢大家支持。
网友评论