Skip to content

Commit d699a5c

Browse files
committed
Make _pushPopGS vastly more efficient
Don't re-parse the entire content stream just to add some pre/post ops. Just write the pre-post ops directly, with the original content in between.
1 parent 11340a3 commit d699a5c

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

PyPDF2/pdf.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,9 +2197,16 @@ def _pushPopGS(contents, pdf):
21972197
# adds a graphics state "push" and "pop" to the beginning and end
21982198
# of a content stream. This isolates it from changes such as
21992199
# transformation matricies.
2200-
stream = ContentStream(contents, pdf)
2201-
stream.operations.insert(0, [[], "q"])
2202-
stream.operations.append([[], "Q"])
2200+
if not isinstance(contents, ArrayObject):
2201+
contents = [contents]
2202+
data = BytesIO()
2203+
data.write(b"q\n")
2204+
for item in contents:
2205+
data.write(item.getData())
2206+
data.write(b"\n")
2207+
data.write(b"Q\n")
2208+
stream = DecodedStreamObject()
2209+
stream.setData(data.getvalue())
22032210
return stream
22042211
_pushPopGS = staticmethod(_pushPopGS)
22052212

0 commit comments

Comments
 (0)