Debian Codegraph @

texmf-dist/source/latex/pdfmanagement/pdfmanagement.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: pdfmanagement.dtx
4 %
5 % Copyright (C) 2019-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "LaTeX PDF management bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/pdfresources
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \DocumentMetadata{tagging=on,pdfstandard=ua-2}
27 \documentclass[full]{l3doc}
28 \usepackage{latex-lab-testphase-l3doc}
29
30 \usepackage{tabularx,array,booktabs}
31 \hypersetup{pdfauthor=The LaTeX Project,pdftitle=LaTeX PDF management bundle}
32 \newcommand\potentialclash{\noindent\llap{\dbend\ }}
33 \pdfextension glyphtounicode {char7f}{2621}
34 \raggedbottom
35 \begin{document}
36 \DocInput{\jobname.dtx}
37 \end{document}
38 %</driver>
39 % \fi
40 %
41 %
42 % \title{The \LaTeX{} PDF management bundle}
43 %
44 % \author{^^A
45 % The \LaTeX{} Project\thanks
46 % {^^A
47 % E-mail:
48 % \href{mailto:[email protected]}
49 % {[email protected]}^^A
50 % }^^A
51 % }
52 %
53 % \date{Version 0.97c, released 2026-05-26}
54 %
55 % \maketitle
56 % \begin{documentation}
57 % \section*{Abstract}
58 % {\em This is a temporary bundle created to allow the external
59 % loading of the new \LaTeX{} PDF management code during a test phase.
60 % It will disappear when the code is integrated into the \LaTeX{} format.}
61 %
62 % \medskip
63 % \noindent The PDF management code is loaded
64 % automatically if you use \cs{DocumentMetadata} before
65 % \cs{documentclass}.
66 %
67 % \begin{verbatim}
68 % \DocumentMetadata % loads also the PDF management
69 % {
70 % % options, see documentmetadata-support.pdf
71 % }
72 %
73 % \documentclass {...}
74 % \end{verbatim}
75 %
76 %
77 % It is also possible to load the PDF as package, e.g. in a class.
78 %
79 % \begin{verbatim}
80 % \RequirePackage{pdfmanagement}
81 % \end{verbatim}
82 %
83 % If the PDF management has already been loaded, e.g. in \cs{DocumentMetadata}
84 % this will do nothing. The package has one option, \texttt{backend} that allows
85 % to set a special backend like \texttt{dvipdfmx}. It will do nothing if the backend
86 % code has already been loaded.
87 %
88 % Other options, e.g., the PDF standard or the PDF version should then be set with a \cs{SetKeys} command:
89 %
90 % \begin{verbatim}
91 % \RequirePackage{pdfmanagement}
92 % \SetKeys[document/metadata]
93 % {
94 % pdfversion=2.0,
95 % lang=de,
96 % xmp=false,
97 % ...
98 % }
99 % \end{verbatim}
100 %
101 % Note that PDF management should be loaded as early as possible.
102 %
103 %
104 % \section*{Feedback wanted!}
105 %
106 % Bug reports and feedback are welcome. Please open an issue at
107 % \url{https://github.com/latex3/pdfresources}.
108 %
109 % While the code targets PDF as output format, feedback about the
110 % effect on other formats is needed too.
111 %
112 % \section{Introduction}
113 % The \LaTeX{} format contained for a long time nearly no code specific to the now quite
114 % central output format, PDF. It also offered nearly no interfaces to important PDF related
115 % primitive commands for package writers.
116 %
117 % Important tasks like supporting PDF standards,
118 % creating links, adding special colors, managing the content of
119 % central PDF-directories or even simple tasks like setting the PDF version
120 % were delegated to external packages which had to recourse to
121 % the primitive low-level commands in their code.
122 %
123 % This was problematic for three reasons:
124 % \begin{itemize}
125 %\item At first using primitives directly can lead to clashes and duplicate
126 % settings with conflicting values---nothing prevent packages to add for example
127 % the \texttt{/Title} twice to the Info dictionary, the \texttt{/Lang} entry
128 % twice to the Catalog, or to add two \texttt{/ExtGState}
129 % resources to a page. The PDF normally doesn't break in such
130 % cases---the format is quite robust---but it will ignore one of the duplicates and
131 % the output can be wrong.
132 %
133 % \item At second the primitives differ between the various engines and backends with
134 % which \LaTeX{} is used. To support the engines and backend
135 % packages have to write and maintain
136 % \enquote{driver} files which they did to a varying degree. This makes it
137 % difficult for users to assess if a package will work with their work-flow and
138 % is a strain for package writers as they have to keep track of engine and backend changes.
139 %
140 % \item And at last generic hooks and configuration points to various
141 % PDF related structures are missing and difficult to add.
142 % \end{itemize}
143 %
144 % Despite the potential problems, the number of conflicts were
145 % small and could be resolved in an ad-hoc fashion. But the plans for
146 % \LaTeX{} regarding support for tagged PDF and
147 % PDF standards mean that much more PDF specific code has to be
148 % written by the kernel directly and this can not be done without proper,
149 % well-defined and well-behaving interfaces and hooks.
150 %
151 % Some first steps for better support of PDF related commands have been done
152 % with the \pkg{l3pdf} package which has then been integrated as a module
153 % into \pkg{l3kernel}.
154 % It offers backend independent commands to create
155 % PDF objects and destination, to set the compress level and the PDF version.
156 %
157 % The PDF management bundle extends this to more PDF related areas
158 % and provides interfaces to them in a backend independent way.
159 %
160 % The PDF management has three main objectives
161 % connected with the problems identified above:
162 % \begin{itemize}
163 % \item For commands with \enquote{clash potential} it implements commands to
164 % replace the primitives and so to resolve potential conflicts.
165 %
166 % \item It implements commands for a variety of PDF related tasks
167 % and supports a well-defined set of backends.
168 %
169 % \item If sensible this commands are enhanced by hooks from the
170 % \LaTeX{} hook system. This has been e.g.\ done for annotations in the
171 % \pkg{l3pdfannot} bundle.
172 % \end{itemize}
173 %
174 % \section{\enquote{Change Strategy}: The integration into \LaTeX\label{sec:change}}
175 %
176 % The central module of this bundle, \pkg{l3pdfmanagement}, defines an interface
177 % for the (pdf\TeX) primitives \cs{pdfcatalog}, \cs{pdfinfo},
178 % \cs{pdfpagesattr}, \cs{pdfpagesattr} and \cs{pdfpageresources} and
179 % the analog commands from the other engines and backends.
180 %
181 % All these commands have a \enquote{clash potential}, this means that the new
182 % interface is incompatible with a parallel use of the primitive commands
183 % which it targets to replace and supersede.
184 % This doesn't affect many packages, but the list of package using such primitives
185 % contains central and important packages like \pkg{hyperref}, \pkg{tikz},
186 % \pkg{pdfx} and more.
187 %
188 % So while the goal is to integrate the code into the \LaTeX{} format directly,
189 % this can not be done immediately without conflicts with existing documents
190 % and packages.
191 %
192 % The code is there not loaded by the kernel but only if the trigger command
193 % \cs{DocumentMetadata} is used, or if the package \texttt{pdfmanagement}
194 % is loaded manually.
195 %
196 %
197 % We hope that this setup will allow packages writers and users to test the
198 % PDF management code and adapt packages and documents safely.
199 %
200 %
201 % \section{Backend support}
202 % The supported backends are pdflatex, lualatex, (x)dvipdfmx (latex, xelatex,
203 % dvilualatex
204 % and dvips with ps2pdf (not completely yet). dvips with distiller could work too
205 % but is untested.
206 %
207 % That the interfaces and commands are backend independent doesn't mean
208 % that the results and even the compilation behavior is identical.
209 % The backends are too different to allow this.
210 % Some backends expand arguments e.g. in a \cs{special} while other don't.
211 % Some backends can insert a resource at the first compilation, while another uses
212 % the aux-file and a label and so needs at least two compilation runs.
213 % Some backends manage some of the resources through side-effects,
214 % some manage them automatically.
215 % All this mean that package writers will still have to keep an eye on
216 % backend requirements and run tests for all variants. Also backend specific code
217 % will still be needed in some cases.
218 %
219 % \section{Use}\label{sec:use}
220 % The PDF management should be loaded as early as possible.
221 % When using \cs{DocumentMetadata}, various PDF related options can
222 % be set in the key-val argument.
223 % The options of \cs{DocumentMetadata} are described in the documentation of
224 % \pkg{ltdocinit} and in \pkg{documentmetadata-support}.
225 %
226 % \begin{verbatim}
227 % \DocumentMetadata % activates the PDF management interface
228 % {
229 % %options
230 % }
231 % \documentclass {...}
232 % \end{verbatim}
233 %
234 % When loading the PDF management as package, options can be set with \cs{SetKeys}:
235 %
236 % \begin{verbatim}
237 % \RequirePackage{pdfmanagement} %loads the PDF management interface
238 % \SetKeys[document/metadata]{
239 % %options
240 % }
241 % \documentclass {...}
242 % \end{verbatim}
243 %
244 % To test if the PDF management has been loaded
245 % \cs{IfPDFManagementActiveTF} can be used.
246 %
247 % \section{Requirements}
248 % The new PDF management is developed parallel to the \LaTeX{} format
249 % and should be updated together with the format.
250 % In some places, e.g. when writing strings to the pdf it assumes that
251 % the file is utf8 encoded -- ascii will naturally work too,
252 % but legacy 8bit encodings are not supported.
253 %
254 % \section{Modules}
255 % The bundle contains a number of modules. The majority of the modules don't
256 % have a stand alone \texttt{sty}, their code is combined
257 % in one file and loaded by the main package. The organization and naming is bound
258 % to change over time: For almost all modules the goal is to
259 % integrate them into the format and the individual files to disappear.
260 %
261 % The description items give the name of the documentation
262 % of the modules. There doesn't exist in all cases a related |.sty|.
263 %
264 % \begin{description}
265 % \item[l3pdfdict] This module provides commands for PDF dictionaries. Its main
266 % purpose is to create name spaces. The code used e.g. by \pkg{l3pdfmanagement} and
267 % \pkg{l3pdfannot}.
268 %
269 % \item[l3pdfannot] This module provides commands for annotations. Currently mainly
270 % link annotations, widget annotations will be added later. It doesn't require
271 % the PDF management to be active.
272 %
273 % \item[l3pdfmanagement] This is the core code of the PDF management.
274 %
275 % \item[ltdocinit] This module provides the \cs{DocumentMetadata} command.
276 %
277 % \item[hyperref-generic] This module provides a new generic hyperref driver.
278 % The driver will
279 % be loaded automatically by hyperref if the PDF management code is active.
280 %
281 % \item[l3backend-pdfmanagement] This module contains backend code needed by the
282 % PDF management. It will in due time be integrated into l3backend.
283 %
284 % \item[l3pdfmeta] This module contains code to handle PDF standards.
285 % Currently it handles pdf/A and colorprofiles/outputintents.
286 %
287 % \item[l3pdfxform] Commands for form XObjects (xforms).
288 %
289 % \item[l3pdf\/tool] A number of commands like text conversion commands and
290 % bcd/emc. The commands will at some time be moved into the \pkg{l3pdf}
291 % module of l3kernel.
292 %
293 % \item[l3pdf\/file] This module provides commands for to embed files.
294 %
295 % \item[pdfmanagement-firstaid] This module provides a number of patches
296 % for external incompatible packages. This patches will disappear as soon as
297 % the packages are natively compatible. It is loaded automatically.
298 %
299 % \item[l3pdffield] Commands for form fields. Currently it only provides commands
300 % for checkboxes. It must be loaded explicitly as with
301 % |\usepackage{l3pdffield}|.
302 %
303 % \end{description}
304 % \section{Incompabilities}
305 %
306 % As described in section~\ref{sec:change},
307 % the new PDF management takes over the management of core PDF dictionaries.
308 % All packages
309 % that bypass the PDF management and access these dictionaries with primitives like
310 % \cs{pdfcatalog}, \cs{pdfinfo}, \cs{pdfpageresources}, \cs{pdfpagesattr}
311 % and \cs{pdfpageattr} or similar commands from other engines and backends are
312 % basically incompatible: values can get lost or will be wrong.
313 %
314 % The following describes known incompatible packages along with some suggestions
315 % how this should or will be handled in future. The list is not exhaustive.
316 %
317 % \subsection{hyperref}
318 % A generic driver that can
319 % be used as replacement has been developed and is provided by this bundle.
320 % It will be loaded automatically if the pdf management is active.
321 %
322 % The generic driver differs in some points from other \pkg{hyperref} drivers:
323 % \begin{itemize}
324 % \item The code for bookmarks has been removed from this driver, instead
325 % the \pkg{bookmarks} is loaded and used.
326 % \item The driver isn't yet fully integrated into hyperref. This means that
327 % it doesn't react to a number of package options. Instead \cs{hypersetup} should
328 % be used.
329 % \item Incomplete is the support for form fields. Quite probably form fields will
330 % be extracted in a dedicated package.
331 % \item The driver uses for the color handling the l3color package. While normally
332 % it should be able to use colors defined with color and xcolor, there could be
333 % edge cases where it fails.
334 % \item The colors have been changed (this counts probably as an improvement \ldots).
335 % \end{itemize}
336 %
337 % More details can be found in the documentation \pkg{hyperref-generic.pdf}.
338 %
339 % \subsection{pdfx}
340 % \pkg{pdfx} is not compatible. It uses the commands \cs{pdfpagesattr}, \cs{pdfpageattr},
341 % \cs{pdfinfo} and \cs{pdfcatalog}. The needed changes are not many, but can
342 % not be done by external patches.
343 %
344 % The PDF management offers support for standards natively. It also writes XMP-metadata
345 % See the documentation of \pkg{l3pdfmeta} for details.
346 %
347 % \subsection{hyperxmp}
348 % \pkg{hyperxmp} uses \cs{pdfcatalog} to insert the \texttt{/MetaData} reference
349 % and also relies on some \pkg{hyperref} internals which are not present in the new
350 % generic driver used by \pkg{hyperref} when the pdfmanagement is active.
351 % This makes \pkg{hyperxmp} incompatible.
352 %
353 % For some time some patch code was provided by the bundle to keep \pkg{hyperxmp}
354 % working but starting with version 0.95s XMP-metadata are handled directly
355 % by the \pkg{pdfmanagement} bundle (see the documentation of \pkg{l3pdfmeta})
356 % and the patch code has been removed and the loading of
357 % \pkg{hyperxmp} has been disabled.
358 %
359 % \subsection{tikz/pgf}
360 % \pkg{pgf} writes to the page resources too and so is incompatible. The needed
361 % changes are rather small and will be done in coordination with the maintainer.
362 % Until this works, the PDF management will load the patches automatically.
363 % This can be disabled by using |debug={firstaidoff=pgf}| in \cs{DocumentMetadata}
364 %
365 % \subsection{transparent}
366 % The package \pkg{transparent} is compatible.
367 %
368 % \subsection{pdflscape}
369 % The package \pkg{pdflscape} is compatible.
370 %
371 % \subsection{colorspace}
372 % The package is incompatible. Some patches
373 % have been added to \pkg{pdfmanagement-firstaid}.
374 % Alternative code for spot colors is
375 % in the \pkg{l3color} package which has now been added to \pkg{l3kernel}.
376 %
377 % \subsection{embedfile, attachfile, attachfile2}
378 % Tools needed to be able to write a replacement
379 % to replace these packages have been developed in the \pkg{l3pdffile} package.
380 % Full replacements for the packages don't exist yet.
381 %
382 % \subsection{ocgx2, animate, media9}
383 % These package all make use of low-level PDF command and will
384 % have to be reviewed.
385 %
386 % \subsection{acrotex}
387 % The \pkg{acrotex} makes heavy use of PDF commands and so must be reviewed and
388 % adapted, including the currently untested route dvips + distiller.
389 %
390 % \subsection{fancytooltips}
391 % This package uses \cs{pdfpageattr} and \pkg{acrotex} and so must be reviewed.
392 % \end{documentation}
393 %
394 % \subsection{bidi}
395 %
396 % The package \pkg{bidi} (loaded e.g. by \pkg{polyglossia} for right-to-left scripts)
397 % patches and redefines internals of \pkg{hyperref} and overwrites
398 % definitions of the generic driver. It is therefore not guarantied to be
399 % compatible with the PDF management if \pkg{hyperref} is used.
400 %
401 % \begin{implementation}
402 %
403 % \section{Implementation}
404 %
405 % \begin{macrocode}
406 %<@@=pdfmanagement>
407 %<*headinit>
408 \ProvidesExplPackage{pdfmanagement-init}{2026-05-26}{0.97c}
409 {LaTeX PDF management bundle}
410
411 \DeclareOption { debug }
412 {
413 \msg_redirect_module:nnn { pdf } { none } { warning }
414 }
415
416 \ProcessOptions\relax
417 %</headinit>
418 %<*headtestphase>
419 \ProvidesExplPackage{pdfmanagement-testphase}{2026-05-26}{0.97c}
420 {LaTeX PDF management bundle}
421
422 \DeclareOption { debug }
423 {
424 \msg_redirect_module:nnn { pdf } { none } { warning }
425 }
426
427 \ProcessOptions\relax
428 %</headtestphase>
429 % \end{macrocode}
430 %<*standalone>
431 % \begin{macrocode}
432 \ProvidesExplPackage{pdfmanagement}{2026-05-26}{0.97c}
433 {LaTeX PDF management bundle}
434 % \end{macrocode}
435 % The only package option is for the backend. Other options can be
436 % set after loading the package with \verb+\SetKeys[document/metadata]+
437 % \begin{macrocode}
438
439 \DeclareKeys[pdfmanagement]
440 {
441 backend .code:n =
442 {
443 \str_if_exist:NTF \c_sys_backend_str
444 {
445 \PackageWarning{pdfmanagement}
446 {
447 backend~is~already~loaded.\MessageBreak
448 'backend=#1'~ignored.
449 }
450 }
451 {
452 \tl_new:N \l__pdfmanagement_backend_tl
453 \tl_set:Nn \l__pdfmanagement_backend_tl {#1}
454 }
455 },
456 backend .usage = load,
457 }
458 \ProcessKeyOptions[pdfmanagement]
459
460 \IfPDFManagementActiveT
461 {
462 \endinput
463 }
464
465 \RequirePackage{pdfmanagement-init}
466 % the backend key must be processed first.
467 % \begin{macrocode}
468 \tl_if_exist:NTF \l_@@_backend_tl
469 {
470 \exp_args:No \sys_load_backend:n { \l_@@_backend_tl }
471 }
472 {
473 \sys_ensure_backend:
474 }
475 \file_input:n {l3backend-pdf-\c_sys_backend_str.def}
476 \hook_use_once:n {pdfmanagement/add}
477 % \end{macrocode}
478 % firstaid should be loaded only after the backend has been set as
479 % it can contain backend dependant code.
480 % \begin{macrocode}
481 \RequirePackage{pdfmanagement-firstaid}
482 %</standalone>
483 % \end{macrocode}
484 %
485 % \subsection{Loading the core file}
486 % This loads the core file. The backend should not be loaded
487 % to allow to set it in the document.
488 % \begin{macrocode}
489 %<*header>
490 \ProvidesExplFile{pdfmanagement.ltx}{2026-05-26}{0.97c}
491 {PDF~management~code}
492 %</header>
493 % \end{macrocode}
494 % \begin{macrocode}
495 %<*package>
496 \RequirePackage{tagpdf-base}
497 \input{pdfmanagement.ltx}
498 % \end{macrocode}
499 % These keys are also needed in documentmetadata-support, so need to be in the init-file:
500 % \begin{macrocode}
501 \keys_define:nn { document / metadata }
502 {
503 ,pdfversion .code:n =
504 {
505 \pdf_version_gset:n { #1 }
506 \AddToDocumentProperties[document]{pdfversion}{#1}
507 }
508 ,uncompress .code:n =
509 {
510 \pdf_uncompress:
511 }
512 ,uncompress .value_forbidden:n = true
513 ,lang .code:n =
514 {
515 \pdfmanagement_add:nnn {Catalog} {Lang}{(#1)}
516 \AddToDocumentProperties[document]{lang}{#1}
517 }
518 ,pdfstandard .code:n =
519 {
520 \clist_map_inline:nn{#1}
521 {
522 \keys_set:ne {document / metadata} {_pdfstandard=\str_uppercase:n{##1}}
523 }
524 }
525 }
526 %</package>
527 % \end{macrocode}
528 % \end{implementation}
529 % \newpage
530 % \PrintIndex