foliojs / pdfkit

A JavaScript PDF generation library for Node and the browser
http://pdfkit.org/
MIT License
9.95k stars 1.16k forks source link

Make wrap accessible externally. #992

Open GeorgeP39 opened 5 years ago

GeorgeP39 commented 5 years ago

Make pdfkit's wrapper accessible externally.

Allow the use of pdfkit's powerful LineWrapper from outside of pdfkit.

Allow access to pdfkit's wrapper from outside the js. I wanted to create multi-line links by creating rectangular annotations over the top of some text. In order to achieve this, I need to know the "rectangles" that describe the lines of text. pdfkit already has a powerful LineWrapperbut I couldn't access it without moving its declaration outside of the function it's currently in.

I want to be able to do something like

var myWrapper = new LineWrapper(doc, {options)});
var wrapRects = myWrapper.wrap(someText, {otherOptions, measure: true});

where measureis an option to have it return the bounding rectangles and wrapRectswill be an array of these rectangles.

blikblum commented 5 years ago

Related to #976

fdb commented 3 years ago

This is what I use now 🙈

  // Make a temporary wrap object by calling "continued" on the text function.
  // Use the _text so it has no effect on the actual document.
  const tmpWrapper = doc._text("", 0, 0, { width: 100, continued: true }, () => {});
  // Extract the LineWrapper object, and its class constructor, from the returned wrapper.
  const LineWrapper = tmpWrapper._wrapper.__proto__.constructor;
  // Construct a new instance with this constructor.
  const wrapper = new LineWrapper(doc, { width: 400, indent: 0 });
  // Wrap the text.
  wrapper.on("line", yourCallback);
  wrapper.wrap(article.body, { width: 400, indent: 0 });