Skip to content

Commit dc28625

Browse files
the-noob-101christopherdro
authored andcommitted
Allow padding options for all sides in iOS (#82)
* Switched ios padding for paddingHorizontal and paddingVertical. Changed the docs to the new props * iOS now return the number of pdfs pages in the promise object key: numberOfPages * Cleaned iOS Files * Fixed a small bug * know android retrieves the number of pdf pages * Changed default size of pdf to standard A4 * feat: add padding bot, top, left and right * chore: update README * feat: background color always in #f6f5f0
1 parent e33b86f commit dc28625

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ export default class Example extends Component {
101101
|---|---|---|---|
102102
| `height` | number | 792 | Set document height (points)
103103
| `width` | number | 612 | Set document width (points)
104-
| `padding` | number | 10 | Outer padding (points)
104+
| `paddingLeft` | number | 10 | Outer left padding (points)
105+
| `paddingRight` | number | 10 | Outer right padding (points)
106+
| `paddingTop` | number | 10 | Outer top padding (points)
107+
| `paddingBottom` | number | 10 | Outer bottom padding (points)
108+
| `padding` | number | 10 | Outer padding for any side (points), overrides any padding listed before
105109

106110

107111
#### Android Only

android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ repositories {
3333

3434

3535
dependencies {
36+
compile 'com.tom_roush:pdfbox-android:1.8.10.0'
3637
compile 'com.facebook.react:react-native:+'
3738
}

android/src/main/java/android/print/PdfConverter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.facebook.react.bridge.Promise;
2323
import com.facebook.react.bridge.WritableMap;
2424

25+
import com.tom_roush.pdfbox.pdmodel.PDDocument;
26+
2527
/**
2628
* Converts HTML to PDF.
2729
* <p>
@@ -74,7 +76,12 @@ public void onWriteFinished(PageRange[] pages) {
7476
if (mShouldEncode) {
7577
base64 = encodeFromFile(mPdfFile);
7678
}
79+
80+
PDDocument myDocument = PDDocument.load(mPdfFile);
81+
int pagesToBePrinted = myDocument.getNumberOfPages();
82+
7783
mResultMap.putString("filePath", mPdfFile.getAbsolutePath());
84+
mResultMap.putString("numberOfPages", String.valueOf(pagesToBePrinted));
7885
mResultMap.putString("base64", base64);
7986
mPromise.resolve(mResultMap);
8087
} catch (IOException e) {
@@ -137,7 +144,7 @@ private PrintAttributes getDefaultPrintAttrs() {
137144
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return null;
138145

139146
return new PrintAttributes.Builder()
140-
.setMediaSize(PrintAttributes.MediaSize.NA_GOVT_LETTER)
147+
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
141148
.setResolution(new PrintAttributes.Resolution("RESOLUTION_ID", "RESOLUTION_ID", 600, 600))
142149
.setMinMargins(PrintAttributes.Margins.NO_MARGINS)
143150
.build();

ios/RNHTMLtoPDF/RNHTMLtoPDF.m

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
// Created by Christopher on 9/3/15.
33

4+
#import <CoreGraphics/CoreGraphics.h>
45
#import <UIKit/UIKit.h>
56
#import <React/RCTConvert.h>
67
#import <React/RCTEventDispatcher.h>
@@ -12,7 +13,7 @@
1213
#define PDFSize CGSizeMake(612,792)
1314

1415
@implementation UIPrintPageRenderer (PDF)
15-
- (NSData*) printToPDF
16+
- (NSData*) printToPDF:(NSInteger**)_numberOfPages
1617
{
1718
NSMutableData *pdfData = [NSMutableData data];
1819
UIGraphicsBeginPDFContextToData( pdfData, self.paperRect, nil );
@@ -24,8 +25,16 @@ - (NSData*) printToPDF
2425
for ( int i = 0 ; i < self.numberOfPages ; i++ )
2526
{
2627
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+
2734
[self drawPageAtIndex: i inRect: bounds];
2835
}
36+
37+
*_numberOfPages = self.numberOfPages;
2938

3039
UIGraphicsEndPDFContext();
3140
return pdfData;
@@ -39,9 +48,13 @@ @implementation RNHTMLtoPDF {
3948
NSString *_html;
4049
NSString *_fileName;
4150
NSString *_filePath;
51+
NSInteger *_numberOfPages;
4252
CGSize _PDFSize;
4353
UIWebView *_webView;
44-
float _padding;
54+
float _paddingBottom;
55+
float _paddingTop;
56+
float _paddingLeft;
57+
float _paddingRight;
4558
BOOL _base64;
4659
BOOL autoHeight;
4760
}
@@ -103,10 +116,35 @@ - (instancetype)init
103116
_PDFSize = PDFSize;
104117
}
105118

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"]];
108139
} 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"]];
110148
}
111149

112150
NSString *path = [[NSBundle mainBundle] bundlePath];
@@ -130,12 +168,13 @@ - (void)webViewDidFinishLoad:(UIWebView *)awebView
130168
// Define the printableRect and paperRect
131169
// If the printableRect defines the printable area of the page
132170
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+
134173

135174
[render setValue:[NSValue valueWithCGRect:paperRect] forKey:@"paperRect"];
136175
[render setValue:[NSValue valueWithCGRect:printableRect] forKey:@"printableRect"];
137176

138-
NSData *pdfData = [render printToPDF];
177+
NSData * pdfData = [render printToPDF:&_numberOfPages];
139178

140179
if (pdfData) {
141180
NSString *pdfBase64 = @"";
@@ -146,6 +185,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)awebView
146185
}
147186
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
148187
pdfBase64, @"base64",
188+
[NSString stringWithFormat: @"%ld", (long)_numberOfPages], @"numberOfPages",
149189
_filePath, @"filePath", nil];
150190
_resolveBlock(data);
151191
} else {

0 commit comments

Comments
 (0)