|
| 1 | +import datetime |
1 | 2 | import io |
2 | 3 | import os |
3 | 4 | from pathlib import Path |
|
7 | 8 | import numpy as np |
8 | 9 | import pytest |
9 | 10 |
|
| 11 | +import matplotlib as mpl |
10 | 12 | from matplotlib import dviread, pyplot as plt, checkdep_usetex, rcParams |
11 | 13 | from matplotlib.backends.backend_pdf import PdfPages |
12 | 14 | from matplotlib.testing.compare import compare_images |
@@ -125,6 +127,78 @@ def test_composite_image(): |
125 | 127 | assert len(pdf._file._images) == 2 |
126 | 128 |
|
127 | 129 |
|
| 130 | +def test_savefig_metadata(monkeypatch): |
| 131 | + pikepdf = pytest.importorskip('pikepdf') |
| 132 | + monkeypatch.setenv('SOURCE_DATE_EPOCH', '0') |
| 133 | + |
| 134 | + fig, ax = plt.subplots() |
| 135 | + ax.plot(range(5)) |
| 136 | + |
| 137 | + md = { |
| 138 | + 'Author': 'me', |
| 139 | + 'Title': 'Multipage PDF', |
| 140 | + 'Subject': 'Test page', |
| 141 | + 'Keywords': 'test,pdf,multipage', |
| 142 | + 'ModDate': datetime.datetime( |
| 143 | + 1968, 8, 1, tzinfo=datetime.timezone(datetime.timedelta(0))), |
| 144 | + 'Trapped': 'True' |
| 145 | + } |
| 146 | + buf = io.BytesIO() |
| 147 | + fig.savefig(buf, metadata=md, format='pdf') |
| 148 | + |
| 149 | + with pikepdf.Pdf.open(buf) as pdf: |
| 150 | + info = {k: str(v) for k, v in pdf.docinfo.items()} |
| 151 | + |
| 152 | + assert info == { |
| 153 | + '/Author': 'me', |
| 154 | + '/CreationDate': 'D:19700101000000Z', |
| 155 | + '/Creator': f'Matplotlib v{mpl.__version__}, https://matplotlib.org', |
| 156 | + '/Keywords': 'test,pdf,multipage', |
| 157 | + '/ModDate': 'D:19680801000000Z', |
| 158 | + '/Producer': f'Matplotlib pdf backend v{mpl.__version__}', |
| 159 | + '/Subject': 'Test page', |
| 160 | + '/Title': 'Multipage PDF', |
| 161 | + '/Trapped': '/True', |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | +def test_multipage_metadata(monkeypatch): |
| 166 | + pikepdf = pytest.importorskip('pikepdf') |
| 167 | + monkeypatch.setenv('SOURCE_DATE_EPOCH', '0') |
| 168 | + |
| 169 | + fig, ax = plt.subplots() |
| 170 | + ax.plot(range(5)) |
| 171 | + |
| 172 | + md = { |
| 173 | + 'Author': 'me', |
| 174 | + 'Title': 'Multipage PDF', |
| 175 | + 'Subject': 'Test page', |
| 176 | + 'Keywords': 'test,pdf,multipage', |
| 177 | + 'ModDate': datetime.datetime( |
| 178 | + 1968, 8, 1, tzinfo=datetime.timezone(datetime.timedelta(0))), |
| 179 | + 'Trapped': 'True' |
| 180 | + } |
| 181 | + buf = io.BytesIO() |
| 182 | + with PdfPages(buf, metadata=md) as pdf: |
| 183 | + pdf.savefig(fig) |
| 184 | + pdf.savefig(fig) |
| 185 | + |
| 186 | + with pikepdf.Pdf.open(buf) as pdf: |
| 187 | + info = {k: str(v) for k, v in pdf.docinfo.items()} |
| 188 | + |
| 189 | + assert info == { |
| 190 | + '/Author': 'me', |
| 191 | + '/CreationDate': 'D:19700101000000Z', |
| 192 | + '/Creator': f'Matplotlib v{mpl.__version__}, https://matplotlib.org', |
| 193 | + '/Keywords': 'test,pdf,multipage', |
| 194 | + '/ModDate': 'D:19680801000000Z', |
| 195 | + '/Producer': f'Matplotlib pdf backend v{mpl.__version__}', |
| 196 | + '/Subject': 'Test page', |
| 197 | + '/Title': 'Multipage PDF', |
| 198 | + '/Trapped': '/True', |
| 199 | + } |
| 200 | + |
| 201 | + |
128 | 202 | def test_pdfpages_fspath(): |
129 | 203 | with PdfPages(Path(os.devnull)) as pdf: |
130 | 204 | pdf.savefig(plt.figure()) |
|
0 commit comments