我下载了jspdf 1.2.60 从我的 html 表中生成包含文本的 pdf。其中一列包含用户输入的注释,因此可能会非常大,因为页面正在运行文本。
在搜索中我找到了this然而
var splitTitle = doc.splitTextToSize(reportTitle, 180);
在 api 中不再可用。 (我查看了 jspdf.js 文件)。
我该如何解决这个问题?
任何想法都会受到赞赏。
最佳答案
下面的代码将对您的问题有用。它将完美地满足您的要求。
var lMargin=15; //left margin in mm
var rMargin=15; //right margin in mm
var pdfInMM=210; // width of A4 in mm
function getPDF() {
var doc = new jsPDF("p","mm","a4");
var paragraph="Apple's iPhone 7 is officially upon us. After a week of pre-orders, the latest in the iPhone lineup officially launches today.\n\nEager Apple fans will be lining up out the door at Apple and carrier stores around the country to grab up the iPhone 7 and iPhone 7 Plus, while Android owners look on bemusedly.\n\nDuring the Apple Event last week, the tech giant revealed a number of big, positive changes coming to the iPhone 7. It's thinner. The camera is better. And, perhaps best of all, the iPhone 7 is finally water resistant.\n\nStill, while there may be plenty to like about the new iPhone, there's plenty more that's left us disappointed. Enough, at least, to make smartphone shoppers consider waiting until 2017, when Apple is reportedly going to let loose on all cylinders with an all-glass chassis design.";
var lines =doc.splitTextToSize(paragraph, (pdfInMM-lMargin-rMargin));
doc.text(lMargin,20,lines);
doc.save('Generated.pdf');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.0.272/jspdf.debug.js"></script>
<button id="getPDF" onclick="getPDF()">Get PDF</button>
对于 splitTextToSize
:
正如您所说,jspdf 1.2.60 api 中不存在 doc.splitTextToSize(text,size)
。
原因:doc.splitTextToSize(text,size)
存在于 jspdf 1.2.60 lib 中,但不 存在于 jspdf 中.js。它存在于 jspdf.debug.js 及其缩小版 jspdf.min.js 中。这就是您在 jspdf.js
中找不到 splitTextToSize
的原因。你在错误的文件中查找它。您可以将 jspdf.js
替换为 jspdf.debug.js
,您的代码将完美运行。
jspdf.debug.js and jspdf.min.js are present in
\jsPDF-master\dist
location in api zip file.
注意:我已经为您提供了将长段落分成多行的代码。在这里,我在 180 毫米(210 毫米-15 毫米-15 毫米)之后分割线,如果文本超过 180 毫米,那么文本将换行。您可以根据您的用途更改参数。
以下截图供您引用,其中包含 jspdf.debug.js
中的 splitTextToSize
函数定义。
关于javascript - 如何在jsPDF中换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39266192/