Skip to content

Commit

Permalink
finalized layout
Browse files Browse the repository at this point in the history
  • Loading branch information
rafnarnason committed Oct 8, 2024
1 parent 0e9a72e commit 2e615c9
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ export class EndorsementService {
nationalId,
showName,
}: EndorsementInput) {
// insert 50 thousand rows into endorsements with unique nationalId and this listId 833450be-882a-48ee-97c6-977ba9df3bfd
const listId = '833450be-882a-48ee-97c6-977ba9df3bfd'
const endorsements = []
for (let i = 7; i < 50000; i++) {
let unqiue8lettternatioalid = Math.random().toString(36).substring(2, 10)
endorsements.push({
endorser: unqiue8lettternatioalid,
endorsementListId: listId,
meta: {
fullName: 'Test',
locality: 'Reykjavík',
showName: true,
},
})
}
await this.endorsementModel.bulkCreate(endorsements)

this.logger.info(`Creating resource with nationalId - ${nationalId}`)

// we don't allow endorsements on closed lists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export class EndorsementListService {
}

async create(list: CreateInput) {

if (!list.openedDate || !list.closedDate) {
this.logger.warn('Body missing openedDate or closedDate value.')
throw new BadRequestException([
Expand Down Expand Up @@ -384,132 +385,248 @@ export class EndorsementListService {
// return await getStream.buffer(doc)
// }

// async createDocumentBuffer(
// endorsementList: any,
// ownerName: string,
// ): Promise<Buffer> {
// const doc = new PDFDocument({ margin: 60 })
// const locale = 'is-IS'
// const buffers: Buffer[] = []
// doc.on('data', buffers.push.bind(buffers))
// doc.on('end', () =>
// this.logger.info(
// 'PDF buffer created successfully for list id ' + endorsementList.id,
// { listId: endorsementList.id },
// ),
// )

// const regularFontPath = path.join(
// process.cwd(),
// 'apps/services/endorsements/api/src/assets/ibm-plex-sans-v7-latin-regular.ttf',
// )
// const boldFontPath = path.join(
// process.cwd(),
// 'apps/services/endorsements/api/src/assets/ibm-plex-sans-v7-latin-600.ttf',
// )
// const headerImagePath = path.join(
// process.cwd(),
// 'apps/services/endorsements/api/src/assets/thjodskra.png',
// )
// const footerImagePath = path.join(
// process.cwd(),
// 'apps/services/endorsements/api/src/assets/island.png',
// )

// // Register fonts with absolute paths
// doc.registerFont('Regular', regularFontPath)
// doc.registerFont('Bold', boldFontPath)

// // Dynamically calculate the height of the header image
// const headerImageHeight = 40 // This can be any height you prefer; here it's 40 for demonstration

// // Add header image
// doc.image(headerImagePath, 60, 40, { width: 120 })

// // Adjust the Y position for the text to appear below the image dynamically
// let currentYPosition = 40 + headerImageHeight + 20 // 20px extra padding below the image

// // Title and petition details
// doc
// .font('Bold')
// .fontSize(24)
// .text('Upplýsingar um undirskriftalista', 60, currentYPosition, {
// align: 'left',
// })
// .moveDown()

// // Adjust Y position after title
// currentYPosition = doc.y + 10 // Adjust by adding extra padding if needed

// doc
// .font('Bold')
// .fontSize(14)
// .text('Heiti undirskriftalista: ', { continued: true })
// doc
// .font('Regular')
// .text(endorsementList.title, { align: 'left' })
// .moveDown()

// doc
// .font('Bold')
// .fontSize(14)
// .text('Um undirskriftalista: ', { continued: true })
// doc
// .font('Regular')
// .text(endorsementList.description, { align: 'left' })
// .moveDown()

// // Petition details
// doc.font('Bold').text('Ábyrgðarmaður: ', { continued: true })
// doc.font('Regular').text(ownerName, { align: 'left' })

// doc.font('Bold').text('Fjöldi skráðir: ', { continued: true })
// doc
// .font('Regular')
// .text(endorsementList.endorsements.length.toString(), { align: 'left' })
// .moveDown()

// // Add endorsement signatures table
// doc.moveDown().font('Bold').fontSize(12)
// const dateX = 60
// const nameX = 160
// const localityX = 360

// doc.text('Dags. skráð', dateX, doc.y, { width: 100 })
// doc.text('Nafn', nameX, doc.y, { width: 200 })
// doc.text('Sveitarfélag', localityX, doc.y, { width: 200 })

// endorsementList.endorsements.forEach((endorsement: Endorsement) => {
// // Assuming Endorsement is typed
// if (doc.y > doc.page.height - 100) {
// doc.addPage()
// doc.text('Dags. skráð', dateX, doc.y, { width: 100 })
// doc.text('Nafn', nameX, doc.y, { width: 200 })
// doc.text('Sveitarfélag', localityX, doc.y, { width: 200 })
// }
// doc.moveDown()
// const y = doc.y
// doc.font('Regular').fontSize(10)
// doc.text(endorsement.created.toLocaleDateString(locale), dateX, y, {
// width: 100,
// })
// doc.text(endorsement.meta.fullName || 'Nafn ótilgreint', nameX, y, {
// width: 200,
// })
// doc.text(
// endorsement.meta.locality || 'Sveitafélag ótilgreint',
// localityX,
// y,
// { width: 200 },
// )
// })

// // Add footer image
// doc.image(footerImagePath, 60, doc.page.height - 80, { width: 120 })

// doc.end()
// return await getStream.buffer(doc)
// }

async createDocumentBuffer(
endorsementList: any,
ownerName: string,
): Promise<Buffer> {
const doc = new PDFDocument({ margin: 60 })
const locale = 'is-IS'
const buffers: Buffer[] = []
doc.on('data', buffers.push.bind(buffers))
): Promise<Buffer> {
const doc = new PDFDocument({ margin: 60 });
const locale = 'is-IS';
const buffers: Buffer[] = [];
doc.on('data', buffers.push.bind(buffers));
doc.on('end', () =>
this.logger.info(
'PDF buffer created successfully for list id ' + endorsementList.id,
{ listId: endorsementList.id },
),
)

const regularFontPath = path.join(
process.cwd(),
'apps/services/endorsements/api/src/assets/ibm-plex-sans-v7-latin-regular.ttf',
)
const boldFontPath = path.join(
process.cwd(),
'apps/services/endorsements/api/src/assets/ibm-plex-sans-v7-latin-600.ttf',
)
const headerImagePath = path.join(
process.cwd(),
'apps/services/endorsements/api/src/assets/thjodskra.png',
)
const footerImagePath = path.join(
process.cwd(),
'apps/services/endorsements/api/src/assets/island.png',
)
this.logger.info('PDF buffer created successfully for list id ' + endorsementList.id, { listId: endorsementList.id }),
);

// Register fonts with absolute paths
doc.registerFont('Regular', regularFontPath)
doc.registerFont('Bold', boldFontPath)
const regularFontPath = path.join(process.cwd(), 'apps/services/endorsements/api/src/assets/ibm-plex-sans-v7-latin-regular.ttf');
const boldFontPath = path.join(process.cwd(), 'apps/services/endorsements/api/src/assets/ibm-plex-sans-v7-latin-600.ttf');
const headerImagePath = path.join(process.cwd(), 'apps/services/endorsements/api/src/assets/thjodskra.png');
const footerImagePath = path.join(process.cwd(), 'apps/services/endorsements/api/src/assets/island.png');

// Dynamically calculate the height of the header image
const headerImageHeight = 40 // This can be any height you prefer; here it's 40 for demonstration
doc.registerFont('Regular', regularFontPath);
doc.registerFont('Bold', boldFontPath);

// Add header image
doc.image(headerImagePath, 60, 40, { width: 120 })
const headerImageHeight = 40;
doc.image(headerImagePath, 60, 40, { width: 120 });

let currentYPosition = 40 + headerImageHeight + 20;

// Title and petition details
doc.font('Bold').fontSize(24).text('Upplýsingar um undirskriftalista', 60, currentYPosition, { align: 'left' });
currentYPosition = doc.y + 20; // Adjust vertical space

// "Heiti undirskriftalista" label and value
doc.font('Bold').fontSize(12).text('Heiti undirskriftalista: ', 60, currentYPosition, { align: 'left' });
currentYPosition = doc.y + 5; // Slight spacing between label and value
doc.font('Regular').fontSize(12).text(endorsementList.title || 'string', 60, currentYPosition, { align: 'left' });
currentYPosition = doc.y + 15; // Adjust space before next label

// "Um undirskriftalista" label and value
doc.font('Bold').fontSize(12).text('Um undirskriftalista: ', 60, currentYPosition, { align: 'left' });
currentYPosition = doc.y + 5;
doc.font('Regular').fontSize(12).text(endorsementList.description || 'string', 60, currentYPosition, { align: 'left' });
currentYPosition = doc.y + 15;

// "Opin til" label and value
doc.font('Bold').fontSize(12).text('Opin til: ', 60, currentYPosition, { align: 'left' });
currentYPosition = doc.y + 5;
doc.font('Regular').fontSize(12).text(endorsementList.closedDate.toLocaleDateString(locale), 60, currentYPosition, { align: 'left' });
currentYPosition = doc.y + 15;

// "Fjöldi undirskrifta" label and value
doc.font('Bold').fontSize(12).text('Fjöldi undirskrifta: ', 60, currentYPosition, { align: 'left' });
currentYPosition = doc.y + 5;
doc.font('Regular').fontSize(12).text(endorsementList.endorsements.length.toString(), 60, currentYPosition, { align: 'left' });
currentYPosition = doc.y + 15;

// "Ábyrgðarmaður" label and value
doc.font('Bold').fontSize(12).text('Ábyrgðarmaður: ', 60, currentYPosition, { align: 'left' });
currentYPosition = doc.y + 5;
doc.font('Regular').fontSize(12).text(ownerName, 60, currentYPosition, { align: 'left' });
currentYPosition = doc.y + 15;

// "Kennitala ábyrgðarmanns" label and value
doc.font('Bold').fontSize(12).text('Kennitala ábyrgðarmanns: ', 60, currentYPosition, { align: 'left' });
currentYPosition = doc.y + 5;
doc.font('Regular').fontSize(12).text(endorsementList.owner, 60, currentYPosition, { align: 'left' });
currentYPosition = doc.y + 30; // Extra space before next section (if any)



// Table for endorsements
// Set the same Y-position for all headers
// const tableHeaderY = currentYPosition + 10; // Start table just below the previous content

const dateX = 60; // Column X position for 'Dags. skráð'
const nameX = 160; // Column X position for 'Nafn'
const localityX = 360; // Column X position for 'Sveitarfélag'


// Table headers drawing function
const drawTableHeaders = () => {
doc.font('Bold').fontSize(12);
doc.text('Dags. skráð', dateX, currentYPosition, { width: 100, align: 'left' });
doc.text('Nafn', nameX, currentYPosition, { width: 200, align: 'left' });
doc.text('Sveitarfélag', localityX, currentYPosition, { width: 200, align: 'left' });
currentYPosition = doc.y + 5; // Adjust space between header and rows
};

// Endorsements List (Rows)
drawTableHeaders();
endorsementList.endorsements.forEach((endorsement: Endorsement) => {
if (doc.y + 20 > doc.page.height - 100) {
// Add a new page if content is about to overflow
doc.addPage();
currentYPosition = 60; // Reset Y-position for the new page
drawTableHeaders(); // Draw table headers at the top of the new page
}

// Adjust the Y position for the text to appear below the image dynamically
let currentYPosition = 40 + headerImageHeight + 20 // 20px extra padding below the image
// Draw the endorsement data
doc.font('Regular').fontSize(10);
doc.text(endorsement.created.toLocaleDateString(locale), dateX, currentYPosition, { width: 100, align: 'left' });
doc.text(endorsement.meta.fullName || 'Nafn ótilgreint', nameX, currentYPosition, { width: 200, align: 'left' });
doc.text(endorsement.meta.locality || 'Sveitafélag ótilgreint', localityX, currentYPosition, { width: 200, align: 'left' });

// Title and petition details
doc
.font('Bold')
.fontSize(24)
.text('Upplýsingar um undirskriftalista', 60, currentYPosition, {
align: 'left',
})
.moveDown()

// Adjust Y position after title
currentYPosition = doc.y + 10 // Adjust by adding extra padding if needed

doc
.font('Bold')
.fontSize(14)
.text('Heiti undirskriftalista: ', { continued: true })
doc
.font('Regular')
.text(endorsementList.title, { align: 'left' })
.moveDown()

doc
.font('Bold')
.fontSize(14)
.text('Um undirskriftalista: ', { continued: true })
doc
.font('Regular')
.text(endorsementList.description, { align: 'left' })
.moveDown()

// Petition details
doc.font('Bold').text('Ábyrgðarmaður: ', { continued: true })
doc.font('Regular').text(ownerName, { align: 'left' })

doc.font('Bold').text('Fjöldi skráðir: ', { continued: true })
doc
.font('Regular')
.text(endorsementList.endorsements.length.toString(), { align: 'left' })
.moveDown()

// Add endorsement signatures table
doc.moveDown().font('Bold').fontSize(12)
const dateX = 60
const nameX = 160
const localityX = 360

doc.text('Dags. skráð', dateX, doc.y, { width: 100 })
doc.text('Nafn', nameX, doc.y, { width: 200 })
doc.text('Sveitarfélag', localityX, doc.y, { width: 200 })

endorsementList.endorsements.forEach((endorsement: Endorsement) => {
// Assuming Endorsement is typed
if (doc.y > doc.page.height - 100) {
doc.addPage()
doc.text('Dags. skráð', dateX, doc.y, { width: 100 })
doc.text('Nafn', nameX, doc.y, { width: 200 })
doc.text('Sveitarfélag', localityX, doc.y, { width: 200 })
}
doc.moveDown()
const y = doc.y
doc.font('Regular').fontSize(10)
doc.text(endorsement.created.toLocaleDateString(locale), dateX, y, {
width: 100,
})
doc.text(endorsement.meta.fullName || 'Nafn ótilgreint', nameX, y, {
width: 200,
})
doc.text(
endorsement.meta.locality || 'Sveitafélag ótilgreint',
localityX,
y,
{ width: 200 },
)
})
currentYPosition = doc.y + 5; // Move down slightly for the next row
});

// Add footer image
doc.image(footerImagePath, 60, doc.page.height - 80, { width: 120 })

doc.end()
return await getStream.buffer(doc)
}
// Add footer image at the bottom of the page
const footerY = doc.page.height - 80;
doc.image(footerImagePath, 60, footerY, { width: 120 });

doc.end();
return await getStream.buffer(doc);
}



async emailPDF(
listId: string,
Expand Down

0 comments on commit 2e615c9

Please sign in to comment.