8000 pgf: Pass PDF metadata via pdfinfo option. · matplotlib/matplotlib@c4613f8 · GitHub
[go: up one dir, main page]

Skip to content

Commit c4613f8

Browse files
committed
pgf: Pass PDF metadata via pdfinfo option.
This takes keys that match the PDF specification instead of lower-case, 'pdf'-prefixed keys, so is a bit simpler. Also, deprecate ability to accept key case-insensitively, as that is not done in the PDF backend.
1 parent e53a00c commit c4613f8

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

doc/api/api_changes_3.3/deprecations.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,3 +593,11 @@ APIs which support the values True, False, and "TeX" for ``ismath``.
593593
``matplotlib.ttconv``
594594
~~~~~~~~~~~~~~~~~~~~~
595595
This module is deprecated.
596+
597+
Stricter PDF metadata keys in PGF
598+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
599+
Saving metadata in PDF with the PGF backend currently normalizes all keys to
600+
lowercase, unlike the PDF backend, which only accepts the canonical case. This
601+
is deprecated; in a future version, only the canonically cased keys listed in
602+
the PDF specification (and the `~.backend_pgf.PdfPages` documentation) will be
603+
accepted.

lib/matplotlib/backends/backend_pgf.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,20 @@ def __init__(self, filename, *, keep_empty=True, metadata=None):
10171017
self._outputfile = filename
10181018
self._n_figures = 0
10191019
self.keep_empty = keep_empty
1020-
self.metadata = metadata or {}
1020+
self.metadata = (metadata or {}).copy()
1021+
if metadata:
1022+
for key in metadata:
1023+
canonical = {
1024+
'creationdate': 'CreationDate',
1025+
'moddate': 'ModDate',
1026+
}.get(key.lower(), key.lower().title())
1027+
if canonical != key:
1028+
cbook.warn_deprecated(
1029+
'3.3', message='Support for setting PDF metadata keys '
1030+
'case-insensitively is deprecated since %(since)s and '
1031+
'will be removed %(removal)s; '
1032+
f'set {canonical} instead of {key}.')
1033+
self.metadata[canonical] = self.metadata.pop(key)
10211034

10221035
# create temporary directory for compiling the figure
10231036
self._tmpdir = tempfile.mkdtemp(prefix="mpl_pgf_pdfpages_")
@@ -1028,27 +1041,28 @@ def __init__(self, filename, *, keep_empty=True, metadata=None):
10281041

10291042
def _write_header(self, width_inches, height_inches):
10301043
supported_keys = {
1031-
'title', 'author', 'subject', 'keywords', 'creator',
1032-
'producer', 'trapped'
1044+
'Title', 'Author', 'Subject', 'Keywords', 'Creator',
1045+
'Producer', 'Trapped'
10331046
}
10341047
infoDict = {
1035-
'creator': f'matplotlib {mpl.__version__}, https://matplotlib.org',
1036-
'producer': f'matplotlib pgf backend {mpl.__version__}',
1048+
'Creator': f'matplotlib {mpl.__version__}, https://matplotlib.org',
1049+
'Producer': f'matplotlib pgf backend {mpl.__version__}',
1050+
**self.metadata
10371051
}
1038-
metadata = {k.lower(): v for k, v in self.metadata.items()}
1039-
infoDict.update(metadata)
10401052
hyperref_options = ''
10411053
for k, v in infoDict.items():
10421054
if k not in supported_keys:
10431055
raise ValueError(
10441056
'Not a supported pdf metadata field: "{}"'.format(k)
10451057
)
1046-
hyperref_options += 'pdf' + k + '={' + str(v) + '},'
1058+
hyperref_options += k + '={' + str(v) + '},'
10471059

10481060
latex_preamble = get_preamble()
10491061
latex_fontspec = get_fontspec()
10501062
latex_header = r"""\PassOptionsToPackage{{
1051-
{metadata}
1063+
pdfinfo={{
1064+
{metadata}
1065+
}}
10521066
}}{{hyperref}}
10531067
\RequirePackage{{hyperref}}
10541068
\documentclass[12pt]{{minimal}}

lib/matplotlib/tests/test_backend_pgf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def test_pdf_pages_metadata():
255255
ax.plot(range(5))
256256
fig.tight_layout()
257257

258-
md = {'author': 'me', 'title': 'Multipage PDF with pgf'}
258+
md = {'Author': 'me', 'Title': 'Multipage PDF with pgf'}
259259
path = os.path.join(result_dir, 'pdfpages_meta.pdf')
260260

261261
with PdfPages(path, metadata=md) as pdf:
@@ -282,7 +282,7 @@ def test_pdf_pages_lualatex():
282282
ax.plot(range(5))
283283
fig.tight_layout()
284284

285-
md = {'author': 'me', 'title': 'Multipage PDF with pgf'}
285+
md = {'Author': 'me', 'Title': 'Multipage PDF with pgf'}
286286
path = os.path.join(result_dir, 'pdfpages_lua.pdf')
287287
with PdfPages(path, metadata=md) as pdf:
288288
pdf.savefig(fig)

0 commit comments

Comments
 (0)
0