14
14
15
15
@implementation UIPrintPageRenderer (PDF)
16
16
- (NSData *) printToPDF:(NSInteger **)_numberOfPages
17
+ backgroundColor:(UIColor*)_bgColor
17
18
{
18
19
NSMutableData *pdfData = [NSMutableData data ];
19
20
UIGraphicsBeginPDFContextToData ( pdfData, self.paperRect , nil );
@@ -26,14 +27,14 @@ - (NSData*) printToPDF:(NSInteger**)_numberOfPages
26
27
{
27
28
UIGraphicsBeginPDFPage ();
28
29
29
- UIColor *myColor = [UIColor colorWithRed: ( 246.0 / 255.0 ) green: ( 245.0 / 255.0 ) blue: ( 240.0 / 255.0 ) alpha: 1 ];
30
+
30
31
CGContextRef currentContext = UIGraphicsGetCurrentContext ();
31
- CGContextSetFillColorWithColor (currentContext, myColor .CGColor );
32
+ CGContextSetFillColorWithColor (currentContext, _bgColor .CGColor );
32
33
CGContextFillRect (currentContext, self.paperRect );
33
34
34
35
[self drawPageAtIndex: i inRect: bounds];
35
36
}
36
-
37
+
37
38
*_numberOfPages = self.numberOfPages ;
38
39
39
40
UIGraphicsEndPDFContext ();
@@ -48,6 +49,7 @@ @implementation RNHTMLtoPDF {
48
49
NSString *_html;
49
50
NSString *_fileName;
50
51
NSString *_filePath;
52
+ UIColor *_bgColor;
51
53
NSInteger *_numberOfPages;
52
54
CGSize _PDFSize;
53
55
UIWebView *_webView;
@@ -93,6 +95,28 @@ - (instancetype)init
93
95
_fileName = [[NSProcessInfo processInfo ] globallyUniqueString ];
94
96
}
95
97
98
+ // Default Color
99
+ _bgColor = [UIColor colorWithRed: (246.0 /255.0 ) green: (245.0 /255.0 ) blue: (240.0 /255.0 ) alpha: 1 ];
100
+ if (options[@" bgColor" ]){
101
+ NSString *hex = [RCTConvert NSString: options[@" bgColor" ]];
102
+ hex = [hex uppercaseString ];
103
+ NSString *cString = [hex stringByTrimmingCharactersInSet:
104
+ [NSCharacterSet whitespaceAndNewlineCharacterSet ]];
105
+
106
+ if ((cString.length ) == 7 ) {
107
+ NSScanner *scanner = [NSScanner scannerWithString: cString];
108
+
109
+ UInt32 rgbValue = 0 ;
110
+ [scanner setScanLocation: 1 ]; // Bypass '#' character
111
+ [scanner scanHexInt: &rgbValue];
112
+
113
+ _bgColor = [UIColor colorWithRed: ((float )((rgbValue & 0xFF0000 ) >> 16 ))/255.0 \
114
+ green: ((float )((rgbValue & 0x00FF00 ) >> 8 ))/255.0 \
115
+ blue: ((float )((rgbValue & 0x0000FF ) >> 0 ))/255.0 \
116
+ alpha: 1.0 ];
117
+ }
118
+ }
119
+
96
120
if (options[@" directory" ] && [options[@" directory" ] isEqualToString: @" Documents" ]){
97
121
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
98
122
NSString *documentsPath = [paths objectAtIndex: 0 ];
@@ -174,7 +198,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)awebView
174
198
[render setValue: [NSValue valueWithCGRect: paperRect] forKey: @" paperRect" ];
175
199
[render setValue: [NSValue valueWithCGRect: printableRect] forKey: @" printableRect" ];
176
200
177
- NSData * pdfData = [render printToPDF: &_numberOfPages];
201
+ NSData * pdfData = [render printToPDF: &_numberOfPages backgroundColor: _bgColor ];
178
202
179
203
if (pdfData) {
180
204
NSString *pdfBase64 = @" " ;
0 commit comments