Reputation: 127
Hi I need to convert particular react components into html file so i can send those html to server for generate pdf file.
Is there any libraries available? Thanks in advance.
Upvotes: 1
Views: 8032
Reputation: 158
I entered this question looking for the answer to the following question:
"How to generate a HTML file from a React component?".
But the author actually wants to generate a PDF file from a React Component.
Fast forward to this date, here is what you wanted:
Upvotes: 1
Reputation: 20047
You can render react components in Node on the server using Server Side Rendering.
If you can pass props to the server side, and render the component there then on the server side you should be able to grab the HTML generated and pass that to something which can create a PDF.
What are you using on the server? are you after a one off render or do you expect to be able to render the same components with different props over time?
A quick google search turned up these libraries:
https://github.com/lang-ai/react-pdfs https://github.com/diegomura/react-pdf https://www.npmjs.com/package/generate-pdf https://www.npmjs.com/package/react-pdf
And this article:
https://medium.com/@tomsp/server-side-render-pdfs-with-blaze-and-phantomjs-in-meteor-34b8b63522c
Upvotes: 0
Reputation: 1292
You must use ref in react, this is react way to do it :
class Com extends Component {
constructor(props){
super(props);
this.createRef = this.createRef.bind(this);
}
createRef(elem){
//use elem and get the html element.
}
render() {
return (
<div ref={(elem) => this.createRef(elem)} >
<span>hey</span>
</div>
);
}
}
Upvotes: 0