Skip to content

Commit 91524c2

Browse files
committed
Address 4520
We previously refused the use of a page without a /Contents object as source page in method `show_pdf_page`. We now simply assume an empty stream and still create the Form XObject in the target PDF.
1 parent 96d2ebe commit 91524c2

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

docs/page.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,11 +1973,11 @@ In a nutshell, this is what you can do with PyMuPDF:
19731973

19741974
.. method:: show_pdf_page(rect, docsrc, pno=0, keep_proportion=True, overlay=True, oc=0, rotate=0, clip=None)
19751975

1976-
PDF only: Display a page of another PDF as a **vector image** (otherwise similar to :meth:`Page.insert_image`). This is a multi-purpose method. For example, you can use it to:
1976+
PDF only: Display a page of another PDF. This is similar to :meth:`Page.insert_image` but the source page will appear like a copy of itself and will not be rasterized. This is a multi-purpose method. For example, you can use it to:
19771977

19781978
* create "n-up" versions of existing PDF files, combining several input pages into **one output page** (see example `combine.py <https://github.com/pymupdf/PyMuPDF-Utilities/blob/master/examples/combine-pages/combine.py>`_),
19791979
* create "posterized" PDF files, i.e. every input page is split up in parts which each create a separate output page (see `posterize.py <https://github.com/pymupdf/PyMuPDF-Utilities/blob/master/examples/posterize-document/posterize.py>`_),
1980-
* include PDF-based vector images like company logos, watermarks, etc., see `svg-logo.py <https://github.com/pymupdf/PyMuPDF-Utilities/tree/master/examples/svg-logo.py>`_, which puts an SVG-based logo on each page (requires additional packages to deal with SVG-to-PDF conversions).
1980+
* include PDF-based vector images like company logos, watermarks, etc., see `svg-logo.py <https://github.com/pymupdf/PyMuPDF-Utilities/tree/master/examples/svg-logo.py>`_, which puts an SVG-based logo on each page.
19811981

19821982
:arg rect_like rect: where to place the image on current page. Must be finite and its intersection with the page must not be empty.
19831983
:arg docsrc: source PDF document containing the page. Must be a different document object, but may be the same file.
@@ -1994,7 +1994,7 @@ In a nutshell, this is what you can do with PyMuPDF:
19941994

19951995
:arg rect_like clip: choose which part of the source page to show. Default is the full page, else must be finite and its intersection with the source page must not be empty.
19961996

1997-
.. note:: In contrast to method :meth:`Document.insert_pdf`, this method does not copy annotations, widgets or links, so these are not included in the target [#f6]_. But all its **other resources (text, images, fonts, etc.)** will be imported into the current PDF. They will therefore appear in text extractions and in :meth:`get_fonts` and :meth:`get_images` lists -- even if they are not contained in the visible area given by *clip*.
1997+
.. note:: In contrast to method :meth:`Document.insert_pdf`, this method does not copy annotations, widgets or links, so these objects are not included in the target [#f6]_. But all its **other resources (text, images, fonts, etc.)** will be imported into the current PDF. They will therefore appear in text extractions and in :meth:`get_fonts` and :meth:`get_images` lists -- even if they are not contained in the visible area given by *clip*.
19981998

19991999
Example: Show the same source page, rotated by 90 and by -90 degrees:
20002000

src/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17240,7 +17240,7 @@ def JM_read_contents(pageref):
1724017240
elif contents.m_internal:
1724117241
res = mupdf.pdf_load_stream(contents)
1724217242
else:
17243-
res = b""
17243+
res = mupdf.FzBuffer(0)
1724417244
return res
1724517245

1724617246

src/utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ def calc_matrix(sr, tr, keep=True, rotate=0):
174174
while pno < 0: # support negative page numbers
175175
pno += docsrc.page_count
176176
src_page = docsrc[pno] # load source page
177-
if src_page.get_contents() == []:
178-
raise ValueError("nothing to show - source page empty")
179177

180178
tar_rect = rect * ~page.transformation_matrix # target rect in PDF coordinates
181179

tests/test_4520.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pymupdf
2+
3+
4+
def test_4520():
5+
"""Accept source pages without /Contents object in show_pdf_page."""
6+
vsn_tuple = tuple(map(int, pymupdf.__version__.split(".")))
7+
tar = pymupdf.open()
8+
src = pymupdf.open()
9+
src.new_page()
10+
page = tar.new_page()
11+
try:
12+
assert page.show_pdf_page(page.rect, src, 0)
13+
rc = True
14+
except Exception as e:
15+
rc = False
16+
if vsn_tuple < (1, 26, 1):
17+
assert rc is False
18+
else:
19+
assert rc is True

0 commit comments

Comments
 (0)