Generate PDF from div



我正在使用jsPDF:

import jsPDF from "jspdf";
const noop = () => {};

const elementRef = React.createRef();
const savePDF = () => {
const doc = new jsPDF({
orientation: "p",
unit: "pt",
format: "letter"
});
const element = elementRef.current;
const margins = {
left: 40,
top: 40,
bottom: 40
};
const options = {
width: 500
};
doc.fromHTML(element, margins.left, margins.top, options, noop, margins);
doc.save("hmw.pdf");
};

在回报中:

<button onClick={savePDF}>Generate PDF</button>
<div ref={elementRef}>12345</div>

然而,我在控制台上得到的只是:

Uncaught TypeError: doc.fromHTML is not a function

如果我删除了那一行,PDF就会被创建,但它会返回空白。

我该如何更正?

fromHTML函数在2.0.0中被html取代。

你可以在这里阅读更多关于他们的问题

看起来你想要的方法可能是doc.html

您可以使用以下代码:-

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$("#btnPrint").live("click", function () {
var divContents = $("#dvContainer").html();
var printWindow = window.open('', '', 'height=400,width=800');
printWindow.document.write('<html><head><title>DIV Contents</title>');
printWindow.document.write('</head><body >');
printWindow.document.write(divContents);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.print();
});
</script>
</head>
<body>
<form id="form1">
<div id="dvContainer">
This content needs to be printed.
</div>
<input type="button" value="Print Div Contents" id="btnPrint" />
</form>
</body>
</html>

最新更新