0.1.22 • Published 4 months ago

pdf-lib-draw-table-beta v0.1.22

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Beta - pdf-lib-draw-table

codecovTests TypeScript MIT License

A library for drawing tables in PDFs using pdf-lib.

Table of Contents

Installation

npm install pdf-lib-draw-table-beta

If you don't already have pdf-lib then

npm install pdf-lib pdf-lib-draw-table

Example

This is a very simple example (server side as we are using fs). Exactly the same code (minus fs!) will also work client side for example in a react component. The options are fairly extensive for formatting and styling, we just show a couple here. You can also pass us a JSON table if you prefer, either that or array as below is fine. Either can contain any of the following within each cell: string - As in the example below. This is drawn as wrapped text, no word splitting. Image, Link, CustomSyledText OR AN ARRAY OF ANY COMBO OF THE ABOVE If you provide an array we automatically put each item on its own line, as such if you need to manually new line text, this is a way of doing that.

import { PDFDocument } from 'pdf-lib';
import { drawTable } from 'pdf-lib-draw-table';
import fs from 'fs';

(async () => {
  // Create a new PDFDocument
  const pdfDoc = await PDFDocument.create();

  // Add a new page
  const page = pdfDoc.addPage([600, 800]);

  // Define the table data
  const tableData = [
    ['Name', 'Age', 'City'],
    ['Alice', '24', 'New York'],
    ['Bob', '30', 'San Francisco'],
    ['Charlie', '22', 'Los Angeles'],
  ];

  // Set the starting X and Y coordinates for the table
  const startX = 50;
  const startY = 750;

  // Set the table options
  const options = {
    header: {
      hasHeaderRow: true,
      backgroundColor: rgb(0.9, 0.9, 0.9),
    },
  };

  try {
    // Draw the table
    const tableDimensions = await drawTable(pdfDoc, page, tableData, startX, startY, options);

    console.log('Table dimensions:', tableDimensions);

    // Serialize the PDF to bytes and write to a file
    const pdfBytes = await pdfDoc.save();
    fs.writeFileSync('table-example.pdf', pdfBytes);
  } catch (error) {
    console.error('Error drawing table:', error);
  }
})();

Massive thanks to PDF lib for creating a powerful PDF manipulation library.

Also, big thanks to Typedoc for providing the amazing documentation generator tool that makes /docs: https://github.com/TypeStrong/typedoc

0.1.20

4 months ago

0.1.21

4 months ago

0.1.22

4 months ago

0.1.16

4 months ago

0.1.17

4 months ago

0.1.18

4 months ago

0.1.19

4 months ago

0.1.10

4 months ago

0.1.11

4 months ago

0.1.12

4 months ago

0.1.13

4 months ago

0.1.14

4 months ago

0.1.15

4 months ago

0.1.0

4 months ago

0.1.2

4 months ago

0.1.1

4 months ago

0.1.8

4 months ago

0.1.7

4 months ago

0.1.9

4 months ago

0.1.4

4 months ago

0.1.3

4 months ago

0.1.6

4 months ago

0.1.5

4 months ago

0.0.96

4 months ago

0.0.97

4 months ago

0.0.98

4 months ago

0.0.99

4 months ago

0.0.84

4 months ago

0.0.85

4 months ago

0.0.86

4 months ago

0.0.87

4 months ago

0.0.88

4 months ago

0.0.89

4 months ago

0.0.80

4 months ago

0.0.81

4 months ago

0.0.82

4 months ago

0.0.83

4 months ago

0.0.76

4 months ago

0.0.77

4 months ago

0.0.79

4 months ago

0.0.95

4 months ago

0.0.90

4 months ago

0.0.91

4 months ago

0.0.92

4 months ago

0.0.93

4 months ago

0.0.94

4 months ago

0.0.73

4 months ago

0.0.74

4 months ago

0.0.75

4 months ago

0.0.70

4 months ago

0.0.71

4 months ago

0.0.72

4 months ago

0.0.66

4 months ago

0.0.67

4 months ago

0.0.68

4 months ago

0.0.69

4 months ago

0.0.40

5 months ago

0.0.41

5 months ago

0.0.43

5 months ago

0.0.44

5 months ago

0.0.45

5 months ago

0.0.46

5 months ago

0.0.47

5 months ago

0.0.37

5 months ago

0.0.38

5 months ago

0.0.39

5 months ago

0.0.30

5 months ago

0.0.31

5 months ago

0.0.32

5 months ago

0.0.33

5 months ago

0.0.34

5 months ago

0.0.35

5 months ago

0.0.36

5 months ago

0.0.26

5 months ago

0.0.27

5 months ago

0.0.28

5 months ago

0.0.29

5 months ago

0.0.62

5 months ago

0.0.63

5 months ago

0.0.64

5 months ago

0.0.65

5 months ago

0.0.25

5 months ago

0.0.60

5 months ago

0.0.61

5 months ago

0.0.59

5 months ago

0.0.51

5 months ago

0.0.52

5 months ago

0.0.53

5 months ago

0.0.54

5 months ago

0.0.55

5 months ago

0.0.56

5 months ago

0.0.57

5 months ago

0.0.58

5 months ago

0.0.50

5 months ago

0.0.48

5 months ago

0.0.49

5 months ago

0.0.20

5 months ago

0.0.21

5 months ago

0.0.22

5 months ago

0.0.23

5 months ago

0.0.24

5 months ago

0.0.15

5 months ago

0.0.16

5 months ago

0.0.17

5 months ago

0.0.18

5 months ago

0.0.19

5 months ago

0.0.10

5 months ago

0.0.11

5 months ago

0.0.12

5 months ago

0.0.13

5 months ago

0.0.14

5 months ago

0.0.9

5 months ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago