Código

Código

Há algum comportamento estranho com novas páginas(obrigado a David Carlisle por observar que não é só \clearpage)nisso atrapalha a capacidade de fazer isso \hypersetupposteriormente no documento.

Eu gero meus metadados após uma nova página, portanto preciso armazenar esses dados de alguma forma para a próxima execução.

Veja a demonstração abaixo para obter detalhes.

Código

\documentclass{article}
\usepackage{fontspec} % typeset with xelatex
\usepackage{hyperref}

\newcommand\insertproducer{placeholder}
\hypersetup{pdfproducer=Tester Schmoe (set in preamble)}

\AtEndDocument{%
  \renewcommand\insertproducer{Tester Schmidt (set in body just before end)}
  \hypersetup{pdfproducer=\insertproducer}
}%

\begin{document}
\null
%\clearpage % <-- uncommenting this results in \hypersetup failure
\end{document}

pdfinfo test.pdfSaída (comentado \clearpage)

Resultado esperado.

Creator:        LaTeX with hyperref package
Producer:       Tester Schmidt (set in body just before end)
CreationDate:   Tue Jun  7 12:13:48 2016
Tagged:         no
Pages:          1
Encrypted:      no
Page size:      612 x 792 pts (letter)
File size:      2149 bytes
Optimized:      no
PDF version:    1.5

pdfinfo test.pdfSaída (não comentada \clearpage)

Saída inesperada.

Creator:        LaTeX with hyperref package
Producer:       Tester Schmoe (set in preamble)
CreationDate:   Tue Jun  7 12:16:44 2016
Tagged:         no
Pages:          1
Encrypted:      no
Page size:      612 x 792 pts (letter)
File size:      2138 bytes
Optimized:      no
PDF version:    1.5

Responder1

Você pode simplificar sua configuração de várias maneiras.

\documentclass{article}

\usepackage{hyperref}

\hypersetup{pdfproducer=Tester Schmitty (set in preamble)}

\AtBeginDocument{%
  \hypersetup{
    pdfproducer = \myproducer,
  }
}

\makeatletter
\newcommand*\myhypersetuptoaux[1]{% command to tell LaTeX to save the value for the next run
  \AtEndDocument{%
    \immediate\write\@auxout{\string\my@producer{#1}}%
  }%
}
\newcommand*{\my@producer}[1]{\protected@xdef\myproducer{#1}}
\newcommand*{\myproducer}{% initial value
  This baby needs extra TLC, run it again.%
}
\makeatother

\begin{document}
\null
\clearpage
\myhypersetuptoaux{Tester Schmoe}
\end{document}

Isto é o que recebo pdfinfoapós a primeira execução

Title:          
Subject:        
Keywords:       
Author:         
Creator:        LaTeX with hyperref package
Producer:       This baby needs extra TLC, run it again.
CreationDate:   Thu Jun  9 09:17:16 2016
ModDate:        Thu Jun  9 09:17:16 2016
Tagged:         no
Form:           none
Pages:          1
Encrypted:      no
Page size:      612 x 792 pts (letter) (rotated 0 degrees)
File size:      8416 bytes
Optimized:      no
PDF version:    1.5

e é isso que recebo após a segunda execução:

Title:          
Subject:        
Keywords:       
Author:         
Creator:        LaTeX with hyperref package
Producer:       Tester Schmoe
CreationDate:   Thu Jun  9 09:20:39 2016
ModDate:        Thu Jun  9 09:20:39 2016
Tagged:         no
Form:           none
Pages:          1
Encrypted:      no
Page size:      612 x 792 pts (letter) (rotated 0 degrees)
File size:      8389 bytes
Optimized:      no
PDF version:    1.5

Responder2

Solução de arquivo AUX

Isto poderia ser potencialmente simplificado e pode ter pontos fracos. Estou apenas começando a experimentar escrever/ler arquivos AUX.

É muito importante observar que certas coisas DEVEM ser definidas no início do documento:

\hypersetup{
    colorlinks=true,
    linkcolor=red,
    urlcolor=red,
    hyperfootnotes=false,
    hypertexnames,
    bookmarks=true % Causes clash if hyperref parameters loaded before bookmark, because bookmark loads hyperref without any parameters
}
  1. Isso grava os metadados no arquivo aux como:

por exemplo

\mysetproducer{Tester Schmoe}
  1. A próxima execução analisa aux imediatamente depois \begin{document}e executa:

por exemplo

 \mysetproducer{Tester Schmoe}

que por sua vez cria um comando my@pdfproducercontendoTester Schmoe

por exemplo

 \expandafter\xdef\csname my@pdfproducer\endcsname{#1}}
  1. O comando em nível de usuário \myproducerobtém esse valor se a macro existir (somente após a criação do arquivo aux - também conhecido como após pelo menos uma execução)

por exemplo

\expandafter\ifx\csname my@pdfproducer\endcsname\relax This baby needs extra TLC, run it again.\else
\csname my@pdfproducer\endcsname\fi}

Código Completo

\documentclass{article}
\usepackage{fontspec} % typeset with xelatex
\usepackage{hyperref}
\usepackage{atveryend}

\hypersetup{pdfproducer=Tester Schmitty (set in preamble)}

\AtBeginDocument{%
  \hypersetup{
    pdfproducer = \myproducer{}
  }
}

\makeatletter
\newcommand*\myhypersetuptoaux[1]{% command to tell LaTeX to save the value for the next run
  %\AfterLastShipout{%
    \immediate\write\@auxout{%
    %\protected@write\@mainaux{}{%
      \string\mysetproducer{#1}%
    %}%
    }%
  %}%
}
\makeatother

\makeatletter
\newcommand*{\mysetproducer}[1]{% called by the aux file and creates command my@producer that expands to input of myhypersetuptoaux
  \expandafter\xdef\csname my@pdfproducer\endcsname{#1}}
\makeatother

\makeatletter
\newcommand*{\myproducer}{% user-level command to retrieve value of my@pdfproducer
  \expandafter\ifx\csname my@pdfproducer\endcsname\relax This baby needs extra TLC, run it again.\else
  \csname my@pdfproducer\endcsname\fi}
%  \@ifundefined{stored@#1}{???}{\csname stored@#1\endcsname}%
\makeatother

\begin{document}
\null
\clearpage
\myhypersetuptoaux{Tester Schmoe}
\end{document}

informação relacionada