-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrop_pdf.py
121 lines (99 loc) · 4.34 KB
/
crop_pdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
import sys
import shutil
import fitz
import shlex
import ghostscript
from PyQt5.QtWidgets import QApplication, QMenu, QFileDialog, QMainWindow, QAction
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import Qt
from mouse_drag import MyWidget
HOME_DIR = os.path.expanduser("~")
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.doc = None
self.activate_dir = None
self.setWindowTitle("Crop PDF")
self.pdfbox = MyWidget()
self.createActions()
self.createMenuBar()
self.pdfbox.sig_mouse_released[int, int, int, int].connect(self.crop_all_pages)
self.setCentralWidget(self.pdfbox)
def createActions(self):
# Creating actions using the second constructor
self.openAction = QAction("&Open...", self)
self.openAction.triggered.connect(self.update_image)
self.exitAction = QAction("&Exit", self)
self.exitAction.triggered.connect(self.close)
self.cropAction = QAction("&Crop", self)
self.cropAction.triggered.connect(self.enable_selection)
self.compressAction = QAction("&Compress", self)
self.compressAction.triggered.connect(self.on_comp)
def createMenuBar(self):
menuBar = self.menuBar()
fileMenu = QMenu("&File", self)
fileMenu.addAction(self.openAction)
fileMenu.addAction(self.exitAction)
toolMenu = QMenu("&Tool", self)
toolMenu.addAction(self.cropAction)
toolMenu.addAction(self.compressAction)
menuBar.addMenu(fileMenu)
menuBar.addMenu(toolMenu)
def update_image(self):
fileName, _ = QFileDialog.getOpenFileName(
self, "Open Pdf", HOME_DIR, "Pdf Files (*.pdf)"
)
if fileName:
self.doc = fitz.open(fileName)
page = self.doc.load_page(3)
pix = page.get_pixmap()
# set the correct QImage format depending on alpha
fmt = QImage.Format_RGBA8888 if pix.alpha else QImage.Format_RGB888
qtimg = QImage(pix.samples_ptr, pix.width, pix.height, fmt)
self.pdfbox.setPixmap(QPixmap.fromImage(qtimg))
self.activate_dir = os.path.dirname(fileName)
def crop_all_pages(self, sx, sy, ex, ey):
if ex - sx > 10 and ey - sy > 10:
for number_of_page in range(self.doc.page_count):
page = self.doc.load_page(number_of_page)
page.set_mediabox(fitz.Rect(sx, sy, ex, ey))
fileName, _ = QFileDialog.getSaveFileName(
self, "Save to PDF", self.activate_dir, "Pdf Files (*.pdf)"
)
if fileName:
QApplication.setOverrideCursor(Qt.WaitCursor)
self.doc.save(fileName)
self.destructive_crop(fileName)
QApplication.restoreOverrideCursor()
def on_comp(self):
fileName, _ = QFileDialog.getSaveFileName(
self, "Save to PDF", self.activate_dir, "Pdf Files (*.pdf)"
)
if fileName:
QApplication.setOverrideCursor(Qt.WaitCursor)
self.doc.save(fileName)
self.compress_pdf_gh(fileName)
QApplication.restoreOverrideCursor()
def destructive_crop(self, file_path):
command = f"gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile={file_path}.pdf {file_path}"
ghostscript.Ghostscript(*shlex.split(command))
shutil.move(f"{file_path}.pdf", file_path)
# -dCompatibilityLevel=1.4 ok, 1.3 not working
def compress_pdf_gh(self, file_path):
command = f'''gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -dCompatibilityLevel=1.4
-dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true
-dColorImageDownsampleType=/Bicubic -dColorImageResolution=144
-dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=144
-dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=144
-sOutputFile={file_path}.pdf {file_path}'''
ghostscript.Ghostscript(*shlex.split(command))
shutil.move(f"{file_path}.pdf", file_path)
def enable_selection(self):
if self.doc is not None:
self.pdfbox.activate_selection = True
if __name__ == "__main__":
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())