How to determine the height of word-wrapped text in pdf-lib?

In pdf-lib, it is possible to auto-word-wrap text using a technique outlined here: /a/77436904/1766230

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { PDFDocument, StandardFonts } from 'pdf-lib';
const doc = await PDFDocument.create();
const font = await doc.embedFont(StandardFonts.TimesRoman);
const page = doc.addPage();
// This will word-wrap fine:
page.drawText(someLongText, { font, size: 20, lineHeight: 25, maxWidth: 200, wordBreaks: [' '] });
// But now what?
// page.moveDown(???);
</code>
<code>import { PDFDocument, StandardFonts } from 'pdf-lib'; const doc = await PDFDocument.create(); const font = await doc.embedFont(StandardFonts.TimesRoman); const page = doc.addPage(); // This will word-wrap fine: page.drawText(someLongText, { font, size: 20, lineHeight: 25, maxWidth: 200, wordBreaks: [' '] }); // But now what? // page.moveDown(???); </code>
import { PDFDocument, StandardFonts } from 'pdf-lib';
const doc = await PDFDocument.create();
const font = await doc.embedFont(StandardFonts.TimesRoman);
const page = doc.addPage();
// This will word-wrap fine:
page.drawText(someLongText, { font, size: 20, lineHeight: 25, maxWidth: 200, wordBreaks: [' '] });
// But now what?
// page.moveDown(???);

The problem is you may need to either…

  • Move the y cursor down so that you can write the next line to the page. (page.moveDown)
  • Detect when a new page might be needed — i.e., the height of this wrapped text would go off the bottom of the page — so that you can split up the text and add a new page (doc.addPage).

But there does not seem to be any functions in the documentation that let’s you determine the height of the word-wrapped text you are drawing to the page. font.heightAtSize(size) just gets the height of the font. What can be done?

0

I figured out a solution after looking into the code for drawText.
There is a utility function called breakTextIntoLines, that we’ll need to run again to get the number of lines.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { breakTextIntoLines } from 'pdf-lib';
//
// ... define font, doc, page ...
//
function wordWrapHeight(text, options) {
const { font, size, lineHeight, wordBreaks, maxWidth } = options;
const getTextWidth = (t) => font.widthOfTextAtSize(t, size);
const lines = breakTextIntoLines(text, wordBreaks, maxWidth, getTextWidth);
return lines.length * lineHeight;
}
const size = 20;
const lineHeight = size * 1.4;
const maxWidth = 200;
const wordBreaks = [' '];
const options = { size, lineHeight, font, maxWidth, wordBreaks };
const distanceDown = wordWrapHeight(text, options);
const y = page.getY();
const endY = y - distanceDown;
// TODO: Determine if the endY would take you off the page.
// If so, then split the lines up based on the amount of room left.
// Otherwise, you do this:
page.drawText(text, options);
page.moveDown(distanceDown);
</code>
<code>import { breakTextIntoLines } from 'pdf-lib'; // // ... define font, doc, page ... // function wordWrapHeight(text, options) { const { font, size, lineHeight, wordBreaks, maxWidth } = options; const getTextWidth = (t) => font.widthOfTextAtSize(t, size); const lines = breakTextIntoLines(text, wordBreaks, maxWidth, getTextWidth); return lines.length * lineHeight; } const size = 20; const lineHeight = size * 1.4; const maxWidth = 200; const wordBreaks = [' ']; const options = { size, lineHeight, font, maxWidth, wordBreaks }; const distanceDown = wordWrapHeight(text, options); const y = page.getY(); const endY = y - distanceDown; // TODO: Determine if the endY would take you off the page. // If so, then split the lines up based on the amount of room left. // Otherwise, you do this: page.drawText(text, options); page.moveDown(distanceDown); </code>
import { breakTextIntoLines } from 'pdf-lib';
//
// ... define font, doc, page ...
//

function wordWrapHeight(text, options) {
    const { font, size, lineHeight, wordBreaks, maxWidth } = options;
    const getTextWidth = (t) => font.widthOfTextAtSize(t, size);
    const lines = breakTextIntoLines(text, wordBreaks, maxWidth, getTextWidth);
    return lines.length * lineHeight;
}

const size = 20;
const lineHeight = size * 1.4;
const maxWidth = 200;
const wordBreaks = [' '];
const options = { size, lineHeight, font, maxWidth, wordBreaks };
const distanceDown = wordWrapHeight(text, options);
const y = page.getY();
const endY = y - distanceDown;
// TODO: Determine if the endY would take you off the page.
// If so, then split the lines up based on the amount of room left.
// Otherwise, you do this:
page.drawText(text, options);
page.moveDown(distanceDown);

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật