Use ghostscript's ps2pdf and xpdf's/poppler's pdftops to distill a file. This yields smaller files without illegal encapsulated postscript operators. This distiller is preferred, generating high-level postscript output that treats text as text.
(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False)
| 1330 | |
| 1331 | |
| 1332 | def xpdf_distill(tmpfile, eps=False, ptype='letter', bbox=None, rotated=False): |
| 1333 | """ |
| 1334 | Use ghostscript's ps2pdf and xpdf's/poppler's pdftops to distill a file. |
| 1335 | This yields smaller files without illegal encapsulated postscript |
| 1336 | operators. This distiller is preferred, generating high-level postscript |
| 1337 | output that treats text as text. |
| 1338 | """ |
| 1339 | mpl._get_executable_info("gs") # Effectively checks for ps2pdf. |
| 1340 | mpl._get_executable_info("pdftops") |
| 1341 | |
| 1342 | if eps: |
| 1343 | paper_option = ["-dEPSCrop"] |
| 1344 | elif ptype == "figure": |
| 1345 | # The bbox will have its lower-left corner at (0, 0), so upper-right |
| 1346 | # corner corresponds with paper size. |
| 1347 | paper_option = [f"-dDEVICEWIDTHPOINTS#{bbox[2]}", |
| 1348 | f"-dDEVICEHEIGHTPOINTS#{bbox[3]}"] |
| 1349 | else: |
| 1350 | paper_option = [f"-sPAPERSIZE#{ptype}"] |
| 1351 | |
| 1352 | with TemporaryDirectory() as tmpdir: |
| 1353 | tmppdf = pathlib.Path(tmpdir, "tmp.pdf") |
| 1354 | tmpps = pathlib.Path(tmpdir, "tmp.ps") |
| 1355 | # Pass options as `-foo#bar` instead of `-foo=bar` to keep Windows |
| 1356 | # happy (https://ghostscript.com/doc/9.56.1/Use.htm#MS_Windows). |
| 1357 | cbook._check_and_log_subprocess( |
| 1358 | ["ps2pdf", |
| 1359 | "-dSAFER", |
| 1360 | "-dAutoFilterColorImages#false", |
| 1361 | "-dAutoFilterGrayImages#false", |
| 1362 | "-sAutoRotatePages#None", |
| 1363 | "-sGrayImageFilter#FlateEncode", |
| 1364 | "-sColorImageFilter#FlateEncode", |
| 1365 | *paper_option, |
| 1366 | tmpfile, tmppdf], _log) |
| 1367 | cbook._check_and_log_subprocess( |
| 1368 | ["pdftops", "-paper", "match", "-level3", tmppdf, tmpps], _log) |
| 1369 | shutil.move(tmpps, tmpfile) |
| 1370 | if eps: |
| 1371 | pstoeps(tmpfile) |
| 1372 | |
| 1373 | |
| 1374 | def _get_bbox_header(lbrt): |
nothing calls this directly
no test coverage detected
searching dependent graphs…