https://github.com/mp70/pdf-lib-draw-table
pdf-lib based tool for drawing tables on new or existing PDFs with TS/JS, server or client side. Simple playground - https://pdf-lib-table-demo.vercel.app/
https://github.com/mp70/pdf-lib-draw-table
nodejs pdf pdf-editing pdf-generation pdf-lib react
Last synced: 3 months ago
JSON representation
pdf-lib based tool for drawing tables on new or existing PDFs with TS/JS, server or client side. Simple playground - https://pdf-lib-table-demo.vercel.app/
- Host: GitHub
- URL: https://github.com/mp70/pdf-lib-draw-table
- Owner: MP70
- License: mit
- Created: 2023-03-18T17:08:15.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-13T22:32:58.000Z (12 months ago)
- Last Synced: 2025-03-07T16:54:32.599Z (3 months ago)
- Topics: nodejs, pdf, pdf-editing, pdf-generation, pdf-lib, react
- Language: TypeScript
- Homepage: https://mp70.github.io/pdf-lib-draw-table/modules.html
- Size: 576 KB
- Stars: 17
- Watchers: 2
- Forks: 2
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Beta - pdf-lib-draw-table
[](https://codecov.io/gh/MP70/pdf-lib-draw-table)[](https://github.com/MP70/pdf-lib-draw-table/actions/workflows/runTests.yml)
[](https://mp70.github.io/pdf-lib-draw-table)
[](https://mp70.github.io/pdf-lib-draw-table)
#### A library for drawing tables in PDFs using pdf-lib.**Table of Contents**
- [Installation](#installation)
- [Usage](#usage)
- [Example](#example)
- [Documentation](#documentation)
- [License](#license)## Installation
```sh
npm install pdf-lib-draw-table-beta
```If you don't already have pdf-lib then
```sh
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](https://mp70.github.io/pdf-lib-draw-table/interfaces/DrawTableOptions.html), we just show a couple here. You can also pass us a [JSON table](https://mp70.github.io/pdf-lib-draw-table/interfaces/TableObject.html) 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](https://mp70.github.io/pdf-lib-draw-table/types/Image.html),
[Link](https://mp70.github.io/pdf-lib-draw-table/types/Link.html),
[CustomSyledText](https://mp70.github.io/pdf-lib-draw-table/interfaces/CustomStyledText.html)
**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](https://github.com/Hopding/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