gpt4 book ai didi

javascript - 向jsPDF添加内容时获取当前x,y位置

转载 作者:行者123 更新时间:2023-11-29 15:23:02 25 4
gpt4 key购买 nike

我正在尝试使用 jsPDF 创建 pdf是否有任何函数可以在将内容写入 pdf 文档时返回当前光标位置?

最佳答案

以下代码片段显示了在添加一行或文本 block 后导出新的 y 位置的可能方法(您可以在 https://parall.ax/products/jspdf 上进行测试):

var doc = new jsPDF()
var text = 'This is a text without real content but with 59 characters.'

var lineHeight = doc.getLineHeight(text) / doc.internal.scaleFactor
var splittedText = doc.splitTextToSize(text, 50)
var lines = splittedText.length // splitted text is a string array
var blockHeight = lines * lineHeight
var yPos = 10
var xPos = 10
doc.text(xPos, yPos, splittedText)
yPos += blockHeight
doc.text(xPos, yPos, '----- This text follows the previous text block.')
yPos += lineHeight
doc.text(xPos, yPos, '----- LineHeight=' + lineHeight + ' / blockHeight=' + blockHeight)
yPos += lineHeight
doc.text(xPos, yPos, '----- doc.internal.scaleFactor = ' + doc.internal.scaleFactor)

如果您更改在 splitTextTosize() 函数中定义为第二个参数的宽度(例如,从 50 到 80),下一行的 y 位置将相应调整。

关于javascript - 向jsPDF添加内容时获取当前x,y位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41979037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com