async generateMultiplePackageSlips(orders: OrderEntity[]) { const pdf = new jsPdf({…
- User
async generateMultiplePackageSlips(orders: OrderEntity[]) { const pdf = new jsPdf({ orientation: 'portrait', compress: true, });
const logo = await readFile( path.resolve(__dirname, '../assets/logo.png'), ); pdf.addImage( Buffer.from(logo).toString('base64'), 'PNG', pdf.internal.pageSize.width / 2 - 12.5, 10, 25, 25, ); pdf.setFontSize(32).text( 'Packing Slip', pdf.internal.pageSize.width / 2, 50, { align: 'center', }, ); pdf.setFontSize(12); const addText = (inputValue: string, x: number, y: number) => { const arrayOfNormalAndBoldText = inputValue.split('**'); arrayOfNormalAndBoldText.map((text, i) => { pdf.setFont(pdf.getFont().fontName, 'bold'); if (i % 2 === 0) { pdf.setFont(pdf.getFont().fontName, 'normal'); } pdf.text(text, x, y); x = x + pdf.getTextWidth(`${text}${i > 0 ? ' ' : ''}`); }); }; const headerStartLine = 64; let lineHeight = 6; let currentPageY = headerStartLine; orders.forEach((order) => { // pdf.addPage(); const spaceRequired = 64; if ( currentPageY + spaceRequired > pdf.internal.pageSize.height - 20 ) { currentPageY += 5; pdf.addPage(); } addText(`Order Number: ${order.wordpressId}`, 20, headerStartLine); addText( `Customer Name: **${order.deliveryName}**`, 20, currentPageY + lineHeight, ); addText( order.customerHeight ? `Customer Height: **${order.customerHeight}cm**` : '', 20, currentPageY + lineHeight * 3, ); addText( order.customerWeight ? `Customer Weight: **${order.customerWeight}kg**` : '', 80, currentPageY + lineHeight * 3, ); pdf.setDrawColor('#828282'); lineHeight = 8; const drawSquare = ( x1: number, y1: number, x2: number, y2: number, ) => { pdf.line(x1, y1, x1, y2); pdf.line(x1, y2, x2, y2); pdf.line(x2, y2, x2, y1); pdf.line(x2, y1, x1, y1); }; const addTable = (y: number, item: IOrderItem) => { const lines = []; let jacketSize = ''; let trousersSize = ''; let shirtSize = ''; let kidsSize = ''; let suitCondition = ''; switch (item.category) { case 'suits and tuxedos': case 'kids collection': case 'suit sale': case 'kids collection sale': case 'clearance suits sale': jacketSize = item.meta['Size of Jacket/Shirt'] ?? item.meta['Size of Jacket'] ?? null; trousersSize = item.meta['Size of Trousers'] ?? null; suitCondition = item.meta['Suit Condition'] ?? null; shirtSize = item.meta['Size of Jacket/Shirt'] ?? item.meta['Size of Shirt'] ?? null; kidsSize = item.meta['Size of Kids'] ?? null; if (kidsSize) { lines.push( `Kids Size: ${ kidsSize ? `**${kidsSize}**` : '' }`, ); } if (jacketSize) { lines.push( `Jacket Size: ${ jacketSize ? `**${jacketSize}**` : '' }`, ); } if (trousersSize) { lines.push( `Trousers Size: ${ trousersSize ? `**${trousersSize}**` : '' }`, ); } if (shirtSize) { lines.push( `Shirt Size: ${ shirtSize ? `**${shirtSize}**` : '' }`, ); } if (suitCondition) { lines.push( `Suit Condition: ${ suitCondition ? `**${suitCondition}**` : '' }`, ); } if (item.packageDescription) { const str = item.packageDescription; const regex = /(a|the|includes)\s+/gi; const arr = str .split(/(,|\s+and\s+)/) .map((subItem: string) => subItem.replace(regex, '').trim(), ) .filter( (subItem: string) => !/tux|suit/i.test(subItem), ) .filter( (subItem) => subItem !== ',' && subItem !== 'and', ); arr.forEach((subItem) => { if ( subItem.includes('Tie') || subItem.includes('Bowtie') ) { const color = subItem .replace('Bowtie', '') .replace('Tie', '') .trim(); if (subItem.includes('Tie')) { lines.push(`Tie: **${color}**`); } else { lines.push(`Bowtie: **${color}**`); } } if (subItem.includes('Vest')) { const color = subItem .replace('Vest', '') .trim(); lines.push(`Vest: **${color}**`); } if (subItem.includes('Belt')) { const color = subItem .replace('Belt', '') .trim(); lines.push(`Belt: **${color}**`); } if (subItem.includes('Pocket Square')) { const color = subItem .replace('Pocket Square', '') .trim(); lines.push(`Pocket Square: **${color}**`); } }); } else { lines.push('Tie/Bowtie'); lines.push('Vest'); lines.push('Belt'); lines.push('Pocket Square'); } break; case 'shirt': break; case 'jacket': } const k = 0.3; const lastY = y + lineHeight * (lines.length + 1) + lineHeight * k; const endPoint = pdf.internal.pageSize.height - 20; if (lastY > endPoint) { pdf.addPage(); y = 20; } for ( let i = 0; i < lines.length + (lines.length > 0 ? 3 : 2); i++ ) { pdf.line( 20, y + lineHeight * (i - 1) + lineHeight * k, pdf.internal.pageSize.width - 20, y + lineHeight * (i - 1) + lineHeight * k, ); } pdf.line( 20, y + lineHeight * -1 + lineHeight * k, 20, y + lineHeight * (lines.length > 0 ? lines.length + 1 : 0) + lineHeight * k, ); pdf.line( pdf.internal.pageSize.width - 20, y + lineHeight * -1 + lineHeight * k, pdf.internal.pageSize.width - 20, y + lineHeight * (lines.length > 0 ? lines.length + 1 : 0) + lineHeight * k, ); const squareSize = lineHeight * 0.5; const header = [`**${item.title}**`]; if (item.meta['Package Name']) { header.push(`Package - **${item.meta['Package Name']}**`); } if (item.meta['Backup Suit']) { header.push( `Backup Suit - **${item.meta['Backup Suit']}**`, ); } if (item.category === 'shirt' && item.meta['Size of Shirt']) { header.push(`Size - **${item.meta['Size of Shirt']}**`); } if ( item.category === 'new shirt sale' && item.meta['Size of New Shirt Sale'] ) { header.push( `Size - **${item.meta['Size of New Shirt Sale']}**`, ); } if (item.category === 'jacket' && item.meta['Size of Jacket']) { header.push(`Size - **${item.meta['Size of Jacket']}**`); } if ( item.category === 'new jacket sale' && item.meta['Size of New Jacket Sale'] ) { header.push( `Size - **${item.meta['Size of New Jacket Sale']}**`, ); } if ( item.category === 'jacket sale' && item.meta['Size of Jacket Sale'] ) { header.push( `Size - **${item.meta['Size of Jacket Sale']}**`, ); } if (item.category === 'shoes' && item.meta['Size of Shoes']) { header.push(`Size - **${item.meta['Size of Shoes']}**`); } if ( item.category === 'shoe sale' && item.meta['Size of Shoe Sale'] ) { header.push(`Size - **${item.meta['Size of Shoe Sale']}**`); } if (item.category === 'vest' && item.meta['Size of Vest']) { header.push(`Size - **${item.meta['Size of Vest']}**`); } if ( item.category === 'new vest sale' && item.meta['Size of New Vest Sale'] ) { header.push( `Size - **${item.meta['Size of New Vest Sale']}**`, ); } if ( item.category === 'vest sale' && item.meta['Size of Vest Sale'] ) { header.push(`Size - **${item.meta['Size of Vest Sale']}**`); } drawSquare( 24, y - lineHeight * k - squareSize * 0.3, 24 + squareSize, y - lineHeight * k + squareSize * 0.7, ); addText(header.join(' '), 24 + squareSize * 1.5, y); if (lines.length !== 0) { addText('Items to include:', 24, y + lineHeight); addText( 'Sent? (Tick when included in order)', 74, y + lineHeight, ); for (let i = 0; i < lines.length; i++) { drawSquare( 24, y + lineHeight * (2 + i) - lineHeight * k - squareSize * 0.3, 24 + squareSize, y + lineHeight * (2 + i) - lineHeight * k + squareSize * 0.7, ); addText( lines[i] as string, 24 + squareSize * 1.5, y + lineHeight * (2 + i), ); } } else { return y + lineHeight + lineHeight * k; } return y + lineHeight * (lines.length + 2) + lineHeight * k; }; // let lastY = 84; let lastY = currentPageY + 50; for (const item of order.items) { lastY = addTable(++lastY, item); } }); return Buffer.from(pdf.output('arraybuffer')); } - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.