foliojs / pdfkit

A JavaScript PDF generation library for Node and the browser
http://pdfkit.org/
MIT License
9.92k stars 1.16k forks source link

Acroform Date field format not working #1546

Open duhmojo opened 3 months ago

duhmojo commented 3 months ago

Bug Report

Description of the problem

Just trying to create a form with a date field. Setting a formText() format doesn't seem to do much. In Chrome and MacOS Preview the date field doesn't support or isn't prompted to show the date picker, and the format param isn't honoured. In Acrobat Reader there's a drop down date picker, but picking a date doesn't set the field value to display using the param format. Changing the year in the date picker doesn't show the date either. So I can have 2 dates from 2024 and 2023 and they're not displaying correctly.

image

doc.fontSize(10).formText(field.name, xOffset, lOffset, fieldWidth - 10, lfieldHeight, {
    format: { type: 'date', param: 'yyyy-mm-dd' },
})

I'm not sure if I'm doing something wrong or what to expect. The docs don't set much in the way of expectations.

The pdfkit docs (https://pdfkit.org/docs/forms.html#text_field_formatting) Acrobat SDK link is broken as well:

"When needing to format the text value of a Form Annotation, the following options are available. This will cause predefined document JavaScript actions to automatically format the text. Refer to the section Formatting scripts in Acrobat Forms Plugin of the Acrobat SDK documentation for more information."

Code sample

I wasted enough time with repl.it accessing my GitHub account and it doesn't build a pdf correctly. Try this:

const Document = require('pdfkit')
const fs = require('fs');

const doc = new Document();

doc.pipe(fs.createWriteStream('output.pdf'));

doc.initForm();
doc.fontSize(10).formText("date", 100, 100, 100, 25, {
    format: { type: 'date', param: 'yyyy-mm-dd' }
})

doc.end();

Your environment

duhmojo commented 2 months ago

Nobody? Is this the extent of what PDF offers for date fields? Is there another correct date field or usage that'll unlock something similar to what typical browser offer?