pdf-lib - 确保文本保留在页面上(使用换行和缩小尺寸)

Cam*_*pey 3 typescript

我正在使用 pdf-lib 动态生成一些 PDF 文档。

我添加的文本内容的长度可能有所不同。

有没有办法设置边界框(或类似的东西)以确保我的文本适合该区域?作为下面的示例,我想确保我的文本根据需要自动换行并缩小尺寸以适合特定区域。

populatePage(pdfPage: PDFPage, pdfFont: PDFFont): void {

const { width, height } = pdfPage.getSize();

const text = 'This is a really long bit of text and I want it to remain on the page and resize as needed and not get cut off';

pdfPage.drawText(text, {
  x: width / 2 - (text.length * 10),
  y: height - 200,
  size: 40,
  font: pdfFont,
  color: rgb(0.95, 0.1, 0.1)
});
Run Code Online (Sandbox Code Playgroud)

}

小智 5

drawText 接受 maxWidth,当超过此宽度时,它将包裹您的文本! https://pdf-lib.js.org/docs/api/interfaces/pdfpagedrawtextoptions