forked from brentnycum/BNHtmlPdfKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BNHtmlPdfKitPageRenderer.m
117 lines (86 loc) · 3.37 KB
/
BNHtmlPdfKitPageRenderer.m
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
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
//
// BNHtmlPdfKitPageRenderer.m
// ACTPrinter4
//
// Created by Pierre Bernard on 6/11/13.
// Copyright (c) 2013 Houdah Software. All rights reserved.
//
#import "BNHtmlPdfKitPageRenderer.h"
#define HEADER_LEFT_TEXT_INSET 20
#define HEADER_FOOTER_MARGIN_PADDING 5
#define FOOTER_RIGHT_TEXT_INSET 20
@interface BNHtmlPdfKitPageRenderer ()
@property (nonatomic, strong) UIFont *headerFooterFont;
@property (nonatomic, strong) NSString *timestampString;
@property (nonatomic, assign) NSRange pageRange;
@end
@implementation BNHtmlPdfKitPageRenderer
- (id)init
{
self = [super init];
if (self != nil) {
_printHeader = YES;
_printFooter = YES;
_headerFooterFont = [UIFont fontWithName:@"Helvetica" size:12.0f];
CGSize headerFooterSize = [@"Sample" sizeWithFont : _headerFooterFont];
CGFloat headerFooterHeight = headerFooterSize.height + HEADER_FOOTER_MARGIN_PADDING;
self.headerHeight = headerFooterHeight;
self.footerHeight = headerFooterHeight;
}
return self;
}
- (CGRect)paperRect
{
return UIGraphicsGetPDFContextBounds();
}
- (CGRect)printableRect
{
return CGRectInset([self paperRect], self.leftAndRightMarginSize, self.topAndBottomMarginSize);
}
- (void)prepareForDrawingPages:(NSRange)range
{
self.pageRange = range;
self.timestampString = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterMediumStyle
timeStyle:NSDateFormatterMediumStyle];
[super prepareForDrawingPages:range];
}
- (void)drawHeaderForPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)headerRect
{
if (self.printHeader) {
NSString *pageTitle = self.pageTitle;
if (pageTitle != nil) {
UIFont *headerFooterFont = self.headerFooterFont;
CGFloat startX = CGRectGetMinX(headerRect) + HEADER_LEFT_TEXT_INSET;
CGFloat startY = self.printableRect.origin.y + HEADER_FOOTER_MARGIN_PADDING;
CGPoint startPoint = CGPointMake(startX, startY);
[pageTitle drawAtPoint:startPoint withFont:headerFooterFont];
}
}
}
- (void)drawFooterForPageAtIndex:(NSInteger)pageIndex inRect:(CGRect)footerRect
{
if (self.printFooter) {
UIFont *headerFooterFont = self.headerFooterFont;
NSString *timestampString = self.timestampString;
if (timestampString != nil) {
CGSize timestampStringSize = [timestampString sizeWithFont:headerFooterFont];
CGFloat startX = CGRectGetMinX(footerRect) + HEADER_LEFT_TEXT_INSET;
CGFloat startY = CGRectGetMaxY(self.printableRect) - timestampStringSize.height - HEADER_FOOTER_MARGIN_PADDING;
CGPoint startPoint = CGPointMake(startX, startY);
[timestampString drawAtPoint:startPoint withFont:headerFooterFont];
}
{
NSString *localizedPageNumberString = NSLocalizedString(@"Page %d of %d", @"BNHtmlPdfKit: Page Count String");
NSRange pageRange = self.pageRange;
NSString *pageNumberString = [NSString stringWithFormat:localizedPageNumberString,
pageIndex + 1 - pageRange.location, pageRange.length];
CGSize pageNumSize = [pageNumberString sizeWithFont:headerFooterFont];
CGFloat startX = CGRectGetMaxX(footerRect) - pageNumSize.width - FOOTER_RIGHT_TEXT_INSET;
CGFloat startY = CGRectGetMaxY(self.printableRect) - pageNumSize.height - HEADER_FOOTER_MARGIN_PADDING;
CGPoint startPoint = CGPointMake(startX, startY);
[pageNumberString drawAtPoint:startPoint withFont:headerFooterFont];
}
}
}
@end