1

This is my pandoc version:

pandoc --version
pandoc 2.3.1
Compiled with pandoc-types 1.17.5.1, texmath 0.11.1.1, skylighting 0.7.3
Default user data directory: /Users/vle/.pandoc
Copyright (C) 2006-2018 John MacFarlane
Web:  http://pandoc.org
This is free software; see the source for copying conditions.
There is no warranty, not even for merchantability or fitness
for a particular purpose.

And the test markdown document (testjs.md):

---
title: "Test Javascript"
geometry: "left=2.5cm,right=2cm,top=2cm,bottom=2cm"
output: pdf_document
header-includes:
    - \usepackage{hyperref}
    - \usepackage{fancyhdr}
    - \pagestyle{fancy}
    - \fancyhead[LE,LO]{Last Modified Date \TextField[name=opendate, calculate={var OpenDate = this.getField("opendate"); OpenDate.value = new Date();}]{}}
    - \fancyfoot[CO,CE]{Template Version \today}
    - \fancyfoot[LE,RO]{\thepage}

---

# Section

## Header :
*Try to set the current date when the PDF file is open*

\TextField[name=datefield,%
              width = \hsize,%
              height = 3cm,%
              multiline=true,%
              bordercolor= ,%
              % color = 1 1 1,%
              calculate={var d = this.getField("datefield"); d.value = new Date();}
              ]{}

\pagebreak

## Header :
*Try to set the current date when the PDF file is open*

\TextField[name=datefield1,%
              width = \hsize,%
              height = 3cm,%
              multiline=true,%
              bordercolor= ,%
              % color = 1 1 1,%
              calculate={var e = this.getField("datefield"); e.value = new Date();}
              ]{}

\pagebreak

## Header :
*Try to set the current date when the PDF file is open*

\TextField[name=datefield2,%
              width = \hsize,%
              height = 3cm,%
              multiline=true,%
              bordercolor= ,%
              % color = 1 1 1,%
              calculate={var f = this.getField("datefield"); f.value = new Date();}
              ]{}

rendering command:

pandoc --number-section -o testjs.pdf testjs.md

The output PDF open in Acrobat Professional DC on macOS gives this: enter image description here

Problems:

  1. Date is not shown on the textfield on the "Last Modified Date" header field.
  2. Date is not shown on the text field in the body of all three pages.

  3. Textfield "Last Modified Date" on page 3 does not appear because of textfield name collision.

How do I solve these problems?

Update:

I think I found some clues: There must be some event to trigger the javascript to update field. So the more practical question is how to update the header on some onsave event or onclick of some form button. I prefer onsave to save the whole document and update the timestamp.

1

I couldn't find a way to do this with hyperref alone.

There are two issues with using hyperref.

  1. Form fields are meant to be enclosed in a Form environment and I came across issues trying to set the contents of the fields from JavaScript when this wasn't done (as in your MWE). And adding in this environment makes Adobe Reader think that this is a real PDF form, which seems a bit odd.
  2. hyperref doesn't provide access to the Acrobat willsave document event which is the most natural event to use in your case.

For working around the first issue, I suggest using the pdfcomment package and using FreeText annotations instead of form text fields.

Fortunately, the second issue is straight forward to solve.

Try this MWE. Just plain latex, but you can adapt it to your pandoc MarkDown workflow.

What does it do?

  • Pass the en-US option to datetime2 (pdfcomment seems to use the iso option by default).
  • Set FreeText annotation flags to make them read only (pdfcomment hard codes them to just turn on printing).
  • Write JavaScript code to be run when the document is about to be saved. The code gets all annotations in the document and sets the content to the current date. (This is not that clever and assumes that the only annotations are the date stamp ones. You could fairly easily make it more clever if you have other annotations present.)
  • Use \pdfcatalog to set up the document willsave event to call our code.
  • Set up the FreeText annotation using \pdfcomment to look pretty(ish) and initialise it to \today.
  • Set up headers.

It seems to work fine with multiple pages.

\documentclass{book}
\PassOptionsToPackage{en-US}{datetime2}
\usepackage{pdfcomment}
\makeatletter
% Set FreeText annotation flags to Print (bit 3) and ReadOnly (bit 7)
\patchcmd{\pc@annot@freetext}
  {/F 4}
  {/F 68}
  {}
  {}
\def\willsavecode{%
  this.syncAnnotScan ();
  var annots = this.getAnnots ();
  for (var i = 0; i < annots.length; i++)
    annots[i].contents = util.printd ("mmmm d, yyyy", new Date ());
}
\pdfcatalog{/AA<</WS<</S/JavaScript/JS(\Hy@escapestring{\willsavecode})>>>>}
\makeatother
\newcommand*{\datestamp}{%
  \pdffreetextcomment[%
    width=6cm,
    height=\normalbaselineskip,
    hoffset=1em,
    voffset=-\dp\strutbox,
    linewidth=0pt,
    color=,
    fontsize=10pt]{\today}}
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[LE,LO]{Last Modified Date: \datestamp}
\fancyfoot[CO,CE]{Template Version: \today}
\fancyfoot[LE,RO]{\thepage}
\begin{document}
Filler text.
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.