drawText accepts a maxWidth, when this width is exceeded it will wrap your text! https://pdf-lib.js.org/docs/api/interfaces/pdfpagedrawtextoptions
Answer from paco on Stack OverflowPDF-LIB
pdf-lib.js.org › docs › api › interfaces › pdfpagedrawtextoptions
PDFPageDrawTextOptions · PDF-LIB
pdf-lib › PDFPageDrawTextOptions · PDFPageDrawTextOptions · blendMode · color · font · lineHeight · maxWidth · opacity · rotate · size · wordBreaks · x · xSkew · y · ySkew · • blendMode? : BlendMode · Defined in api/PDFPageOptions.ts:24 ·
Top answer 1 of 3
8
drawText accepts a maxWidth, when this width is exceeded it will wrap your text! https://pdf-lib.js.org/docs/api/interfaces/pdfpagedrawtextoptions
2 of 3
4
Use maxWidth with wordBreaks. i.e
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),
maxWidth: 500, wordBreaks: [" "]
});
New Releases
newreleases.io › project › github › Hopding › pdf-lib › release › v1.1.1
Hopding/pdf-lib v1.1.1 on GitHub
September 25, 2019 - Support automatic text wrapping for PDFPage.drawText(...) with maxWidth and wordBreaks options (Thanks @multiplegeorges, @GerardSmit, and @vicary!): #173 · #193 · #198 · Handle undefined refs when copying PDF objects (Thanks @mlecoq!): #186 · #187 · #199 · Handle parsing updated PDFs missing EOL after EOF marker: #192 · #200 · Check out latest releases or releases around Hopding/ pdf-lib v1.1.1 ·
GitHub
github.com › Hopding › pdf-lib › issues › 72
Text in multiple lines · Issue #72 · Hopding/pdf-lib
January 28, 2019 - Hi! First of all, thanks a lot for this library. I am trying to paint a long and dynamic text that comes from an API, the text is content of multiple paragraphs that must be painted on one or several pages, the characters have not been i...
Published Jan 28, 2019
UNPKG
unpkg.com › browse › [email protected] › dist › pdf-lib.js.map
pdf-lib
'.' : escapeRegExp(wordBreak));\n }\n var breakRules = escapedRules.join('|');\n return new RegExp(\"(\\\\n|\\\\r)|((.*?)(\" + breakRules + \"))\", 'gm');\n};\nexport var breakTextIntoLines = function (text, wordBreaks, maxWidth, computeWidthOfText) {\n var regex = buildWordBreakRegex(wordBreaks);\n var words = cleanText(text).match(regex);\n var currLine = '';\n var currWidth = 0;\n var lines = [];\n var pushCurrLine = function () {\n if (currLine !== '')\n lines.push(currLine);\n currLine = '';\n currWidth = 0;\n };\n for (var idx = 0, len = words.length; idx < len; idx++) {\n var word = wor
GitHub
github.com › Hopding › pdf-lib › issues › 182
How to do line break while using drawText method · Issue #182 · Hopding/pdf-lib
July 3, 2019 - I am inserting new page into an existing pdf, I did this part. I also added some text with drawText() method, but now I am in need of an automatic line break if the text is too long. Thanks
Published Sep 04, 2019
PDF-LIB
pdf-lib.js.org › docs › api › interfaces › multilinetextlayout
MultilineTextLayout · PDF-LIB
pdf-lib › MultilineTextLayout · MultilineTextLayout · bounds · fontSize · lineHeight · lines · • bounds: LayoutBounds · Defined in api/text/layout.ts:94 · • fontSize: number · Defined in api/text/layout.ts:96 · • lineHeight: number · Defined in api/text/layout.ts:97 ·
Stack Overflow
stackoverflow.com › questions › 47046314 › pdflib-line-breaking-is-not-working-cant-find-the-proper-options-to-set-it
php - PDFLIB: line breaking is not working, can't find the proper options to set it - Stack Overflow
The options I found, for controlling the line break algorithm are here on the PDFlib-9.0.1-API-reference on page 92.
PDF-LIB
pdf-lib.js.org › docs › api › interfaces › drawtextoptions
DrawTextOptions · PDF-LIB
pdf-lib › DrawTextOptions · ↳ DrawLinesOfTextOptions
GitHub
github.com › Hopding › pdf-lib › issues › 390
Getting the dimensions of text generated by page.drawText (incl. wrapping)? · Issue #390 · Hopding/pdf-lib
February 1, 2020 - function drawMultilineText( page: PDFPage, text: string, opts: PDFPageDrawTextOptions & Required< Pick<PDFPageDrawTextOptions, "font" | "size" | "maxWidth" | "lineHeight"> > ): { width: number; height: number } { page.drawText(text, opts); const lines = breakTextIntoLines( text, opts.wordBreaks || page.doc.defaultWordBreaks, opts.maxWidth, (t) => opts.font.widthOfTextAtSize(t, opts.size) ); const lineCount = lines.length; const height = lineCount * opts.lineHeight; const width = Math.max( ...lines.map((l) => opts.font.widthOfTextAtSize(l, opts.size)) ); return { width, height }; } ❤️React with ❤️4bjszd, MatthieuPierce, htran844 and cmgchess ·
Published Mar 26, 2020
PDF-LIB
pdf-lib.js.org › docs › api › classes › pdfdocument
PDFDocument · PDF-LIB
// font=StandardFonts import { StandardFonts } from 'pdf-lib' const font1 = await pdfDoc.embedFont(StandardFonts.Helvetica) // font=string const font2 = await pdfDoc.embedFont('AAEAAAAVAQAABABQRFNJRx/upe...') const font3 = await pdfDoc.embedFont('data:font/opentype;base64,AAEAAA...') // font=Uint8Array import fs from 'fs' const font4 = await pdfDoc.embedFont(fs.readFileSync('Ubuntu-R.ttf')) // font=ArrayBuffer const url = 'https://pdf-lib.js.org/assets/ubuntu/Ubuntu-R.ttf' const ubuntuBytes = await fetch(url).then(res => res.arrayBuffer()) const font5 = await pdfDoc.embedFont(ubuntuBytes)
PDFlib
pdflib.com › pdflib-cookbook › textflow
textflow
PDFlib GmbH(Powered by Apryse, Producer of the Market's Leading PDF SDK)
UNPKG
unpkg.com › [email protected] › src › api › PDFDocument.ts
https://unpkg.com/[email protected]/src/api/PDFDocument.ts
This method accepts * three different value types for the `page` parameter: * * | Type | Behavior | * | ------------------ | ------------------------------------------------------------------------------ | * | `undefined` | Create a new page and insert it into this document | * | `[number, number]` | Create a new page with the given dimensions and insert it into this document | * | `PDFPage` | Insert the existing page into this document | * * For example: * ```js * // page=undefined * const newPage = pdfDoc.insertPage(2) * * // page=[number, number] * import { PageSizes } from 'pdf-lib' * cons
GitHub
github.com › Hopding › pdf-lib › issues › 210
[Feature Request]: `opacity` option for `drawText()` method · Issue #210 · Hopding/pdf-lib
October 3, 2019 - export const rgb = (red: number, ... wordBreaks, options.maxWidth, textWidth); const encodedLines = new Array(lines.length) as PDFHexString[]; for (let idx = 0, len = lines.length; idx < len; idx++) { encodedLines[idx] = font.encodeText(lines[idx]); } I cannot see how i can set opacity value. Does this library ...
Published Oct 03, 2019
PDFlib
pdflib.com › pdflib-cookbook › textflow › avoid_linebreaking
avoid_linebreaking
*/ text_avoid = "For more information about the Giant Wing Paper Plane " + "see our Web site <underline=true avoidbreak>" + "www.kraxi-systems.com<underline=false noavoidbreak>.<nextline>" + "Alternatively, contact us by email via <underline=true " + "charclass={letter {- .}}>[email protected]" + "<charclass={default {- .}} underline=false>. You'll get all " + "information about how to fly the Giant Wing in a thunderstorm " + "as soon as possible."; p.fit_textline("Text with \"charclass\" and \"avoidbreak\" options", 450, 430, "fontname=NotoSerif-Bold fontsize=12"); /* Create the Tex
Snyk
snyk.io › advisor › pdf-lib › functions › pdf-lib.drawtext
How to use the pdf-lib.drawText function in pdf-lib | Snyk
The naming convention // for composite operators is "draw". drawText('This PDF was Created with JavaScript!', { x: 85, y: PAGE_1_HEIGHT - 48, font: HELVETIVA_FONT, size: 24, }), drawText(helveticaFont.encodeText('Olé! - Œ'), { x: PAGE_1_WIDTH * 0.5 - 30, y: PAGE_1_HEIGHT - 48 - 30, font: HELVETIVA_FONT, size: 12, }), // Now we'll draw the Unicorn image on the page's content stream.