#!/usr/bin/env ts-node /** * Generate PDF Business Cards and convert to CMYK */ import * as fs from 'fs'; import * as path from 'path'; import * as React from 'react'; import { renderToBuffer } from '@react-pdf/renderer'; import { execSync } from 'child_process'; import { PDFBusinessCard, PersonData } from '../lib/pdf-business-card'; import QRCode from 'qrcode'; const CONFIG = { outputDir: path.join(process.cwd(), 'public/downloads/business-cards'), logoWhite: path.join(process.cwd(), 'public/logo-white.png'), logoBlue: path.join(process.cwd(), 'public/logo-blue.png'), logoFull: path.join(process.cwd(), 'public/logo-full.png'), truckImage: path.join(process.cwd(), 'public/truck.png'), }; const PEOPLE: PersonData[] = [ { name: 'Michael Bodemer', role: 'Managing Director', phone: '+49 171 785 2420', email: 'michael.bodemer@klz-cables.com', }, { name: 'Klaus Mintel', role: 'Managing Director', phone: '+49 151 1775 2873', email: 'klaus.mintel@klz-cables.com', }, ]; function convertToCMYK(inputPath: string, outputPath: string) { console.log(`- Converting to CMYK via Ghostscript...`); try { const gsCommand = `gs -dSAFER -dBATCH -dNOPAUSE -dNOCACHE -sDEVICE=pdfwrite -sColorConversionStrategy=CMYK -dProcessColorModel=/DeviceCMYK -dAutoFilterColorImages=false -dColorImageFilter=/FlateEncode -sOutputFile="${outputPath}" "${inputPath}"`; execSync(gsCommand, { stdio: 'pipe' }); console.log(`✓ CMYK Print File generated: ${path.basename(outputPath)}`); } catch (err: any) { console.error(`❌ Ghostscript CMYK conversion failed! Is ghostscript (gs) installed?`); console.error(err.message); } } async function generateBusinessCards() { if (!fs.existsSync(CONFIG.outputDir)) { fs.mkdirSync(CONFIG.outputDir, { recursive: true }); } for (const person of PEOPLE) { console.log(`\nGenerating Business Card for: ${person.name}`); // Generate VCard QR Code const lastName = person.name.split(' ').slice(1).join(' '); const firstName = person.name.split(' ')[0]; const vcard = `BEGIN:VCARD VERSION:3.0 N:${lastName};${firstName};;; FN:${person.name} ORG:KLZ Vertriebs GmbH TITLE:${person.role} TEL;TYPE=WORK,VOICE:${person.phone} EMAIL;TYPE=PREF,INTERNET:${person.email} URL:https://klz-cables.com END:VCARD`; const qrCodeDataUrl = await QRCode.toDataURL(vcard, { width: 400, margin: 0, color: { dark: '#000000', light: '#ffffff' }, }); const safeName = person.name.replace(/\s+/g, '_'); const rgbFileName = `KLZ_BusinessCard_${safeName}_RGB.pdf`; const cmykFileName = `KLZ_BusinessCard_${safeName}_CMYK_Print.pdf`; const rgbPath = path.join(CONFIG.outputDir, rgbFileName); const cmykPath = path.join(CONFIG.outputDir, cmykFileName); console.log(`- Rendering React-PDF layout...`); const buffer = await renderToBuffer( React.createElement(PDFBusinessCard, { person, logoWhitePath: CONFIG.logoWhite, logoBluePath: CONFIG.logoBlue, logoFullPath: CONFIG.logoFull, truckImagePath: CONFIG.truckImage, qrCodeDataUrl, }), ); fs.writeFileSync(rgbPath, buffer); console.log(`✓ RGB Master generated: ${rgbFileName}`); convertToCMYK(rgbPath, cmykPath); } } async function main() { console.log('--- KLZ Business Card Generator ---'); const start = Date.now(); try { await generateBusinessCards(); console.log(`\n✅ Finished in ${((Date.now() - start) / 1000).toFixed(2)}s`); } catch (error) { console.error('\n❌ Fatal error:', error); process.exit(1); } } main().catch(console.error);