@@ -43,8 +43,18 @@ function lighten(hex: string, amount = 0.85): string {
4343 return `#${ rr } ${ gg } ${ bb } ` ;
4444}
4545
46- function formatDate ( d ?: Date ) {
47- return d ? new Date ( d ) . toLocaleDateString ( ) : undefined ;
46+ function formatDate ( d ?: Date , format : string = "YYYY-MM-DD" ) {
47+ if ( ! d ) return undefined ;
48+ const date = new Date ( d ) ;
49+ const year = date . getFullYear ( ) ;
50+ const month = String ( date . getMonth ( ) + 1 ) . padStart ( 2 , '0' ) ;
51+ const day = String ( date . getDate ( ) ) . padStart ( 2 , '0' ) ;
52+
53+ if ( format === "DD.MM.YYYY" ) {
54+ return `${ day } .${ month } .${ year } ` ;
55+ }
56+ // Default to YYYY-MM-DD
57+ return `${ year } -${ month } -${ day } ` ;
4858}
4959
5060function toMoney ( n : number ) : string {
@@ -232,6 +242,7 @@ function buildContext(
232242 invoice : InvoiceWithDetails ,
233243 settings ?: BusinessSettings & { logoUrl ?: string ; brandLayout ?: string } ,
234244 _highlight ?: string ,
245+ dateFormat ?: string ,
235246) : TemplateContext & { logoUrl ?: string ; brandLogoLeft ?: boolean } {
236247 const currency = invoice . currency || settings ?. currency || "USD" ;
237248 // Build tax summary from normalized taxes if present
@@ -267,8 +278,8 @@ function buildContext(
267278
268279 // Invoice
269280 invoiceNumber : invoice . invoiceNumber ,
270- issueDate : formatDate ( invoice . issueDate ) ! ,
271- dueDate : formatDate ( invoice . dueDate ) ,
281+ issueDate : formatDate ( invoice . issueDate , dateFormat ) ! ,
282+ dueDate : formatDate ( invoice . dueDate , dateFormat ) ,
272283 currency,
273284 status : invoice . status ,
274285
@@ -330,7 +341,7 @@ export async function generateInvoicePDF(
330341 businessSettings ?: BusinessSettings ,
331342 templateId ?: string ,
332343 customHighlightColor ?: string ,
333- opts ?: { embedXmlProfileId ?: string ; embedXml ?: boolean ; xmlOptions ?: Record < string , unknown > } ,
344+ opts ?: { embedXmlProfileId ?: string ; embedXml ?: boolean ; xmlOptions ?: Record < string , unknown > ; dateFormat ?: string } ,
334345) : Promise < Uint8Array > {
335346 // Inline remote logo when possible for robust HTML rendering
336347 const inlined = await inlineLogoIfPossible ( businessSettings ) ;
@@ -339,6 +350,7 @@ export async function generateInvoicePDF(
339350 inlined ,
340351 templateId ,
341352 customHighlightColor ,
353+ opts ?. dateFormat ,
342354 ) ;
343355 // First, attempt Puppeteer-based rendering
344356 let pdfBytes = await tryPuppeteerPdf ( html ) ;
@@ -349,6 +361,7 @@ export async function generateInvoicePDF(
349361 inlined ,
350362 customHighlightColor ,
351363 templateId ,
364+ opts ?. dateFormat || "YYYY-MM-DD" ,
352365 ) ;
353366 }
354367
@@ -448,8 +461,9 @@ export function buildInvoiceHTML(
448461 settings ?: BusinessSettings ,
449462 templateId ?: string ,
450463 highlight ?: string ,
464+ dateFormat ?: string ,
451465) : string {
452- const ctx = buildContext ( invoice , settings , highlight ) ;
466+ const ctx = buildContext ( invoice , settings , highlight , dateFormat ) ;
453467 const hl = normalizeHex ( highlight ) || "#2563eb" ;
454468 const hlLight = lighten ( hl , 0.86 ) ;
455469
@@ -661,6 +675,7 @@ async function generateStyledPDF(
661675 businessSettings ?: BusinessSettings ,
662676 customHighlightColor ?: string ,
663677 templateId ?: string ,
678+ dateFormat : string = "YYYY-MM-DD" ,
664679) : Promise < Uint8Array > {
665680 const highlight = customHighlightColor
666681 ? hexToRgb ( customHighlightColor )
@@ -671,12 +686,14 @@ async function generateStyledPDF(
671686 invoiceData ,
672687 businessSettings ,
673688 highlight ,
689+ dateFormat ,
674690 ) ;
675691 }
676692 return await generateProfessionalPDF (
677693 invoiceData ,
678694 businessSettings ,
679695 highlight ,
696+ dateFormat ,
680697 ) ;
681698}
682699
@@ -704,6 +721,7 @@ async function generateProfessionalPDF(
704721 invoiceData : InvoiceWithDetails ,
705722 businessSettings ?: BusinessSettings ,
706723 highlightRgb ?: ReturnType < typeof rgb > ,
724+ dateFormat : string = "YYYY-MM-DD" ,
707725) : Promise < Uint8Array > {
708726 const highlight = highlightRgb || rgb ( 0.15 , 0.39 , 0.92 ) ;
709727
@@ -774,7 +792,7 @@ async function generateProfessionalPDF(
774792 color : rgb ( 0.1 , 0.1 , 0.1 ) ,
775793 } ) ;
776794 ctx . page . drawText (
777- `Date: ${ new Date ( invoiceData . issueDate ) . toLocaleDateString ( ) } ` ,
795+ `Date: ${ formatDate ( invoiceData . issueDate , dateFormat ) } ` ,
778796 {
779797 x : boxX + 10 ,
780798 y : boxY + boxH - 34 ,
@@ -785,7 +803,7 @@ async function generateProfessionalPDF(
785803 ) ;
786804 if ( invoiceData . dueDate ) {
787805 ctx . page . drawText (
788- `Due: ${ new Date ( invoiceData . dueDate ) . toLocaleDateString ( ) } ` ,
806+ `Due: ${ formatDate ( invoiceData . dueDate , dateFormat ) } ` ,
789807 {
790808 x : boxX + 10 ,
791809 y : boxY + boxH - 50 ,
@@ -1022,6 +1040,7 @@ async function generateMinimalistPDF(
10221040 invoiceData : InvoiceWithDetails ,
10231041 businessSettings ?: BusinessSettings ,
10241042 highlightRgb ?: ReturnType < typeof rgb > ,
1043+ dateFormat : string = "YYYY-MM-DD" ,
10251044) : Promise < Uint8Array > {
10261045 const highlight = highlightRgb || rgb ( 0.05 , 0.59 , 0.41 ) ;
10271046
@@ -1075,7 +1094,7 @@ async function generateMinimalistPDF(
10751094 drawLabelValue (
10761095 ctx ,
10771096 "Issue Date:" ,
1078- new Date ( invoiceData . issueDate ) . toLocaleDateString ( ) ,
1097+ formatDate ( invoiceData . issueDate , dateFormat ) || "" ,
10791098 margins . left ,
10801099 9 ,
10811100 70 ,
@@ -1085,7 +1104,7 @@ async function generateMinimalistPDF(
10851104 drawLabelValue (
10861105 ctx ,
10871106 "Due Date:" ,
1088- new Date ( invoiceData . dueDate ) . toLocaleDateString ( ) ,
1107+ formatDate ( invoiceData . dueDate , dateFormat ) || "" ,
10891108 margins . left ,
10901109 9 ,
10911110 70 ,
0 commit comments