How to use @react-pdf/textkit - 9 common examples

To help you get started, we’ve selected a few @react-pdf/textkit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github diegomura / react-pdf / src / elements / Text.js View on Github external
renderText() {
    const { top, left } = this.getAbsoluteLayout();
    const initialY = this.lines[0] ? this.lines[0].box.y : 0;

    // We translate lines based on Yoga container
    this.root.instance.save();
    this.root.instance.translate(
      left + this.padding.left,
      top + this.padding.top - initialY,
    );
    // Perform actual text rendering on document
    PDFRenderer.render(this.root.instance, [this.lines]);
    setLink(this);
    setDestination(this)


    this.root.instance.restore();
  }
  async render() {
github diegomura / react-pdf / src / render / renderText.js View on Github external
const renderText = (ctx, node) => {
  const { top, left } = node.box;
  const paddingTop = R.pathOr(0, ['box', 'paddingTop'], node);
  const paddingLeft = R.pathOr(0, ['box', 'paddingLeft'], node);
  const initialY = node.lines[0] ? node.lines[0].box.y : 0;

  ctx.save();
  ctx.translate(left + paddingLeft, top + paddingTop - initialY);

  PDFRenderer.render(ctx, [node.lines]);
  ctx.restore();

  return node;
};
github diegomura / react-pdf / src / utils / attributedString.js View on Github external
export const getAttributedString = instance => {
  return AttributedString.fromFragments(getFragments(instance));
};
github diegomura / react-pdf / src / svg / layoutText.js View on Github external
const getAttributedString = instance =>
  AttributedString.fromFragments(getFragments(instance));
github diegomura / react-pdf / src / render / renderSvgText.js View on Github external
const renderSpan = (ctx, line, textAnchor) => {
  ctx.save();

  const x = R.pathOr(0, ['box', 'x'], line);
  const y = R.pathOr(0, ['box', 'y'], line);
  const width = lineWidth(line);

  switch (textAnchor) {
    case 'middle':
      ctx.translate(x - width / 2, y);
      break;
    case 'end':
      ctx.translate(x - width, y);
      break;
    default:
      ctx.translate(x, y);
      break;
  }

  for (const run of line.runs) {
    renderRun(ctx, run);
  }
github diegomura / react-pdf / src / layout / index.js View on Github external
import textDecoration from '@react-pdf/textkit/engines/textDecoration';
import scriptItemizer from '@react-pdf/textkit/engines/scriptItemizer';
import wordHyphenation from '@react-pdf/textkit/engines/wordHyphenation';

import fontSubstitution from './fontSubstitution';

const engines = {
  linebreaker,
  justification,
  textDecoration,
  scriptItemizer,
  wordHyphenation,
  fontSubstitution,
};

const engine = layoutEngine(engines);

export default engine;
github diegomura / react-pdf / src / svg / layoutText.js View on Github external
import Font from '../font';
import transformText from '../text/transformText';
import isTextInstance from '../node/isTextInstance';
import fontSubstitution from '../text/fontSubstitution';

const engines = {
  linebreaker,
  justification,
  textDecoration,
  scriptItemizer,
  wordHyphenation,
  fontSubstitution,
};

const engine = layoutEngine(engines);

const layoutOptions = {
  hyphenationCallback: Font.getHyphenationCallback(),
  shrinkWhitespaceFactor: { before: -0.5, after: -0.5 },
};

const getFragments = instance => {
  if (!instance) return [{ string: '' }];

  const fragments = [];

  const {
    fill = 'black',
    fontFamily = 'Helvetica',
    fontWeight,
    fontStyle,
github diegomura / react-pdf / src / elements / Text.js View on Github external
      ...this.lines.map(line => AttributedString.advanceWidth(line)),
    );
github diegomura / react-pdf / src / render / renderSvgText.js View on Github external
const renderRun = (ctx, run) => {
  const runAdvanceWidth = runWidth(run);
  const { font, fontSize, color, opacity } = run.attributes;

  ctx.fillColor(color);
  ctx.fillOpacity(opacity);

  if (font.sbix || (font.COLR && font.CPAL)) {
    ctx.save();
    ctx.translate(0, -run.ascent);

    for (let i = 0; i < run.glyphs.length; i++) {
      const position = run.positions[i];
      const glyph = run.glyphs[i];

      ctx.save();
      ctx.translate(position.xOffset, position.yOffset);

@react-pdf/textkit

An advanced text layout framework

MIT
Latest version published 27 days ago

Package Health Score

92 / 100
Full package analysis

Similar packages