Created
November 20, 2024 14:22
-
-
Save linnil1/b4d06b61809099e9ef6431be5daf107f to your computer and use it in GitHub Desktop.
Decrypt + Watermark + merge + to PDF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from functools import partial | |
| from itertools import chain | |
| import os | |
| def runDocker(image, command): | |
| os.system(f"podman run -it --rm -v $PWD:/app -w /app " f"{image} {command}") | |
| def pdfToImage(name, password): | |
| name_image = name + ".image" | |
| runDocker( | |
| "docker.io/minidocks/poppler", | |
| f"pdftoppm -opw {password} -r 300 {name}.pdf -png {name_image}", | |
| ) | |
| print(f"convert {name}.pdf to {name_image}-*.png") | |
| for i in range(1, 10): | |
| name_image1 = f"{name_image}-{i}" | |
| if not os.path.exists(f"{name_image1}.png"): | |
| break | |
| yield name_image1 | |
| def addWatermark(name, watermark_text): | |
| name_watermark = name + ".watermarked" | |
| runDocker( | |
| "docker.io/dpokidov/imagemagick", | |
| f"{name}.png -font './NotoSansCJKtc-Regular.otf' -pointsize 128 -fill gray " | |
| f"-gravity northwest " | |
| f"-draw \"rotate 30 text 600,0 '{watermark_text}'\" {name_watermark}.png", | |
| ) | |
| print(f"add watermark to {name}.png") | |
| return name_watermark | |
| def mergeImages(names, name_out): | |
| files = map(lambda i: i + ".png", names) | |
| files = " ".join(files) | |
| print(f"merge {files} to {name_out}.png") | |
| runDocker( | |
| "docker.io/dpokidov/imagemagick", | |
| f"{files} -append {name_out}.png" | |
| ) | |
| return name_out | |
| def mergeImagesToPdf(names, name_out): | |
| files = map(lambda i: i + ".png", names) | |
| files = " ".join(files) | |
| print(f"merge {files} to {name_out}.pdf") | |
| runDocker( | |
| "docker.io/dpokidov/imagemagick", | |
| f"-density 300 -page A4 {files} {name_out}.pdf" | |
| ) | |
| return name_out | |
| names = [ | |
| "202310", # 202310.pdf | |
| ] | |
| password = "" | |
| watermark_text = "僅供使用" | |
| names = map(partial(pdfToImage, password=password), names) | |
| names = chain.from_iterable(names) | |
| names = map(partial(addWatermark, watermark_text=watermark_text), names) | |
| # names = map(lambda i: i + ".image-1.watermarked", names) | |
| mergeImagesToPdf(names, "xxx.mergeall") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment