[Tex/LaTex] setting the ‘filename’ of a file embedded in a PDF using the attachfile package

attachfilepdfpdftex

I'm using the attachfile LaTeX package (in PDFLaTeX) to embed files in a PDF and I've run into a difficulty regarding what I suspect is the 'filename' of the embedded file.

I can attach/embed files in the following manner:

\textattachfile[author=Zapp Brannigan, color=0 0 0]{my_directory/my_file.dat}{\detokenize{my_file.dat}}

The file seems to be embedded successfully in the resulting PDF and is accessible, but the file name suggested by the Evince PDF viewer when selecting to "Save Attachment As…" on the link for the attached file is the following:

my_directory/my_file.dat

So, you can see that it is including the path of the original file in this 'filename'. Obviously, there are difficulties when one tries to save the attached file under this suggested filename.

How might I go about changing this suggested filename to something such as my_file.dat? I've experimented and I've consulted documentation on the attachfile package and have not been able to come up with an answer.

Just as a quick note: I am disinclined to use a package other than attachfile because, in reality, my code is more complicated than that shown; I'm including custom icons for various file types, as permitted by the attachfile package. However, I'll keep an open mind.

Thank you very much for any thoughts you may have on the matter.

Best Answer

I haven't tried to get TeX to automatically parse the filename component for you (but if you're interested, I'm sure someone else could help with that), but the following patches the \textattachfile macro to require an extra argument, the name of the file as it should appear in the attachment, which has no relation to the name of the file on the hard disk when the PDF is created. That is, the syntax is now \textattachfile[options]{physicalfilepath}{text}{virtualfilename}.

\documentclass{article}
\usepackage{attachfile}
\makeatletter

\def\atfi@textattachfile@i#1#2#3#4{%
    \setkeys{AtFi}{#1}%
    \atfi@embedfile{#2}%
    \def\atfi@textcolor(##1 ##2 ##3)##4{%
      \textcolor[rgb]{##1,##2,##3}{##4}}%
    \atfi@set@appearance{%
      \expandafter\atfi@textcolor\expandafter
      (\atfi@color@rgb){#3\strut}}%
    \atfi@flags@to@int
    \atfi@insert@file@annot{\detokenize{#4}}%
  \endgroup
}
\makeatother

\begin{document}
% now have to pass an extra argument to \textattachfile, the filename you want the viewer to see (note that Adobe Reader appears to 'hide' the path component)
\textattachfile[author=Zapp Brannigan, color=0 0 0]{my_directory/my_file.dat}{\detokenize{my_file.dat}}{filename_seen_by_viewer.txt}
\end{document}

In your case, you would probably want to change filename_seen_by_viewer.txt to my_file.dat, but I left it like this to illustrate more clearly.

(Just grepping the PDF file for my_directory should be sufficient to indicate that it works.)