update pdf params

This commit is contained in:
LittleBoy 2024-08-27 22:55:51 +08:00
parent 8590d577bd
commit 7c84803b36

View File

@ -4,16 +4,16 @@ import {useState} from "react";
import dayjs from "dayjs";
const PDF_Y_START = 40;
function drawItem(doc: JsPDF, item: {
title: string;
content?: string
}, y: number, align: 'left' | 'right' = 'left', fontSize: number = 13) {
doc.setFontSize(fontSize);
// const width = doc.internal.pageSize.getWidth();
doc.text(item.title, align == 'left' ? 20 : 180, y);
if (item.content && item.content.length > 0) doc.text(item.content, align == 'left' ? 65 : 230, y, {maxWidth: 150});
doc.text(item.title, align == 'left' ? 30 : 170, y);
if (item.content && item.content.length > 0) doc.text(item.content, align == 'left' ? 75 : 220, y, {maxWidth: 150});
}
export function GeneratePdf(bill: BillModel,save = true) {
const doc = new JsPDF({
orientation: 'landscape',
@ -23,27 +23,29 @@ export function GeneratePdf(bill: BillModel,save = true) {
// const height = doc.internal.pageSize.getHeight();
doc.setFont('Helvetica', 'normal', 'bold');
doc.setFontSize(20);
doc.text('ACKNOWLEDGEMENT RECEIPT', 100, 20, {});
doc.text('ACKNOWLEDGEMENT RECEIPT', 100, PDF_Y_START, {});
doc.setFont('Helvetica', 'normal', 'normal');
drawItem(doc, {title: 'Student Name:', content: bill.student_english_name || bill.student_chinese_name || 'N/A'}, 40)
drawItem(doc, {title: 'Reference Number:', content: `${bill.id}`}, 40, "right")
drawItem(doc, {title: `${bill.student_number?"Student":"Bill"} Number:`, content: `${bill.student_number || bill.application_number}`}, 48)
drawItem(doc, {title: 'Print Date:', content: dayjs().format('YYYY-MM-DD')}, 48, "right")
drawItem(doc, {title: 'Student Name:', content: bill.student_english_name || bill.student_chinese_name || 'N/A'}, PDF_Y_START + 10)
drawItem(doc, {title: 'Reference Number:', content: `${bill.id}`}, PDF_Y_START + 10, "right")
drawItem(doc, {title: `${bill.student_number?"Student":"Bill"} Number:`, content: `${bill.student_number || bill.application_number}`}, PDF_Y_START + 18)
drawItem(doc, {title: 'Print Date:', content: dayjs().format('YYYY-MM-DD')}, PDF_Y_START + 18, "right")
const programme_english_name = bill.programme_english_name || ''
drawItem(doc, {
title: 'Programme:',
content: bill.programme_english_name || 'N/A'
}, 56)
if(bill.programme_english_name && bill.programme_english_name.length > 0){
drawItem(doc, {title: 'Mode of Study:', content: bill.attendance_mode == 'FT' ? 'FULL-TIME': bill.attendance_mode}, bill.programme_english_name.length > 70?70:64)
content: programme_english_name || 'N/A'
}, PDF_Y_START + 26)
const programme_limit = programme_english_name.length > 76 ;
if(programme_english_name.length > 0){
drawItem(doc, {title: 'Mode of Study:', content: bill.attendance_mode == 'FT' ? 'FULL-TIME': bill.attendance_mode}, programme_limit? PDF_Y_START + 38 : PDF_Y_START + 34)
}
// draw table
autoTable(doc, {
startY: 80,
startY: programme_limit ? PDF_Y_START + 44 : PDF_Y_START + 40,
theme: 'grid',
margin: {top: 37, left: 20, right: 20},
margin: {top: 37, left: 30, right: 30},
styles: {
fontSize: 13,
fillColor: [255, 255, 255],
@ -56,7 +58,7 @@ export function GeneratePdf(bill: BillModel,save = true) {
headStyles: {
fontSize: 15,
},
head: [['No.', 'Transaction Date', 'Payment Type', 'Payment Method', 'HK$']],
head: [['No.', 'Transaction Date', 'Payment Type', 'Payment Method', {content:'HK$',styles:{minCellWidth:30,halign:'right'}}]],
body: [
...(bill.details.map(it=>{
return [
@ -64,14 +66,17 @@ export function GeneratePdf(bill: BillModel,save = true) {
bill.paid_at?dayjs(bill.initiated_paid_at).format('YYYY-MM-DD'):'',
it.bill_type,
`${bill.payment_channel}` + (bill.payment_method && bill.payment_channel != bill.payment_method ? `(${bill.payment_method})` : ''),
`${it.amount}`
{content:`${it.amount}`,styles:{halign:'right'}} as any
];
})),
[{
colSpan: 4,
content: 'TOTAL:',
styles: {valign: 'middle', halign: 'right'},
}, `${bill.amount}`],
styles: {valign: 'middle', halign: 'right',fontStyle: 'bold'},
}, {
content:`${bill.amount}`,
styles:{halign:'right'}
}],
],
})
// draw foot