Let's suppose that we have an attached file:
\immediate\pdfobj stream file {\jobname.tex}
\pdfannot width 10pt height 10pt depth 0pt {
/Subtype /FileAttachment
/FS << /F (file)
/Type /Filespec
/EF << /F \the\pdflastobj\space 0 R >> >>}
It is attached via two operations - first is a kind of file resource, the second is a visible image, by clicking on which we open the file.
Now suppose that we need to put the image at current position. The file which must be attached to it is formed dynamically by something like
\immediate\openout\fileout=file.tmp\relax
\immediate\write\fileout{some dynamic content}%
\immediate\closeout\fileout
while the text which is written to the file line by line goes to the document also. This means that it becomes available at a later stage, than the image is inserted.
If I do something like this, the document is compiled correctly, but the pdf viewer crashes:
\pdfoutput=1
\newcount\fileno \fileno=0
\newwrite\fileout
\pdfannot width 8pt height 8pt depth 0pt {
/Subtype /FileAttachment
/FS <</Type /Filespec /F (test.txt)
/EF <</F \the\fileno\space 0 R>> >> /Name /PushPin}
\immediate\openout\fileout=test.tmp\relax
\immediate\write\fileout{a}%
\immediate\closeout\fileout
\immediate\pdfobj stream file {test.tmp}%
\fileno=\the\pdflastobj
\bye
Is it possible to point the annotation to a yet-non-existing file resource, which is guaranteed to exist before the end of the whole document?
It is desirable that this feature will work with LuaTeX.
Is it possible to find the answer in this resource?
EDIT
If I put the file resource before the annotation, it complains:
! LuaTeX error (ext5): cannot open file for embedding.
l.6 \immediate\pdfobj stream file {test.tmp}
because LuaTeX is attaching the file contents in-place. An alternative solution would be to delay this operation until the end of the document, if this is at all possible.
EDIT
Some useful info from
http://tug.ctan.org/systems/doc/pdftex/manual/samplepdf/samplepdf.tex
User-defined object can be inserted into the pdf output by \pdfobj. The
object is written out as specified, apart from case when \pdfobj is used with
"stream" option. An object created by \pdfobj is held in memory and will
not be written to the pdf output, unless 1) the object is referenced by
saying \pdfrefobj <object number>; or 2) \pdfobj is preceded by
\immediate. The object number of the last object created by \pdfobj is
accessible via \pdflastobj
\immediate\openout\fileout=file.tmp%
better as\immediate\openout\fileout=file.tmp %
or\immediate\openout\fileout=file.tmp\relax
so that the file name is properly terminated (rather than relying on the following tokens).