我需要打印多行段落,并使用jsPDF缩进第一行。
我使用选项textIndent使用.splitTextToSize函数分割了文本。 然后使用.text呈现结果。参见下面的代码:
doc = mbjsPDF({
orientation: 'portrait',
unit: 'mm',
format: 'a4'
});
var text = "To be, or not to be, that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune";
var textTab = doc.splitTextToSize(text, 100, {textIndent: 30});
doc.text(10, 20, textTab);
doc.save('test.pdf');
实际结果:
To be, or not to be, that is
the question: Whether 'tis nobler in the
mind to suffer The slings and arrows of
outrageous fortune
预期结果:(我希望第一行移动30mm)
To be, or not to be, that is
the question: Whether 'tis nobler in the
mind to suffer The slings and arrows of
outrageous fortune
答案 0 :(得分:0)
textIndent用于行之间的间隔,这确实令人困惑! 您可以将第一行设置为单独的文本组件。比您可以更改Text类的x参数。
答案 1 :(得分:0)
不确定option textIndent
,但是如果只有一个段落,则可以在该段落的开头添加一个制表符。
var textTab = doc.splitTextToSize(' ' + text, 100);
我主要使用jsPDF将html转换为PDF。如果您只想转换纯文本,也许您也可以考虑使用pdfmake。它具有自动分页符,易于添加页边距等。
<script src="http://pdfmake.org/build/pdfmake.min.js"></script>
<script src="http://pdfmake.org/build/vfs_fonts.js"></script>
<script>
function print() {
// open the PDF in a new window
pdfMake.createPdf(dd).open();
}
var dd = {
content: [{
leadingIndent: 30,
lineHeight: 1.5, // optional
text: "To be, or not to be, that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune"
}]
}
</script>