diff --git a/HTMLToPDFExample/index.ios.js b/HTMLToPDFExample/index.ios.js index d3922bc..1a9ac99 100644 --- a/HTMLToPDFExample/index.ios.js +++ b/HTMLToPDFExample/index.ios.js @@ -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) { diff --git a/README.md b/README.md index 3f6dce1..e8389eb 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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() { diff --git a/RNHTMLtoPDF/RNHTMLtoPDF.m b/RNHTMLtoPDF/RNHTMLtoPDF.m index 8ce5998..e708ba4 100644 --- a/RNHTMLtoPDF/RNHTMLtoPDF.m +++ b/RNHTMLtoPDF/RNHTMLtoPDF.m @@ -42,6 +42,7 @@ @implementation RNHTMLtoPDF { CGSize _PDFSize; UIWebView *_webView; float _padding; + BOOL _base64; BOOL autoHeight; } @@ -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"]]; @@ -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));