#javascript #reactjs #jspdf
#javascript #reactjs #jspdf
Вопрос:
Я пытаюсь использовать jspdf в своем приложении react, и в документах I и в типах для jspdf я вижу, что есть функция, вызываемая fromHtml . Но когда я пытаюсь вызвать функцию, я получаю следующую ошибку:
doc.fromHTML is not a function
Может кто-нибудь, пожалуйста, помогите мне?
import { jsPDF } from 'jspdf'
const Prints = () => (
<div>
<h3>Time amp; Materials Statement of Work (SOW)</h3>
<h4>General Information</h4>
<table id='tab_customers' class='table table-striped'>
<colgroup>
<col span='1' />
<col span='1'/>
</colgroup>
<thead>
<tr class='warning'>
<th>SOW Creation Date</th>
<th>SOW Start Date</th>
<th>Project</th>
<th>Last Updated</th>
<th>SOW End Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>Dec 13, 2017</td>
<td>Jan 1, 2018</td>
<td>NM Connect - NMETNMCM</td>
<td>Dec 13, 2017</td>
<td>Dec 31, 2018</td>
</tr>
</tbody>
</table>
<p>
This is a Time and Materials Statement of Work between Northwestern Mutual
Life Insurance Company and Infosys with all general terms and conditions
as described in the current Master Agreement and its related documents
</p>
</div>
)
function generatePdf() {
const doc = new jsPDF( 'p', 'mm', 'a4' )
let index = 0
const htmlToConvert= renderToString(<Prints />)
doc.fromHTML(htmlToConvert)
doc.save('myPdf')
}
Функция generatePdf вызывается при нажатии кнопки
Комментарии:
1. можете ли вы поделиться кодом, который приводит к этой ошибке?
2. добавили код в мой компонент @RanMarciano
Ответ №1:
Попробуйте изменить doc.fromHtml НА doc.html вот так:
function generatePdf() {
const doc = new jsPDF( 'p', 'mm', 'a4' )
let index = 0
const htmlToConvert= renderToString(<Prints />)
doc.html(htmlToConvert, {
callback: function (doc) {
doc.save();
}
});
}
Это вам помогает?