Mercurial > hgrepos > Python2 > PyMuPDF
comparison tests/test_objectstreams.py @ 3:2c135c81b16c
MERGE: upstream PyMuPDF 1.26.4 with MuPDF 1.26.7
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:44:09 +0200 |
| parents | 1d09e1dec1d9 |
| children |
comparison
equal
deleted
inserted
replaced
| 0:6015a75abc2d | 3:2c135c81b16c |
|---|---|
| 1 import pymupdf | |
| 2 | |
| 3 | |
| 4 def test_objectstream1(): | |
| 5 """Test save option "use_objstms". | |
| 6 This option compresses PDF object definitions into a special object type | |
| 7 "ObjStm". We test its presence by searching for that /Type. | |
| 8 """ | |
| 9 if not hasattr(pymupdf, "mupdf"): | |
| 10 # only implemented for rebased | |
| 11 return | |
| 12 | |
| 13 # make some arbitrary page with content | |
| 14 text = "Hello, World! Hallo, Welt!" | |
| 15 doc = pymupdf.open() | |
| 16 page = doc.new_page() | |
| 17 rect = (50, 50, 200, 500) | |
| 18 | |
| 19 page.insert_htmlbox(rect, text) # place into the rectangle | |
| 20 _ = doc.write(use_objstms=True) | |
| 21 found = False | |
| 22 for xref in range(1, doc.xref_length()): | |
| 23 objstring = doc.xref_object(xref, compressed=True) | |
| 24 if "/Type/ObjStm" in objstring: | |
| 25 found = True | |
| 26 break | |
| 27 assert found, "No object stream found" | |
| 28 | |
| 29 | |
| 30 def test_objectstream2(): | |
| 31 """Test save option "use_objstms". | |
| 32 This option compresses PDF object definitions into a special object type | |
| 33 "ObjStm". We test its presence by searching for that /Type. | |
| 34 """ | |
| 35 if not hasattr(pymupdf, "mupdf"): | |
| 36 # only implemented for rebased | |
| 37 return | |
| 38 | |
| 39 # make some arbitrary page with content | |
| 40 text = "Hello, World! Hallo, Welt!" | |
| 41 doc = pymupdf.open() | |
| 42 page = doc.new_page() | |
| 43 rect = (50, 50, 200, 500) | |
| 44 | |
| 45 page.insert_htmlbox(rect, text) # place into the rectangle | |
| 46 _ = doc.write(use_objstms=False) | |
| 47 | |
| 48 found = False | |
| 49 for xref in range(1, doc.xref_length()): | |
| 50 objstring = doc.xref_object(xref, compressed=True) | |
| 51 if "/Type/ObjStm" in objstring: | |
| 52 found = True | |
| 53 break | |
| 54 assert not found, "Unexpected: Object stream found!" | |
| 55 | |
| 56 | |
| 57 def test_objectstream3(): | |
| 58 """Test ez_save(). | |
| 59 Should automatically use object streams | |
| 60 """ | |
| 61 if not hasattr(pymupdf, "mupdf"): | |
| 62 # only implemented for rebased | |
| 63 return | |
| 64 import io | |
| 65 | |
| 66 fp = io.BytesIO() | |
| 67 | |
| 68 # make some arbitrary page with content | |
| 69 text = "Hello, World! Hallo, Welt!" | |
| 70 doc = pymupdf.open() | |
| 71 page = doc.new_page() | |
| 72 rect = (50, 50, 200, 500) | |
| 73 | |
| 74 page.insert_htmlbox(rect, text) # place into the rectangle | |
| 75 | |
| 76 doc.ez_save(fp) # save PDF to memory | |
| 77 found = False | |
| 78 for xref in range(1, doc.xref_length()): | |
| 79 objstring = doc.xref_object(xref, compressed=True) | |
| 80 if "/Type/ObjStm" in objstring: | |
| 81 found = True | |
| 82 break | |
| 83 assert found, "No object stream found!" |
