-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import PDFDocument from 'pdfkit-table'; | ||
import fs from 'fs'; | ||
|
||
// Unit: 1pt = 1/72 inch = 0.352777239 mm | ||
const MARGIN_IN_PTS = 40; // The margin between the edge of the page and cards | ||
const SPACING_IN_PTS = 35; // The spacing between cards | ||
const CARD_WIDTH_IN_PTS = 243; | ||
const CARD_HEIGHT_IN_PTS = 153; | ||
const CARD_PADDING = 10; | ||
const NB_CARDS_PER_PAGE = 8; | ||
const FONT_NORMAL = '.fonts/Inter-Regular.otf'; | ||
const FONT_BOLD = '.fonts/Inter-Bold.otf'; | ||
|
||
const OC_SVG_LOGO = fs.readFileSync('public/static/images/opencollective-icon.svg', 'utf8'); | ||
const LINK_SVG = fs.readFileSync('public/static/images/external-link.svg', 'utf8'); | ||
|
||
/** | ||
* Generate a PDF using PDFKit and return it as a buffer. | ||
*/ | ||
export default async function handler(req, res) { | ||
// Set response headers | ||
res.setHeader('Cache-Control', 'no-store'); | ||
res.setHeader('Content-Type', 'application/pdf'); | ||
// res.setHeader('Content-Disposition', 'attachment; filename="gift-cards.pdf"'); | ||
|
||
// Generate doc | ||
const doc = new PDFDocument(); | ||
doc.pipe(res); | ||
doc.fillColor('#000000').fontSize(10); | ||
doc.font(FONT_NORMAL); | ||
|
||
doc.table( | ||
{ | ||
title: 'Payment Receipt', | ||
headers: [ | ||
{ label: 'Date', align: 'left', headerColor: '#36bcff' }, | ||
{ label: 'Description', align: 'left', headerColor: '#36bcff' }, | ||
{ label: 'QTY', align: 'left', headerColor: '#36bcff' }, | ||
{ label: 'Unit Price', align: 'left', headerColor: '#36bcff' }, | ||
{ label: 'Tax', align: 'left', headerColor: '#36bcff' }, | ||
{ label: 'Net Amount', align: 'right', headerColor: '#36bcff' }, | ||
], | ||
rows: [ | ||
['2020-01-01', 'Financial contribution to Dark Reader', '1', '$5.00', '0%', '$5.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-06-01', 'TShirt from Babel', '1', '$20.00', '0%', '$20.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
['2021-01-01', 'Financial contribution to Webpack', '1', '$15.00', '0%', '$15.00'], | ||
], | ||
}, | ||
{ | ||
columnsSize: [60, 150, 20, 100, 20, 100], | ||
}, | ||
); | ||
|
||
// Show Subtotal + Total on the bottom right of the table | ||
// Subtotal | ||
doc | ||
.font(FONT_BOLD) | ||
.fontSize(10) | ||
.text('Subtotal', 350, doc.y, { lineBreak: false, align: 'right' }) | ||
.text('$5.00', 490, doc.y, { lineBreak: false, align: 'right' }); | ||
|
||
// Blue box for the total | ||
doc | ||
.rect(325, doc.y + 20, 200, 25) | ||
.fillColor('#F0F8FF') | ||
.fill(); | ||
|
||
// Total | ||
doc | ||
.font(FONT_BOLD) | ||
.fontSize(10) | ||
.fillColor('#000000') | ||
.text('Total', 350, doc.y + 25, { lineBreak: false, align: 'right' }) | ||
.text('$5.00', 490, doc.y, { lineBreak: false, align: 'right' }); | ||
|
||
doc.end(); | ||
} |