如何在jsPDF中包装单词?

Ham*_*Zia 8 html javascript pdf pdf-generation jspdf

我下载了jspdf 1.2.60来生成包含html表格文本的pdf.其中一列包含用户输入的注释,因此可能非常大,因为文本正在运行页面.

在淘我发现不过

var splitTitle = doc.splitTextToSize(reportTitle, 180);
Run Code Online (Sandbox Code Playgroud)

在api中不再可用.(我查看了jspdf.js文件).

我该如何解决这个问题?

任何想法都表示赞赏.

Abh*_*eet 21

下面的代码对您的问题很有用.它将完美地满足您的要求.

    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');
    }
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

用于splitTextToSize:

正如你所说doc.splitTextToSize(text,size),jspdf 1.2.60 api中没有.

原因:它doc.splitTextToSize(text,size)存在于jspdf 1.2.60 lib中,但不存在jspdf.js中.它存在于jspdf.debug.js及其缩小的vesion jspdf.min.js中.这就是你没有找到splitTextToSize你的原因jspdf.js.你在错误的文件中查找它.您可以取代你jspdf.jsjspdf.debug.js你的代码将很好地工作.

jspdf.debug.js和jspdf.min.js存在\jsPDF-master\dist于api zip文件中的位置.

注意:我已经为您提供了用于在多行中拆分长段的代码.这里我在180mm(210mm-15mm-15mm)后分割线,如果文字超过180mm,那么文字将以换行方式进行.您可以根据用途更改参数.

下面是您的参考屏幕截图,其中包含splitTextToSize函数定义jspdf.debug.js.

在此输入图像描述 如果您需要任何帮助,请告诉我.快乐的编码......!


Sar*_*ran 4

它只是工作没有任何缺陷。证明:

function createPdf() {
	var doc = new jsPDF('p','in','letter')
	var loremipsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus id eros turpis. Vivamus tempor urna vitae sapien mollis molestie. Vestibulum in lectus non enim bibendum laoreet at at libero. Etiam malesuada erat sed sem blandit in varius orci porttitor. Sed at sapien urna. Fusce augue ipsum, molestie et adipiscing at, varius quis enim. Morbi sed magna est, vel vestibulum urna. Sed tempor ipsum vel mi pretium at elementum urna tempor. Nulla faucibus consectetur felis, elementum venenatis mi mollis gravida. Aliquam mi ante, accumsan eu tempus vitae, viverra quis justo.\n\nProin feugiat augue in augue rhoncus eu cursus tellus laoreet. Pellentesque eu sapien at diam porttitor venenatis nec vitae velit. Donec ultrices volutpat lectus eget vehicula. Nam eu erat mi, in pulvinar eros. Mauris viverra porta orci, et vehicula lectus sagittis id. Nullam at magna vitae nunc fringilla posuere. Duis volutpat malesuada ornare. Nulla in eros metus. Vivamus a posuere libero.'

        // This line works. Try generating PDF.
	lines = doc.splitTextToSize(loremipsum, 7.5)

	doc.text(0.5, 0.5, lines)
	doc.save('Test.pdf')
}
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>
<head>
	<title>PDF Test</title>
</head>
<body>
	<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.2.60/jspdf.min.js"></script>
	<button onclick="createPdf()">Create PDF</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

您也可以在最新版本 1.2.61 中看到相同的结果。因此,显然是其他原因导致了错误

编辑:这里我使用过in,所以如果文本超过7.5in ,它就会换行。如果您使用不同的设备,请妥善处理,否则可能会产生问题