Business Card Sheet
Business Card Sheet
Section titled “Business Card Sheet”An A4 sheet of eight print-ready business cards arranged in a 2 × 4 grid, with cut marks at each corner to guide trimming.
How it works
Section titled “How it works”Each card occupies a fixed slot on the page. A helper function translates
to the card’s origin and draws all elements relative to (0, 0) within
that slot, so the layout code is written once and stamped eight times.
The card face is split into two zones: a coloured top strip (company name and role title) and a white lower area (contact details). A thin hairline divider separates the zones.
Techniques shown
Section titled “Techniques shown”- Tiling layout — one drawing function called in a loop with offset transforms rather than duplicating coordinates.
- Cut marks — short lines extending beyond each card corner, drawn in a light grey that won’t show on a white background.
- Compact contact block — consistent leading and alignment for multi-line text without any text-flow engine.
- Colour from hex — accent colour converted from a CSS hex string to
pdf-lib
rgb()values so it can be driven by a colour picker parameter.
Parameters
Section titled “Parameters”| Parameter | Effect |
|---|---|
| Name | Printed in bold on every card |
| Job title | Subtitle below the name |
| Company | Shown in the coloured header strip |
| Email / Phone / Website | Contact details in the lower section |
| Accent colour | Background of the header strip |
"use strict";
const { PDFDocument, StandardFonts, rgb } = require('pdf-lib');
const W = 595, H = 842; // A4 portraitconst CW = 242, CH = 136; // card dimensions (approx 85 × 48 mm at 72 dpi)const COLS = 2, ROWS = 4;const GAP_X = (W - COLS * CW) / (COLS + 1);const GAP_Y = (H - ROWS * CH) / (ROWS + 1);const STRIP = 50; // height of coloured header stripconst MARK = 6; // cut-mark length
function getParameterDefinitions() { return [ { name: 'name', type: 'text', default: 'Alex Morgan', caption: 'Name' }, { name: 'jobtitle',type: 'text', default: 'Senior Software Engineer', caption: 'Job title' }, { name: 'company', type: 'text', default: 'Acme Corp', caption: 'Company' }, { name: 'phone', type: 'text', default: '+44 7700 900123', caption: 'Phone' }, { name: 'website', type: 'text', default: 'www.acmecorp.example', caption: 'Website' }, { name: 'accent', type: 'color', default: '#0f3460', caption: 'Accent colour' }, ];}
function hexToRgb(hex) { var r = parseInt(hex.slice(1, 3), 16) / 255; var g = parseInt(hex.slice(3, 5), 16) / 255; var b = parseInt(hex.slice(5, 7), 16) / 255; return rgb(r, g, b);}
async function main({ name, jobtitle, company, email, phone, website, accent }) { const doc = await PDFDocument.create(); const bold = await doc.embedFont(StandardFonts.HelveticaBold); const reg = await doc.embedFont(StandardFonts.Helvetica); const page = doc.addPage([W, H]);
const accentClr = hexToRgb(accent); const white = rgb(1, 1, 1); const dark = rgb(0.12, 0.12, 0.12); const muted = rgb(0.45, 0.45, 0.45); const cutClr = rgb(0.75, 0.75, 0.75);
function drawCard(ox, oy) { // Coloured header strip page.drawRectangle({ x: ox, y: oy + CH - STRIP, width: CW, height: STRIP, color: accentClr });
// Company name in strip const compW = bold.widthOfTextAtSize(company, 11); page.drawText(company, { x: ox + (CW - compW) / 2, y: oy + CH - STRIP + (STRIP - 11) / 2 + 2, size: 11, font: bold, color: white, });
// White card body page.drawRectangle({ x: ox, y: oy, width: CW, height: CH - STRIP, color: white });
// Name page.drawText(name, { x: ox + 14, y: oy + CH - STRIP - 20, size: 13, font: bold, color: dark });
// Job title page.drawText(jobtitle, { x: ox + 14, y: oy + CH - STRIP - 34, size: 8.5, font: reg, color: accentClr });
// Divider page.drawLine({ start: { x: ox + 14, y: oy + CH - STRIP - 42 }, end: { x: ox + CW - 14, y: oy + CH - STRIP - 42 }, thickness: 0.4, color: rgb(0.85, 0.85, 0.85), });
// Contact lines var cy = oy + CH - STRIP - 58; [email, phone, website].forEach(function(line) { page.drawText(line, { x: ox + 14, y: cy, size: 8, font: reg, color: muted }); cy -= 14; });
// Card border page.drawRectangle({ x: ox, y: oy, width: CW, height: CH, borderColor: rgb(0.88, 0.88, 0.88), borderWidth: 0.3, });
// Cut marks at each corner var corners = [ { x: ox, y: oy }, { x: ox + CW, y: oy }, { x: ox, y: oy + CH }, { x: ox + CW, y: oy + CH }, ]; corners.forEach(function(c) { var dx = c.x === ox ? -1 : 1; var dy = c.y === oy ? -1 : 1; page.drawLine({ start: { x: c.x + dx * 2, y: c.y }, end: { x: c.x + dx * (2 + MARK), y: c.y }, thickness: 0.3, color: cutClr }); page.drawLine({ start: { x: c.x, y: c.y + dy * 2 }, end: { x: c.x, y: c.y + dy * (2 + MARK) }, thickness: 0.3, color: cutClr }); }); }
for (var row = 0; row < ROWS; row++) { for (var col = 0; col < COLS; col++) { var ox = GAP_X + col * (CW + GAP_X); var oy = H - GAP_Y - (row + 1) * CH - row * GAP_Y; drawCard(ox, oy); } }
return doc;}
module.exports = { main, getParameterDefinitions };