8000 Fix PDFpages bug by jklymak · Pull Request #9724 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fix PDFpages bug #9724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2591,7 +2591,8 @@ def print_pdf(self, filename, **kwargs):
bbox_inches_restore=_bbox_inches_restore)
self.figure.draw(renderer)
renderer.finalize()
file.finalize()
if not isinstance(filename, PdfPages):
file.finalize()
finally:
if isinstance(filename, PdfPages): # finish off this page
file.endStream()
Expand Down
14 changes: 14 additions & 0 deletions lib/matplotlib/tests/test_backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io
import os
import sys
import tempfile

import pytest
Expand Down Expand Up @@ -71,6 +72,19 @@ def test_multipage_pagecount():
assert pdf.get_pagecount() == 2


def test_multipage_properfinalize():
pdfio = io.BytesIO()
with PdfPages(pdfio) as pdf:
for i in range(10):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('This is a long title')
fig.savefig(pdf, format="pdf")
pdfio.seek(0)
assert sum(b'startxref' in line for line in pdfio) == 1
assert sys.getsizeof(pdfio) < 40000


def test_multipage_keep_empty():
from matplotlib.backends.backend_pdf import PdfPages
from tempfile import NamedTemporaryFile
Expand Down
0