Mercurial > hgrepos > Python2 > PyMuPDF
comparison tests/test_showpdfpage.py @ 1:1d09e1dec1d9 upstream
ADD: PyMuPDF v1.26.4: the original sdist.
It does not yet contain MuPDF. This normally will be downloaded when
building PyMuPDF.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:37:51 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 1:1d09e1dec1d9 |
|---|---|
| 1 """ | |
| 2 Tests: | |
| 3 * Convert some image to a PDF | |
| 4 * Insert it rotated in some rectangle of a PDF page | |
| 5 * Assert PDF Form XObject has been created | |
| 6 * Assert that image contained in inserted PDF is inside given rectangle | |
| 7 """ | |
| 8 import os | |
| 9 | |
| 10 import pymupdf | |
| 11 | |
| 12 scriptdir = os.path.abspath(os.path.dirname(__file__)) | |
| 13 imgfile = os.path.join(scriptdir, "resources", "nur-ruhig.jpg") | |
| 14 | |
| 15 | |
| 16 def test_insert(): | |
| 17 doc = pymupdf.open() | |
| 18 page = doc.new_page() | |
| 19 rect = pymupdf.Rect(50, 50, 100, 100) # insert in here | |
| 20 img = pymupdf.open(imgfile) # open image | |
| 21 tobytes = img.convert_to_pdf() # get its PDF version (bytes object) | |
| 22 src = pymupdf.open("pdf", tobytes) # open as PDF | |
| 23 xref = page.show_pdf_page(rect, src, 0, rotate=-23) # insert in rectangle | |
| 24 # extract just inserted image info | |
| 25 img = page.get_images(True)[0] | |
| 26 assert img[-1] == xref # xref of Form XObject! | |
| 27 img = page.get_image_info()[0] # read the page's images | |
| 28 | |
| 29 # Multiple computations may have lead to rounding deviations, so we need | |
| 30 # some generosity here: enlarge rect by 1 point in each direction. | |
| 31 assert img["bbox"] in rect + (-1, -1, 1, 1) | |
| 32 | |
| 33 def test_2742(): | |
| 34 dest = pymupdf.open() | |
| 35 destpage = dest.new_page(width=842, height=595) | |
| 36 | |
| 37 a5 = pymupdf.Rect(0, 0, destpage.rect.width / 3, destpage.rect.height) | |
| 38 shiftright = pymupdf.Rect(destpage.rect.width/3, 0, destpage.rect.width/3, 0) | |
| 39 | |
| 40 src = pymupdf.open(os.path.abspath(f'{__file__}/../../tests/resources/test_2742.pdf')) | |
| 41 | |
| 42 destpage.show_pdf_page(a5, src, 0) | |
| 43 destpage.show_pdf_page(a5 + shiftright, src, 0) | |
| 44 destpage.show_pdf_page(a5 + shiftright + shiftright, src, 0) | |
| 45 | |
| 46 dest.save(os.path.abspath(f'{__file__}/../../tests/test_2742-out.pdf')) | |
| 47 print("The end!") | |
| 48 | |
| 49 rebased = hasattr(pymupdf, 'mupdf') | |
| 50 if rebased: | |
| 51 wt = pymupdf.TOOLS.mupdf_warnings() | |
| 52 assert wt == ( | |
| 53 'Circular dependencies! Consider page cleaning.\n' | |
| 54 '... repeated 3 times...' | |
| 55 ), f'{wt=}' |
