How to use the pdf-lib.drawRectangle 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
// Here, we define the second 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 contentStream2 = pdfDoc.createContentStream(
  // Here we draw the image of Mario on the page's content stream. We'll draw
  // him centered horizontally in the top half of the page.
  drawImage(MARIO_PNG, {
    x: PAGE_2_WIDTH * 0.5 - MARIO_PNG_WIDTH * 0.5,
    y: PAGE_2_HEIGHT * 0.5,
    width: MARIO_PNG_WIDTH,
    height: MARIO_PNG_HEIGHT,
  }),
  // Next we'll draw a Solarized White rectangle with a Solarized Gray border
  // beneath the image of Mario. It will be centered horizontally in the page.
  drawRectangle({
    x: PAGE_2_WIDTH * 0.5 - TEXT_BOX_WIDTH * 0.5,
    y: PAGE_2_HEIGHT * 0.5 - 100,
    width: TEXT_BOX_WIDTH,
    height: 90,
    colorRgb: SOLARIZED_WHITE,
    borderWidth: 4,
    borderColorRgb: SOLARIZED_GRAY,
  }),
  // Now we'll draw three lines of text within the rectangle we just drew. The
  // text will be drawn in the Ubuntu font colored Solarized Gray.
  drawLinesOfText(
    [
      'Here is a picture of Mario',
      'running. It was placed in',
      'this PDF using JavaScript!',
    ],
github Hopding / pdf-lib / examples / document_modification / node / index.js View on Github external
// This step is platform independent. The same code can be used in any
// JavaScript runtime (e.g. Node, the browser, or React Native).
const page1 = pdfDoc
  .createPage([600, 250])
  .addFontDictionary(UBUNTU_FONT, ubuntuFontRef);

// 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,
// which is why we are dividing each color component by 255.
const PURPLE = [119 / 255, 41 / 255, 83 / 255];
const ORANGE = [224 / 255, 90 / 255, 43 / 255];

const contentStream1 = pdfDoc.createContentStream(
  drawRectangle({
    width: 600,
    height: 250,
    colorRgb: PURPLE,
  }),
  drawLinesOfText(
    ['This is the new first page.', 'It was inserted with JavaScript!'],
    {
      x: 30,
      y: 130,
      font: UBUNTU_FONT,
      size: 38,
      colorRgb: ORANGE,
    },
  ),
);