parallax/jsPDF · error · Error

Invalid argument passed to jsPDF.setCreationDate

Error message

Invalid argument passed to jsPDF.setCreationDate

What it means

`setCreationDate` (src/jspdf.js:673) accepts three forms: no argument (defaults to now), a `Date` instance (converted via convertDateToPDFDate at src/jspdf.js:634), or a string matching the PDF date regex `^D:YYYYMMDDHHmmSS±HH'mm'?` (src/jspdf.js:675). Anything else throws at src/jspdf.js:685. The regex is strict — it requires the `D:` prefix, a year in 1970–2037, and the timezone suffix in the `+HH'mm'` / `-HH'mm'` form.

Source

Thrown at src/jspdf.js:685

    // var timeZoneMinutes = parseInt(parmPDFDate.substr(20, 2), 10);

    var resultingDate = new Date(year, month, date, hour, minutes, seconds, 0);
    return resultingDate;
  });

  var setCreationDate = (API.__private__.setCreationDate = function(date) {
    var tmpCreationDateString;
    var regexPDFCreationDate = /^D:(20[0-2][0-9]|203[0-7]|19[7-9][0-9])(0[0-9]|1[0-2])([0-2][0-9]|3[0-1])(0[0-9]|1[0-9]|2[0-3])(0[0-9]|[1-5][0-9])(0[0-9]|[1-5][0-9])(\+0[0-9]|\+1[0-4]|-0[0-9]|-1[0-1])'(0[0-9]|[1-5][0-9])'?$/;
    if (typeof date === "undefined") {
      date = new Date();
    }

    if (date instanceof Date) {
      tmpCreationDateString = convertDateToPDFDate(date);
    } else if (regexPDFCreationDate.test(date)) {
      tmpCreationDateString = date;
    } else {
      throw new Error("Invalid argument passed to jsPDF.setCreationDate");
    }
    creationDate = tmpCreationDateString;
    return creationDate;
  });

  var getCreationDate = (API.__private__.getCreationDate = function(type) {
    var result = creationDate;
    if (type === "jsDate") {
      result = convertPDFDateToDate(creationDate);
    }
    return result;
  });

  /**
   * @name setCreationDate
   * @memberof jsPDF#
   * @function
   * @instance

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Pass a `Date` object and let jsPDF format it: `doc.setCreationDate(new Date('2023-01-01'))`.
  2. If you must pass a string, build it in the exact `D:YYYYMMDDHHmmSS+HH'mm'` format the regex expects.
  3. For epoch numbers, wrap in `new Date(ms)` first.
  4. Avoid years outside 1970–2037 (the regex caps at 2037).

Example fix

// before
 doc.setCreationDate('2023-01-01T00:00:00Z');  // throws

// after
 doc.setCreationDate(new Date('2023-01-01T00:00:00Z'));
Defensive patterns

Strategy: validation

Validate before calling

function toPdfDate(input) {
  if (input == null) return new Date();          // default now
  if (input instanceof Date) return input;        // Date instance
  if (typeof input === 'number') return new Date(input); // epoch ms
  if (typeof input === 'string' && /^D:\d{14}/.test(input)) return input; // already PDF date
  // anything else (incl. ISO strings) -> convert via Date
  var d = new Date(input);
  return isNaN(d.getTime()) ? null : d;
}
var d = toPdfDate(value);
if (d) doc.setCreationDate(d); else /* handle invalid */

Type guard

function isPdfDateOrConvertible(v) {
  if (v == null || v instanceof Date) return true;
  if (typeof v === 'number') return true;
  if (typeof v === 'string') return /^D:(19[7-9]\d|20[0-3]\d)\d{10}/.test(v) || !isNaN(new Date(v).getTime());
  return false;
}

Try / catch

try {
  doc.setCreationDate(value);
} catch (e) {
  if (/setCreationDate/.test(e.message)) {
    doc.setCreationDate(new Date(value));  // retry via Date object
  } else throw e;
}

Prevention

When it happens

Trigger: `doc.setCreationDate('2023-01-01')` (ISO string, no D: prefix); `doc.setCreationDate('D:2023')` (too short); `doc.setCreationDate(1672531200000)` (a number, not a Date); `doc.setCreationDate('D:2099...')` (year out of the 1970–2037 range).

Common situations: Passing a database/ISO timestamp string directly instead of `new Date(isoString)`; copying a PDF metadata string but dropping the `D:` prefix; year-2037 boundary issues for far-future dated documents; numeric epoch timestamps passed as-is.

Related errors


AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13). Data as JSON: /api/errors/9c5d8a1df87e7c8a. Report an issue: GitHub.