I am beginner in react-redux.
I trying create a function like exporting a html text to pdf with Javascript and it works with html, but when I apply it to react component, it doesn't work.
This is my code:
import React from 'react';
class App extends React.Component {
constructor(props){
super(props);
this.pdfToHTML=this.pdfToHTML.bind(this);
}
pdfToHTML(){
var pdf = new jsPDF('p', 'pt', 'letter');
var source = $('#HTMLtoPDF')[0];
var specialElementHandlers = {
'#bypassme': function(element, renderer) {
return true
}
};
var margins = {
top: 50,
left: 60,
width: 545
};
pdf.fromHTML (
source // HTML string or DOM elem ref.
, margins.left // x coord
, margins.top // y coord
, {
'width': margins.width // max width of content on PDF
, 'elementHandlers': specialElementHandlers
},
function (dispose) {
// dispose: object with X, Y of the last line add to the PDF
// this allow the insertion of new lines after html
pdf.save('html2pdf.pdf');
}
)
}
render() {
return (
<div>
<div classID="HTMLtoPDF">
<center>
<h2>HTML to PDF</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing </p>
</center>
</div>
<button onClick={this.pdfToHTML}>Download PDF</button>
</div>
);
}
}
export default App;
Javascript with HTML: https://www.youtube.com/watch?v=HVuHr-Q7HEs
React Generate PDF Report, Export HTML to PDF Tutorial. 1 Step 1 – Create React App. 2 Step 2 – Install Bootstrap 4 Package. 3 Step 3 – Install Print PDF Library React. 4 Step 4 – Create HTML and PDF Component. 5 Step 5 – Add PDF Component in App.js. More items
This is where react-pdf comes in. It is an open source, easy-to-use library that allows developers to generate PDF documents in a React environment. In this article, you will learn the basics of the react-pdf library. We will cover the following concepts: To install the react-pdf package, run the following terminal command:
You can now run the React application to the HTML to PDF working in action. Execute the following npm command to run the react app It will run the application to the browser at default port 3000 at the following URL
You can also try "React on the fly pdf" which will generate pdf from HTML. This plugin uses html2canvas and jspdf under the hood to generate the pdf document. This plugin supports multiple pages, header and footer. I like to warn you that it won't generate "real" pdf. Meaning real pdf should be vector.
React don't have "classID" property in html tags, it's passed to div as props, which will never be resolved. className was only implemented because it's reserved word in JS, perhabs you need to replace your "classID" by only "id" property and it will work
P.s. JQuery is bad practice when all what you need is DOM manipulation. javascript have document.getElementById() and dependency is not needed
P.p.s. small tip for you is "pdfToHTML(){}" can be replaced to lambda as "pdfToHTML = () => {}", and your function will have "this" from class instance, binding will be removed and constructor will become useless.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With