Skip to content

Commit

Permalink
Export to Avery 5160 Labels (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
jblayone authored Sep 29, 2022
1 parent 6afab96 commit ee18c58
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/main/ipc/file-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import {
WriteCSVFileModel,
} from '../models/ipc-models';
import { encrypt, decrypt } from './service/encryption';
import { familyCardsPDF, businessCardsPDF, clubCardsPDF } from './service/pdf';
import {
familyCardsPDF,
businessCardsPDF,
clubCardsPDF,
} from './service/pdf/pdf-cards';
import labelPDF from './service/pdf/pdf-labels';

const fs = require('fs');

Expand Down Expand Up @@ -183,6 +188,17 @@ function writeCalendarFile(
});
}

function writeLabelPDF(fileInfo: WriteCalendarFileModel): boolean {
try {
const pdfData = labelPDF(fileInfo.calendar);

fs.writeFileSync(fileInfo.path, pdfData, { encoding: 'utf8' });
return true;
} catch (err) {
return false;
}
}

function writeFamilyCardPDF(fileInfo: WriteCalendarFileModel): boolean {
try {
const { familyCards } = fileInfo.calendar;
Expand Down Expand Up @@ -249,6 +265,9 @@ export default function FileManager() {
ipcMain.handle('files:read-calendar-file', (_event, args) => {
return readCalendarFile(args);
});
ipcMain.handle('files:write-label-file', (_event, args) => {
return writeLabelPDF(args);
});
ipcMain.handle('files:write-family-pdf-file', (_event, args) => {
return writeFamilyCardPDF(args);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/* eslint-disable import/no-duplicates */
/* eslint-disable new-cap */

import sortEvents from '../../services/sort-calendar-events';
import { CardModel } from '../../models/calendar-model';
import sortEvents from '../../../services/sort-calendar-events';
import { CardModel } from '../../../models/calendar-model';

const jsPDF = require('jspdf');
require('jspdf-autotable');
Expand Down
173 changes: 173 additions & 0 deletions src/main/ipc/service/pdf/pdf-labels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable import/no-duplicates */
/* eslint-disable new-cap */

import CalendarModel from '../../../models/calendar-model';

const jsPDF = require('jspdf');
require('jspdf-autotable');

const maxRows = 10;
const maxColumns = 3;
const labelsPerPage = maxRows * maxColumns;

const xOffset = 7;
const yOffset = 18;

const boxWidth = 59;
const boxHeight = 15;

const horizontalSpacing = 14;
const verticalSpacing = 11.5;

const textSpacing = boxHeight / 4;
const labelNumberSize = 8;
const businessContactNameSpacing = 24;

interface LabelModel {
name: string;
phone: string;
address1: string;
address2: string;
labelNumber: string;
optionalName: string;
}

function addDashes(f: string): string {
const f_val = f.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
return f_val;
}

export default function labelPDF(calendar: CalendarModel) {
const doc = new jsPDF.jsPDF({
format: 'letter',
});
doc.setFontSize(9);

const labels: LabelModel[] = [];

for (
let clubIndex = 0;
clubIndex < calendar.clubCards.length;
clubIndex += 1
) {
let name = '';
if (calendar.clubCards[clubIndex].contacts.length === 2) {
name = `${calendar.clubCards[clubIndex].contacts[1].firstName} & ${calendar.clubCards[clubIndex].contacts[0].firstName} ${calendar.clubCards[clubIndex].contacts[0].lastName}`;
} else if (calendar.clubCards[clubIndex].contacts.length === 1) {
name = `${calendar.clubCards[clubIndex].contacts[0].firstName} ${calendar.clubCards[clubIndex].contacts[0].lastName}`;
}
labels.push({
name: `${calendar.clubCards[clubIndex].name}`,
phone: `${addDashes(
calendar.clubCards[clubIndex].contactDetails.homePhone
)}`,
address1: `${calendar.clubCards[clubIndex].address.addressLine}`,
address2: `${calendar.clubCards[clubIndex].address.city}, ${calendar.clubCards[clubIndex].address.province}, ${calendar.clubCards[clubIndex].address.postalCode}`,
labelNumber: `1 of 1`,
optionalName: name,
});
}

for (
let businessIndex = 0;
businessIndex < calendar.businessCards.length;
businessIndex += 1
) {
let name = '';
if (calendar.businessCards[businessIndex].contacts.length === 2) {
name = `${calendar.businessCards[businessIndex].contacts[1].firstName} & ${calendar.businessCards[businessIndex].contacts[0].firstName} ${calendar.businessCards[businessIndex].contacts[0].lastName}`;
} else if (calendar.businessCards[businessIndex].contacts.length === 1) {
name = `${calendar.businessCards[businessIndex].contacts[0].firstName} ${calendar.businessCards[businessIndex].contacts[0].lastName}`;
}
labels.push({
name: `${calendar.businessCards[businessIndex].name}`,
phone: `${addDashes(
calendar.businessCards[businessIndex].contactDetails.homePhone
)}`,
address1: `${calendar.businessCards[businessIndex].address.addressLine}`,
address2: `${calendar.businessCards[businessIndex].address.city}, ${calendar.businessCards[businessIndex].address.province}, ${calendar.businessCards[businessIndex].address.postalCode}`,
labelNumber: `1 of 1`,
optionalName: name,
});
}

for (
let familyIndex = 0;
familyIndex < calendar.familyCards.length;
familyIndex += 1
) {
for (
let la = 0;
la <
parseInt(
calendar.familyCards[familyIndex].order.amountOfCalendarsPurchased,
10
);
la += 1
) {
let { name } = calendar.familyCards[familyIndex];
if (calendar.familyCards[familyIndex].contacts.length === 2) {
name += `,${
calendar.familyCards[familyIndex].contacts[1].firstName !== ''
? ` ${calendar.familyCards[familyIndex].contacts[1].firstName} &`
: ''
} ${calendar.familyCards[familyIndex].contacts[0].firstName}`;
} else if (calendar.familyCards[familyIndex].contacts.length === 1) {
name += `, ${calendar.familyCards[familyIndex].contacts[0].firstName}`;
}
labels.push({
name,
phone: `${addDashes(
calendar.familyCards[familyIndex].contactDetails.homePhone
)}`,
address1: `${calendar.familyCards[familyIndex].address.addressLine}`,
address2: `${calendar.familyCards[familyIndex].address.city}, ${calendar.familyCards[familyIndex].address.province}, ${calendar.familyCards[familyIndex].address.postalCode}`,
labelNumber: `${la + 1} of ${
calendar.familyCards[familyIndex].order.amountOfCalendarsPurchased
}`,
optionalName: '',
});
}
}

for (
let page = 0;
page < Math.ceil(labels.length / labelsPerPage);
page += 1
) {
for (let row = 0; row < maxRows; row += 1) {
const y = yOffset + (boxHeight + verticalSpacing) * row;
for (let column = 0; column < maxColumns; column += 1) {
const x = xOffset + (boxWidth + horizontalSpacing) * column;

const index = page * labelsPerPage + row * maxColumns + column;

if (index < labels.length) {
doc.setFont('helvetica', 'bold');
doc.text(labels[index].name, x, y);
doc.setFontSize(8);
doc.text(
labels[index].optionalName,
x + businessContactNameSpacing,
y + 1 * textSpacing
);
doc.setFontSize(9);
doc.setFont('helvetica', 'normal');
doc.text(labels[index].phone, x, y + 1 * textSpacing);
doc.text(labels[index].address1, x, y + 2 * textSpacing);
doc.text(labels[index].address2, x, y + 3 * textSpacing);

doc.text(
labels[index].labelNumber,
x + boxWidth - labelNumberSize,
y + 3 * textSpacing
);
}
}
}
doc.addPage();
}

return doc.output();
}
2 changes: 2 additions & 0 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ contextBridge.exposeInMainWorld('electron', {
ipcRenderer.invoke('files:read-legacy-file', fileInfo),
writeCalendarFile: (fileInfo: WriteCalendarFileModel) =>
ipcRenderer.invoke('files:write-calendar-file', fileInfo),
writeLabelPDF: (fileInfo: WriteCalendarFileModel) =>
ipcRenderer.invoke('files:write-label-file', fileInfo),
writeFamilyCardPDF: (fileInfo: WriteCalendarFileModel) =>
ipcRenderer.invoke('files:write-family-pdf-file', fileInfo),
writeBusinessCardPDF: (fileInfo: WriteCalendarFileModel) =>
Expand Down
1 change: 1 addition & 0 deletions src/renderer/preload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ declare global {
writeCalendarFile(
fileInfo: WriteCalendarFileModel
): ImportCalendarModel;
writeLabelPDF(fileInfo: WriteCalendarFileModel): string;
writeFamilyCardPDF(fileInfo: WriteCalendarFileModel): string;
writeBusinessCardPDF(fileInfo: WriteCalendarFileModel): string;
writeClubCardPDF(fileInfo: WriteCalendarFileModel): string;
Expand Down
19 changes: 19 additions & 0 deletions src/renderer/screens/dashboard/CardTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,18 @@ export default function CardTable(props: CardTableProps) {
}
}

async function clickSaveLabelPDF() {
const filePath: string =
await window.electron.dialogs.createPDFFileDialog();
if (filePath !== '') {
await window.electron.files.writeLabelPDF({
path: filePath,
password: '',
calendar,
});
}
}

const dateTable = (
values: CardModel,
handleChange:
Expand Down Expand Up @@ -403,6 +415,13 @@ export default function CardTable(props: CardTableProps) {
>
Get Cards PDF
</Button>
<Button
className="card-options-items"
variant="primary"
onClick={() => clickSaveLabelPDF()}
>
Get Labels
</Button>
<Form.Control
className="card-options-items"
style={{ width: '275px' }}
Expand Down

0 comments on commit ee18c58

Please sign in to comment.