Skip to content

Commit

Permalink
[iOS] Return pdf as Base64
Browse files Browse the repository at this point in the history
* Update README.md

* base64 pdf

* Update index.ios.js

* Update index.ios.js

* Update README.md
  • Loading branch information
Nicolas-Menettrier authored and christopherdro committed Jan 15, 2017
1 parent 7a9058f commit f04f7a6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
4 changes: 2 additions & 2 deletions HTMLToPDFExample/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ var HTMLToPDFExample = React.createClass({
fileName: 'test'
};

RNHTMLtoPDF.convert(options).then((result) => {
RNHTMLtoPDF.convert(options).then((data) => {
RNMail.mail({
subject: '',
recipients: [''],
body: '',
attachmentPath: result,
attachmentPath: data.filePath,
attachmentType: 'pdf',
}, (error, event) => {
if(error) {
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ var Example = React.createClass({
Default: Temp directory
*/

height: 800, /* Optional: 800 sets the height of the DOCUMENT that will be produced
base64: true /* Optional: get the base64 PDF value
Default: false
*/

height: 800 /* Optional: 800 sets the height of the DOCUMENT that will be produced
Default: 612
*/
width: 1056, /* Optional: 1056 sets the width of the DOCUMENT that will produced
Expand All @@ -62,9 +67,11 @@ var Example = React.createClass({
*/
};

RNHTMLtoPDF.convert(options).then((filePath) => {
console.log(filePath);
RNHTMLtoPDF.convert(options).then((data) => {
console.log(data.filePath);
console.log(data.base64);
});
// data.base64 will be empty if base64 is set to false
},

render() {
Expand Down
17 changes: 16 additions & 1 deletion RNHTMLtoPDF/RNHTMLtoPDF.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ @implementation RNHTMLtoPDF {
CGSize _PDFSize;
UIWebView *_webView;
float _padding;
BOOL _base64;
BOOL autoHeight;
}

Expand Down Expand Up @@ -83,6 +84,12 @@ - (instancetype)init
_filePath = [NSString stringWithFormat:@"%@%@.pdf", NSTemporaryDirectory(), _fileName];
}

if (options[@"base64"] && [options[@"base64"] boolValue]) {
_base64 = true;
} else {
_base64 = false;
}

if (options[@"height"] && options[@"width"]) {
float width = [RCTConvert float:options[@"width"]];
float height = [RCTConvert float:options[@"height"]];
Expand Down Expand Up @@ -126,8 +133,16 @@ - (void)webViewDidFinishLoad:(UIWebView *)awebView
NSData *pdfData = [render printToPDF];

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

[pdfData writeToFile:_filePath atomically:YES];
_resolveBlock(_filePath);
if (_base64) {
pdfBase64 = [pdfData base64EncodedStringWithOptions:0];
}
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
pdfBase64, @"base64",
_filePath, @"filePath", nil];
_resolveBlock(data);
} else {
NSError *error;
_rejectBlock(RCTErrorUnspecified, nil, RCTErrorWithMessage(error.description));
Expand Down

0 comments on commit f04f7a6

Please sign in to comment.