How to use the pdf-lib.drawText function in pdf-lib

To help you get started, we’ve selected a few pdf-lib 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 Hopding / pdf-lib / examples / document_creation / node / index.js View on Github external
// Let's define some RGB colors. Note that these arrays are of the form:
//   [, , ]
// where each color value must be in the range 0.0-1.0. Note that they should
// *not* be in the range 0-255 as they would be if you were writing CSS styles.
const CYAN = [0.25, 1.0, 0.79];
const PURPLE = [0.79, 0.25, 1.0];

// Here, we define the first page's "content stream". A content stream is simply
// a sequence of PDF operators that define what we want to draw on the page.
const contentStream1 = pdfDoc.createContentStream(
  // `drawText` is a "composite" PDF operator that lets us easily draw text on
  // a page's content stream. "composite" just means that it is composed of
  // several lower-level PDF operators. Usually, you'll want to work with
  // composite operators - they make things a lot easier! 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. We'll
  // position it a little bit below the text we just drew, and we'll center it
  // within the page.
  drawImage(UNICORN_JPG, {
    x: PAGE_1_WIDTH * 0.5 - UNICORN_JPG_WIDTH * 0.5,
github Hopding / pdf-lib / examples / document_creation / node / index.js View on Github external
// Here, we define the first page's "content stream". A content stream is simply
// a sequence of PDF operators that define what we want to draw on the page.
const contentStream1 = pdfDoc.createContentStream(
  // `drawText` is a "composite" PDF operator that lets us easily draw text on
  // a page's content stream. "composite" just means that it is composed of
  // several lower-level PDF operators. Usually, you'll want to work with
  // composite operators - they make things a lot easier! 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. We'll
  // position it a little bit below the text we just drew, and we'll center it
  // within the page.
  drawImage(UNICORN_JPG, {
    x: PAGE_1_WIDTH * 0.5 - UNICORN_JPG_WIDTH * 0.5,
    y: PAGE_1_HEIGHT * 0.5,
    width: UNICORN_JPG_WIDTH,
    height: UNICORN_JPG_HEIGHT,
  }),
  // Finally, let's draw an ellipse on the page's content stream. We'll draw it
  // below the image we just drew, and we'll center it within the page. We'll