Skip to content

Commit a88ae96

Browse files
sydgchristopherdro
authored andcommitted
Add param to change the Background Color in iOS (#112)
1 parent 2cf694f commit a88ae96

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export default class Example extends Component {
106106
| `paddingTop` | number | 10 | Outer top padding (points)
107107
| `paddingBottom` | number | 10 | Outer bottom padding (points)
108108
| `padding` | number | 10 | Outer padding for any side (points), overrides any padding listed before
109+
| `bgColor` | string | #F6F5F0 | Background color in Hexadecimal
109110

110111

111112
#### Android Only

ios/RNHTMLtoPDF/RNHTMLtoPDF.m

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
@implementation UIPrintPageRenderer (PDF)
1616
- (NSData*) printToPDF:(NSInteger**)_numberOfPages
17+
backgroundColor:(UIColor*)_bgColor
1718
{
1819
NSMutableData *pdfData = [NSMutableData data];
1920
UIGraphicsBeginPDFContextToData( pdfData, self.paperRect, nil );
@@ -26,14 +27,14 @@ - (NSData*) printToPDF:(NSInteger**)_numberOfPages
2627
{
2728
UIGraphicsBeginPDFPage();
2829

29-
UIColor *myColor = [UIColor colorWithRed: (246.0/255.0) green:(245.0/255.0) blue:(240.0/255.0) alpha:1];
30+
3031
CGContextRef currentContext = UIGraphicsGetCurrentContext();
31-
CGContextSetFillColorWithColor(currentContext, myColor.CGColor);
32+
CGContextSetFillColorWithColor(currentContext, _bgColor.CGColor);
3233
CGContextFillRect(currentContext, self.paperRect);
3334

3435
[self drawPageAtIndex: i inRect: bounds];
3536
}
36-
37+
3738
*_numberOfPages = self.numberOfPages;
3839

3940
UIGraphicsEndPDFContext();
@@ -48,6 +49,7 @@ @implementation RNHTMLtoPDF {
4849
NSString *_html;
4950
NSString *_fileName;
5051
NSString *_filePath;
52+
UIColor *_bgColor;
5153
NSInteger *_numberOfPages;
5254
CGSize _PDFSize;
5355
UIWebView *_webView;
@@ -93,6 +95,28 @@ - (instancetype)init
9395
_fileName = [[NSProcessInfo processInfo] globallyUniqueString];
9496
}
9597

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+
96120
if (options[@"directory"] && [options[@"directory"] isEqualToString:@"Documents"]){
97121
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
98122
NSString *documentsPath = [paths objectAtIndex:0];
@@ -174,7 +198,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)awebView
174198
[render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
175199
[render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];
176200

177-
NSData * pdfData = [render printToPDF:&_numberOfPages];
201+
NSData * pdfData = [render printToPDF:&_numberOfPages backgroundColor:_bgColor ];
178202

179203
if (pdfData) {
180204
NSString *pdfBase64 = @"";

0 commit comments

Comments
 (0)