Skip to content

Commit

Permalink
Add callback on Document and node mounter (#69)
Browse files Browse the repository at this point in the history
Fixes #63
  • Loading branch information
diegomura committed May 22, 2017
1 parent c671601 commit 18f8473
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
4 changes: 3 additions & 1 deletion examples/fractals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ const doc = (
</Document>
);

ReactPDF.render(doc, `${__dirname}/output.pdf`);
ReactPDF.render(doc, `${__dirname}/output.pdf`, () => {
console.log('Fractals rendered!');
});
Binary file modified examples/fractals/output.pdf
Binary file not shown.
8 changes: 6 additions & 2 deletions packages/react-pdf-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import path from 'path';
import { PDFRenderer, createElement, pdf } from '@react-pdf/core';

export default {
render(element, filePath) {
render(element, filePath, callback) {
const container = createElement('ROOT');

const node = PDFRenderer.createContainer(container);

PDFRenderer.updateContainer(element, node, null);

const output = pdf(container).toBuffer();
Expand All @@ -19,6 +19,10 @@ export default {
fs.write(fd, output, 0, output.length, null, function(err) {
if (err) throw new Error(`PDF-react 'Error writing file: ${err}'`);
fs.close(fd, function() {
if (callback) {
callback(output, filePath);
}

console.log(
`📝 PDF successfuly exported on ${path.resolve(filePath)}`,
);
Expand Down
8 changes: 7 additions & 1 deletion packages/react-pdf/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ const Document = 'DOCUMENT';

const pdf = input => {
function parse(input) {
return input.render();
const result = input.render();

if (input.props.onRender) {
input.props.onRender();
}

return result;
}

function toBlob() {
Expand Down

0 comments on commit 18f8473

Please sign in to comment.