1
1
2
2
// Created by Christopher on 9/3/15.
3
3
4
+ #import < CoreGraphics/CoreGraphics.h>
4
5
#import < UIKit/UIKit.h>
5
6
#import < React/RCTConvert.h>
6
7
#import < React/RCTEventDispatcher.h>
12
13
#define PDFSize CGSizeMake (612 ,792 )
13
14
14
15
@implementation UIPrintPageRenderer (PDF)
15
- - (NSData *) printToPDF
16
+ - (NSData *) printToPDF:( NSInteger **)_numberOfPages
16
17
{
17
18
NSMutableData *pdfData = [NSMutableData data ];
18
19
UIGraphicsBeginPDFContextToData ( pdfData, self.paperRect , nil );
@@ -24,8 +25,16 @@ - (NSData*) printToPDF
24
25
for ( int i = 0 ; i < self.numberOfPages ; i++ )
25
26
{
26
27
UIGraphicsBeginPDFPage ();
28
+
29
+ UIColor *myColor = [UIColor colorWithRed: (246.0 /255.0 ) green: (245.0 /255.0 ) blue: (240.0 /255.0 ) alpha: 1 ];
30
+ CGContextRef currentContext = UIGraphicsGetCurrentContext ();
31
+ CGContextSetFillColorWithColor (currentContext, myColor.CGColor );
32
+ CGContextFillRect (currentContext, self.paperRect );
33
+
27
34
[self drawPageAtIndex: i inRect: bounds];
28
35
}
36
+
37
+ *_numberOfPages = self.numberOfPages ;
29
38
30
39
UIGraphicsEndPDFContext ();
31
40
return pdfData;
@@ -39,9 +48,13 @@ @implementation RNHTMLtoPDF {
39
48
NSString *_html;
40
49
NSString *_fileName;
41
50
NSString *_filePath;
51
+ NSInteger *_numberOfPages;
42
52
CGSize _PDFSize;
43
53
UIWebView *_webView;
44
- float _padding;
54
+ float _paddingBottom;
55
+ float _paddingTop;
56
+ float _paddingLeft;
57
+ float _paddingRight;
45
58
BOOL _base64;
46
59
BOOL autoHeight;
47
60
}
@@ -103,10 +116,35 @@ - (instancetype)init
103
116
_PDFSize = PDFSize;
104
117
}
105
118
106
- if (options[@" padding" ]) {
107
- _padding = [RCTConvert float: options[@" padding" ]];
119
+ if (options[@" paddingBottom" ]) {
120
+ _paddingBottom = [RCTConvert float: options[@" paddingBottom" ]];
121
+ } else {
122
+ _paddingBottom = 10 .0f ;
123
+ }
124
+
125
+ if (options[@" paddingLeft" ]) {
126
+ _paddingLeft = [RCTConvert float: options[@" paddingLeft" ]];
127
+ } else {
128
+ _paddingLeft = 10 .0f ;
129
+ }
130
+
131
+ if (options[@" paddingTop" ]) {
132
+ _paddingTop = [RCTConvert float: options[@" paddingTop" ]];
133
+ } else {
134
+ _paddingTop = 10 .0f ;
135
+ }
136
+
137
+ if (options[@" paddingRight" ]) {
138
+ _paddingRight = [RCTConvert float: options[@" paddingRight" ]];
108
139
} else {
109
- _padding = 10 .0f ;
140
+ _paddingRight = 10 .0f ;
141
+ }
142
+
143
+ if (options[@" padding" ]) {
144
+ _paddingTop = [RCTConvert float: options[@" padding" ]];
145
+ _paddingBottom = [RCTConvert float: options[@" padding" ]];
146
+ _paddingLeft = [RCTConvert float: options[@" padding" ]];
147
+ _paddingRight = [RCTConvert float: options[@" padding" ]];
110
148
}
111
149
112
150
NSString *path = [[NSBundle mainBundle ] bundlePath ];
@@ -130,12 +168,13 @@ - (void)webViewDidFinishLoad:(UIWebView *)awebView
130
168
// Define the printableRect and paperRect
131
169
// If the printableRect defines the printable area of the page
132
170
CGRect paperRect = CGRectMake (0 , 0 , _PDFSize.width , _PDFSize.height );
133
- CGRect printableRect = CGRectMake (_padding, _padding, _PDFSize.width -(_padding * 2 ), _PDFSize.height -(_padding * 2 ));
171
+ CGRect printableRect = CGRectMake (_paddingTop, _paddingLeft, _PDFSize.width -(_paddingLeft + _paddingRight), _PDFSize.height -(_paddingBottom + _paddingTop));
172
+
134
173
135
174
[render setValue: [NSValue valueWithCGRect: paperRect] forKey: @" paperRect" ];
136
175
[render setValue: [NSValue valueWithCGRect: printableRect] forKey: @" printableRect" ];
137
176
138
- NSData *pdfData = [render printToPDF ];
177
+ NSData * pdfData = [render printToPDF: &_numberOfPages ];
139
178
140
179
if (pdfData) {
141
180
NSString *pdfBase64 = @" " ;
@@ -146,6 +185,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)awebView
146
185
}
147
186
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
148
187
pdfBase64, @" base64" ,
188
+ [NSString stringWithFormat: @" %ld " , (long )_numberOfPages], @" numberOfPages" ,
149
189
_filePath, @" filePath" , nil ];
150
190
_resolveBlock (data);
151
191
} else {
0 commit comments