我是react-redux的初学者。
我尝试创建一个函数,比如使用Javascript将html文本导出为pdf,并且它与html一起使用,但是当我将它应用于反应组件时,它不起作用。
这是我的代码:
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;
带有HTML的JavaScript:https://www.youtube.com/watch?v=HVuHr-Q7HEs
答案 0 :(得分:4)
React在html标签中没有“classID”属性,它作为props传递给div,永远不会被解析。 className只是实现的,因为它是JS中的保留字,perhabs你需要用“id”属性替换你的“classID”并且它会起作用
P.S。当所有你需要的东西都是DOM操作时,JQuery是不好的做法。 javascript有document.getElementById()和不需要依赖
P.p.s。小技巧是“pdfToHTML(){}”可以替换为lambda为“pdfToHTML =()=&gt; {}”,你的函数将从类实例中获得“this”,绑定将被删除,构造函数将变为无用的。
答案 1 :(得分:0)
这是我的方式
- You can use that package in pure javascript file or server
side(Backend)
- When you use it with the ReactJS(Frontend), it doesn't work.
- So I didn't use that.
- With html2canvas and jsPDF, I could build pdf.
- First build component, then save(download) it.