Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@ jupyter lab

// run the script
python unlock_pdf.py locked.pdf unlocked.pdf password
```

### Testing
```bash
tox
```

### uploade to pypi
```bash
twine upload dist/*
```
Empty file added eservice_unlock_pdf/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions eservice_unlock_pdf/tests/test_unlock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import unittest
from eservice_unlock_pdf.unlock import unlock_pdf

class TestUnlockPDF(unittest.TestCase):
def setUp(self):
"""กำหนด path ให้แน่นอน"""
self.input_pdf = os.path.abspath("files/simple.pdf") # ✅ ใช้ absolute path
self.output_pdf = os.path.abspath("outputs/simple-unlocked.pdf")

def test_unlock_pdf(self):
"""ทดสอบการปลดล็อก PDF ด้วยรหัสผ่านที่ถูกต้อง"""
result = unlock_pdf(self.input_pdf, self.output_pdf, "123456789A")
self.assertTrue(result)

def test_wrong_password(self):
"""ทดสอบกรณีใส่รหัสผิด ต้องคืนค่า False"""
result = unlock_pdf(self.input_pdf, self.output_pdf, "wrongpassword")
self.assertFalse(result)

if __name__ == "__main__":
unittest.main()
14 changes: 14 additions & 0 deletions eservice_unlock_pdf/unlock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pikepdf

def unlock_pdf(input_pdf, output_pdf, password):
try:
pdf = pikepdf.open(input_pdf, password=password)
pdf.save(output_pdf)
print(f"✅ PDF ปลดล็อกสำเร็จ! บันทึกที่: {output_pdf}")
return True
except pikepdf.PasswordError: # เช็คกรณีรหัสผ่านผิด
print("❌ รหัสผ่านไม่ถูกต้อง")
return False
except Exception as e: # เช็ค Error อื่น ๆ เช่น ไฟล์ไม่พบ หรือไฟล์ไม่ใช่ PDF
print(f"⚠️ Error: {e}")
return False
9 changes: 9 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from setuptools import setup, find_packages

setup(
name="eservice-unlock-pdf",
version="0.1.0",
packages=find_packages(),
install_requires=[],
test_suite="tests",
)
8 changes: 8 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[tox]
envlist = py38, py39, py310, py311, py312, py313

[testenv]
deps =
pytest
pikepdf
commands = pytest