Files
PDFReaderLite/scripts/smoke_test.py
T

155 lines
5.4 KiB
Python
Raw Normal View History

2026-06-29 14:50:47 +08:00
from __future__ import annotations
import shutil
import tempfile
import sys
from pathlib import Path
import fitz
from PIL import Image
PROJECT_ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT_ROOT))
from pdfreaderlite.core.document import PdfDocument
def create_pdf(path: Path, text: str = "PDFReaderLite feature test") -> None:
doc = fitz.open()
try:
page = doc.new_page()
page.insert_text((72, 72), text)
doc.save(path)
finally:
doc.close()
2026-06-29 15:12:05 +08:00
def create_form_pdf(path: Path) -> None:
doc = fitz.open()
try:
page = doc.new_page()
widget = fitz.Widget()
widget.field_name = "name"
widget.field_type = fitz.PDF_WIDGET_TYPE_TEXT
widget.rect = fitz.Rect(72, 72, 260, 96)
widget.field_value = ""
page.add_widget(widget)
doc.save(path)
finally:
doc.close()
2026-06-29 14:50:47 +08:00
def main() -> int:
temp_dir = Path(tempfile.mkdtemp(prefix="pdfreaderlite_"))
try:
source_pdf = temp_dir / "source.pdf"
insert_pdf = temp_dir / "insert.pdf"
image_path = temp_dir / "image.png"
extracted_pdf = temp_dir / "extract.pdf"
merged_pdf = temp_dir / "merged.pdf"
image_pdf = temp_dir / "image.pdf"
encrypted_pdf = temp_dir / "encrypted.pdf"
2026-06-29 15:12:05 +08:00
decrypted_pdf = temp_dir / "decrypted.pdf"
form_pdf = temp_dir / "form.pdf"
exported_image = temp_dir / "page.png"
2026-06-29 14:50:47 +08:00
split_folder = temp_dir / "split"
image_export_folder = temp_dir / "images"
2026-06-29 14:50:47 +08:00
create_pdf(source_pdf)
create_pdf(insert_pdf, "inserted page")
2026-06-29 15:12:05 +08:00
create_form_pdf(form_pdf)
2026-06-29 14:50:47 +08:00
Image.new("RGB", (80, 60), (80, 130, 220)).save(image_path)
document = PdfDocument()
document.open(source_pdf)
2026-06-29 18:24:59 +08:00
assert not document.is_dirty
2026-06-29 14:50:47 +08:00
assert document.render_page(0, 1.0).width() > 0
assert document.render_page(0, 1.0).width() > 0
2026-06-29 14:50:47 +08:00
assert document.search("feature")
assert document.highlight_text(0, "feature") == 1
assert document.can_undo()
assert document.undo()
assert document.redo()
assert document.highlight_text(0, "feature") == 1
2026-06-29 14:50:47 +08:00
assert document.underline_text(0, "feature") == 1
assert document.strikeout_text(0, "feature") == 1
document.add_note(0, "note")
2026-06-29 18:24:59 +08:00
assert document.is_dirty
annotations = document.annotations()
assert annotations
assert document.delete_annotation(annotations[0].page_index, annotations[0].xref)
2026-06-29 14:50:47 +08:00
document.add_text(0, "PDFReaderLite")
2026-06-29 16:44:45 +08:00
document.add_text(0, "Placed text", (150, 160))
2026-06-29 17:48:48 +08:00
selected = document.annotation_at_point(0, (155, 165))
assert selected and selected.rect
x0, y0, x1, y1 = selected.rect
assert document.update_annotation_rect(0, selected.xref, (x0 + 10, y0 + 10, x1 + 30, y1 + 30))
2026-06-29 14:50:47 +08:00
document.insert_image(0, image_path)
2026-06-29 16:44:45 +08:00
document.insert_image(0, image_path, (180, 200))
2026-06-29 18:12:05 +08:00
document.insert_image_rect(0, image_path, (220, 120, 280, 170))
document.add_signature(0, image_path)
2026-06-29 16:44:45 +08:00
document.add_signature(0, image_path, (220, 260))
2026-06-29 18:12:05 +08:00
document.add_signature_rect(0, image_path, (300, 220, 370, 260))
2026-06-29 14:50:47 +08:00
document.add_rectangle(0)
document.add_circle(0)
document.add_line(0)
document.add_line(0, arrow=True)
2026-06-29 16:37:26 +08:00
document.add_ink(0, [(80, 120), (120, 140), (160, 130)])
2026-06-29 14:50:47 +08:00
document.add_cover_area(0)
blank_index = document.insert_blank_page(0)
assert blank_index == 1
assert document.insert_pdf(insert_pdf, blank_index) == 1
assert document.page_count == 3
document.move_page(2, 1)
2026-06-29 16:30:27 +08:00
document.reorder_pages([2, 0, 1])
2026-06-29 14:50:47 +08:00
document.extract_pages([1, 3], extracted_pdf)
document.split_to_folder(split_folder)
document.export_page_image(0, exported_image)
document.export_pages_images([1, 2], image_export_folder)
document.update_metadata("PDFReaderLite", "smoke test", "test", "pdf")
2026-06-29 14:50:47 +08:00
PdfDocument.merge_pdfs([source_pdf, insert_pdf], merged_pdf)
PdfDocument.images_to_pdf([image_path], image_pdf)
document.save_encrypted(
encrypted_pdf,
"123456",
allow_print=True,
allow_copy=False,
allow_modify=False,
)
2026-06-29 14:50:47 +08:00
document.save()
2026-06-29 18:24:59 +08:00
assert not document.is_dirty
2026-06-29 14:50:47 +08:00
document.close()
2026-06-29 15:12:05 +08:00
encrypted_document = PdfDocument()
encrypted_document.open(encrypted_pdf, "123456")
encrypted_document.save_as(decrypted_pdf)
encrypted_document.close()
form_document = PdfDocument()
form_document.open(form_pdf)
fields = form_document.form_fields()
assert fields and fields[0].name == "name"
assert form_document.update_form_field("name", "PDFReaderLite")
form_document.save()
form_document.close()
2026-06-29 14:50:47 +08:00
assert extracted_pdf.exists()
assert merged_pdf.exists()
assert image_pdf.exists()
assert encrypted_pdf.exists()
2026-06-29 15:12:05 +08:00
assert decrypted_pdf.exists()
assert exported_image.exists()
2026-06-29 14:50:47 +08:00
assert len(list(split_folder.glob("*.pdf"))) == 3
assert len(list(image_export_folder.glob("*.png"))) == 2
2026-06-29 14:50:47 +08:00
print("PDFReaderLite smoke test passed.")
return 0
finally:
shutil.rmtree(temp_dir, ignore_errors=True)
if __name__ == "__main__":
raise SystemExit(main())