-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf_merger.py
1540 lines (1267 loc) · 53.9 KB
/
pdf_merger.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""A GUI for selecting files to merge into one PDF file and save.
Can use the file formats natively supported by pymupdf (PDF, XPS, EPUB, HTML),
as well as image formats (JPG, TIF, PNG, SVG, GIF, BMP) and text files.
Also allows selecting the first page, last page, and rotation of each individual file
and allows previewing the merged file before saving.
@author: Donald Erb
Created on 2021-02-04
Copyright (C) 2020 Donald Erb
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
(See LICENSE.txt in this repository)
Requirements
------------
fitz>=1.18.4
fitz is pymupdf; versions after 1.18.4 have snake_case rather than
CamelCase or mixedCase.
wx>=4.0
wx is wxpython; v4.0 is the first to work with python v3.
To install:
pip install pymupdf>=1.18.4 wxPython>4.0
License
-------
This program is licensed under the GNU AGPL V3+ license (GNU AFFERO GPL).
History
-------
2021-10-16, Donald Erb
Added SAFE_SAVE attribute to allow saving with the default values,
in case the default saving options cause issues viewing the output
pdf.
Attributes
----------
CENTER_IMAGES_H : bool
If True (default) and if FULL_PAGE_IMAGES is True, then will center
images horizontally on the page.
CENTER_IMAGES_V : bool
If True (default) and if FULL_PAGE_IMAGES is True, then will center
images vertically on the page.
EXPAND_IMAGES : bool
If True (default), will expand images to fit the size specified by
PAGE_LAYOUT while retaining the original aspect ratio. If False, the
pdf page with the image will directly fit the image's original size.
FONT : str
The font name to use. If FONT_PATH is None, then it must be the name
of a font covered by pymupdf. Default is 'helvetica'. Font is only
used for converting text files to pdf.
FONT_PATH : str or None
The file path to the font file to use, if not using a built-in font
from pymupdf. Default is None, which means that FONT is a built-in font.
Only used if converting text files to pdf.
FONT_SIZE : int
The font size. Used for converting epub, html, and text files to pdf.
Default is 11.
FULL_PAGE_IMAGES : bool
If True, then images will retain their default size, or will be shrunked
to fit the page size. This way, smaller images can keep their size, rather
than being expanded to fill the page. Default is False. If True, images by
default will be placed in the top-left corner, but can be centered using
CENTER_IMAGES_H and CENTER_IMAGES_V.
PAGE_LAYOUT : str
A string designating the paper size to use. Must be a valid input for
fitz.PaperSize. Default is 'letter'. Append '-l' to change the page
orientation.
USE_LANDSCAPE : bool
If True, will append '-l' to PAGE_LAYOUT before passing it to fitz.PaperSize.
SAFE_SAVE : bool
If True, will use the default options when saving, which do not compression
or cleaning of the file. If False (default), will use the following options
when saving:
garbage=4, deflate=1
Included since the more aggressive garbage collection used when SAFE_SAVE is False
can sometimes cause issues when viewing the pdfs in Adobe (although the pdfs are fine
when using other software like Chrome or Firefox to view the pdfs).
Notes
-----
If both EXPAND_IMAGES and FULL_PAGE_IMAGES are False when making a pdf page from an
image, then the pdf page will be made to fit the image, regardless of its size.
"""
import base64
import functools
import io
import os
from pathlib import Path
import re
import textwrap
import traceback
import fitz
import wx
import wx.grid
fitz.TOOLS.mupdf_display_errors(False)
CENTER_IMAGES_H = True
CENTER_IMAGES_V = True
EXPAND_IMAGES = True
FONT = 'Helvetica'
FONT_PATH = None
FONT_SIZE = 11
FULL_PAGE_IMAGES = False
PAGE_LAYOUT = 'letter'
USE_LANDSCAPE = False
SAFE_SAVE = False
if Path(__file__).parent.joinpath('logo.png').is_file():
with Path(__file__).parent.joinpath('logo.png').open('rb') as fp:
LOGO = base64.encodebytes(fp.read())
else:
LOGO = None
# paper_sizes function replaced the paperSizes dictionary in a fitz version > 1.18.4
try:
PAPER_SIZES = fitz.paper_sizes()
except AttributeError:
PAPER_SIZES = fitz.paperSizes
class SettingsDialog(wx.Dialog):
"""
Allows changing the default settings for page configuration.
Parameters
----------
parent : wx.Window, optional
The parent widget for the dialog.
**kwargs
Any additional keyword arguments for initializing wx.Dialog.
"""
def __init__(self, parent=None, **kwargs):
super().__init__(parent, **kwargs)
main_sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer_1 = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(sizer_1, 1, wx.ALL | wx.EXPAND, 5)
label_1 = wx.StaticText(
self, label='Note: these settings are not used\nif directly using PDF files.',
style=wx.ALIGN_CENTER_HORIZONTAL
)
sizer_1.Add(label_1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.BOTTOM, 15)
self.expand_images = wx.RadioButton(
self, label='Expand/shrink images to fill page', style=wx.RB_GROUP
)
self.expand_images.SetValue(EXPAND_IMAGES)
sizer_1.Add(self.expand_images, 0, wx.BOTTOM | wx.TOP, 5)
self.fit_pg_image = wx.RadioButton(self, label='Fit page to image')
self.fit_pg_image.SetValue(not EXPAND_IMAGES and not FULL_PAGE_IMAGES)
sizer_1.Add(self.fit_pg_image, 0, wx.BOTTOM | wx.TOP, 5)
self.full_pg_image = wx.RadioButton(self, label='Keep image and page sizes')
self.full_pg_image.SetValue(FULL_PAGE_IMAGES)
sizer_1.Add(self.full_pg_image, 0, wx.BOTTOM | wx.TOP, 5)
self.center_images_h = wx.CheckBox(self, label='Center images horizontally')
sizer_1.Add(self.center_images_h, 0, wx.LEFT, 10)
self.center_images_v = wx.CheckBox(self, label='Center images vertically')
sizer_1.Add(self.center_images_v, 0, wx.LEFT | wx.BOTTOM, 10)
if FULL_PAGE_IMAGES:
self.center_images_h.SetValue(CENTER_IMAGES_H)
self.center_images_v.SetValue(CENTER_IMAGES_V)
else:
self.center_images_h.SetValue(False)
self.center_images_v.SetValue(False)
self.center_images_h.Enable(False)
self.center_images_v.Enable(False)
# filler
sizer_1.Add((20, 20))
sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
sizer_1.Add(sizer_6, 1, wx.BOTTOM | wx.EXPAND | wx.TOP, 5)
label_4 = wx.StaticText(self, label='Page Layout')
sizer_6.Add(label_4, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 10)
paper_sizes = list(PAPER_SIZES.keys())
self.page_layout = wx.Choice(self, wx.ID_ANY, choices=paper_sizes)
if PAGE_LAYOUT in PAPER_SIZES:
selection = paper_sizes.index(PAGE_LAYOUT)
else:
selection = paper_sizes.index('letter')
self.page_layout.SetSelection(selection)
sizer_6.Add(self.page_layout, 0, wx.ALIGN_CENTER_VERTICAL, 0)
self.landscape = wx.CheckBox(self, label='Use landscape (makes width > height)')
self.landscape.SetValue(USE_LANDSCAPE)
sizer_1.Add(self.landscape, 0, wx.BOTTOM | wx.TOP, 5)
self.safe_save = wx.CheckBox(self, label='Safe Save? Check if the output files have issues')
self.safe_save.SetValue(SAFE_SAVE)
sizer_1.Add(self.safe_save, 0, wx.BOTTOM | wx.TOP, 5)
# filler
sizer_1.Add((20, 20))
sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
sizer_1.Add(sizer_4, 1, wx.BOTTOM | wx.EXPAND | wx.TOP, 5)
label_2 = wx.StaticText(self, label='Font')
sizer_4.Add(label_2, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 10)
self.font = wx.Choice(self, choices=fitz.Base14_fontnames)
if FONT in fitz.Base14_fontnames:
selection = fitz.Base14_fontnames.index(FONT)
else:
selection = fitz.Base14_fontnames.index('Helvetica')
self.font.SetSelection(selection)
sizer_4.Add(self.font, 0, wx.ALIGN_CENTER_VERTICAL, 0)
sizer_5 = wx.BoxSizer(wx.HORIZONTAL)
sizer_1.Add(sizer_5, 1, wx.BOTTOM | wx.EXPAND | wx.TOP, 5)
label_3 = wx.StaticText(self, label='Font Size')
sizer_5.Add(label_3, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 10)
self.font_size = wx.SpinCtrl(self, value=str(int(float(FONT_SIZE))), min=1, max=256)
sizer_5.Add(self.font_size, 0, wx.ALIGN_CENTER_VERTICAL, 0)
# filler
sizer_1.Add((20, 30))
sizer_3 = wx.StdDialogButtonSizer()
sizer_1.Add(sizer_3, 0, wx.ALIGN_RIGHT | wx.ALL, 5)
self.button_OK = wx.Button(self, wx.ID_OK)
self.button_OK.SetDefault()
sizer_3.AddButton(self.button_OK)
self.button_CANCEL = wx.Button(self, wx.ID_CANCEL)
sizer_3.AddButton(self.button_CANCEL)
sizer_3.Realize()
self.SetAffirmativeId(self.button_OK.GetId())
self.SetEscapeId(self.button_CANCEL.GetId())
self.SetSizer(main_sizer)
main_sizer.Fit(self)
self.expand_images.Bind(wx.EVT_RADIOBUTTON, self.on_radio)
self.full_pg_image.Bind(wx.EVT_RADIOBUTTON, self.on_radio)
self.fit_pg_image.Bind(wx.EVT_RADIOBUTTON, self.on_radio)
def on_radio(self, event):
"""Enables or disables image options depending on self.full_pg_image."""
checked = self.full_pg_image.GetValue()
self.center_images_h.SetValue(checked)
self.center_images_v.SetValue(checked)
self.center_images_h.Enable(checked)
self.center_images_v.Enable(checked)
def set_options(self):
"""Overrides the global config variables."""
global CENTER_IMAGES_H
global CENTER_IMAGES_V
global EXPAND_IMAGES
global FONT
global FONT_SIZE
global FULL_PAGE_IMAGES
global PAGE_LAYOUT
global USE_LANDSCAPE
global SAFE_SAVE
CENTER_IMAGES_H = self.center_images_h.GetValue()
CENTER_IMAGES_V = self.center_images_v.GetValue()
EXPAND_IMAGES = self.expand_images.GetValue()
FONT = self.font.GetStringSelection()
FONT_SIZE = self.font_size.GetValue()
FULL_PAGE_IMAGES = self.full_pg_image.GetValue()
PAGE_LAYOUT = self.page_layout.GetStringSelection()
USE_LANDSCAPE = self.landscape.GetValue()
SAFE_SAVE = self.safe_save.GetValue()
class PagesGrid(wx.grid.Grid):
"""
Contains information about the pages and rotation of pdf files.
Parameters
----------
parent : wx.Window
The parent widget.
**kwargs
Any additional keyword arguments for initializing wx.Grid.
"""
def __init__(self, parent, **kwargs):
super().__init__(parent, **kwargs)
self.CreateGrid(0, 5)
self.EnableDragRowSize(False)
self.HideCol(4) # column 4 is just to hold the total number of pages
self.SetSelectionMode(wx.grid.Grid.SelectRows)
attr = wx.grid.GridCellAttr()
attr.SetReadOnly(True)
self.SetColAttr(0, attr)
attr.IncRef()
attr = wx.grid.GridCellAttr()
attr.SetAlignment(wx.ALIGN_CENTER, -1)
for col in range(1, 4):
self.SetColAttr(col, attr)
attr.IncRef()
attr = wx.grid.GridCellAttr()
attr.SetAlignment(wx.ALIGN_CENTER, -1)
attr.SetReadOnly(True)
self.SetColAttr(4, attr)
self.SetColLabelValue(0, 'File')
self.SetColSize(0, 400)
self.SetColLabelValue(1, 'First Page')
self.SetColLabelValue(2, 'Last Page')
self.SetColLabelValue(3, 'Rotation')
self.SetColLabelValue(4, 'Total Pages')
def add_row(self, file_path, row_index=None):
"""
Appends a row to the grid with information after opening the given file.
Parameters
----------
file_path : str or os.Pathlike
The path of the pdf file to open and read.
row_index : int, optional
The index to insert a new row. Default is None, which will append
the row to the grid.
"""
try:
pdf = get_pdf(file_path)
if pdf.needs_pass: # password protected
raise ValueError('File is encrypted and cannot be processed')
pages = len(pdf)
except Exception:
with wx.MessageDialog(
self,
(f'Problem opening {file_path}\n\nError:\n {traceback.format_exc()}'),
) as dlg:
dlg.ShowModal()
return
finally:
try:
pdf.close()
except Exception:
pass
if row_index is None:
row = self.GetNumberRows()
self.AppendRows(1)
else:
row = row_index
self.InsertRows(row)
self.create_row(row, file_path, pages)
def create_row(self, row, file_path, total_pages, first_page='1',
last_page=None, rotation=None):
"""
Adds data about a pdf file to a row in the grid.
Note that this function does not handle the actual creation of the row, so
that must be done before calling this method.
Parameters
----------
row : int
The index of the row to add information to.
file_path : str or os.Pathlike
The file path of the pdf file.
total_pages : int
The total number of pages in the pdf file.
first_page : str or int, optional
The first page of the pdf to use; by default '1'.
last_page : str or int, optional
The last page of the pdf to use. If None, will be set to the last page.
rotation : {0, 90, -90, 180}, optional
The integer rotation (in degrees) of the pdf file.
"""
pages = [str(page + 1) for page in range(total_pages)]
first_pg = first_page if str(first_page) in pages else pages[0]
last_pg = last_page if last_page is not None else pages[-1]
rotations = {0: '0°', -90: '-90° (left)', 90: '90° (right)', 180: '180°'}
if rotation is None:
start_rotation = rotations[0]
elif int(rotation) in rotations.keys():
start_rotation = rotations[int(rotation)]
elif str(rotation) in rotations.values():
start_rotation = str(rotation)
else:
start_rotation = rotations[0]
self.SetCellValue(row, 0, str(file_path))
self.SetCellEditor(row, 1, wx.grid.GridCellChoiceEditor(pages, False))
self.SetCellValue(row, 1, str(first_pg))
self.SetCellEditor(row, 2, wx.grid.GridCellChoiceEditor(pages, False))
self.SetCellValue(row, 2, str(last_pg))
self.SetCellEditor(row, 3, wx.grid.GridCellChoiceEditor(list(rotations.values()), False))
self.SetCellValue(row, 3, start_rotation)
self.SetCellValue(row, 4, str(total_pages))
def get_values(self):
"""
Returns the relevant info for each pdf file in the grid.
Returns
-------
data : list(list(str, int, int, int))
A list of lists. Each internal list should have four items, telling
the file path for each pdf to merge, the first page to use, the last
page to use, and the rotation. Each individual entry is as follows:
file_path: str
The collection of files to merge into a single document and saved.
first_pg: int
The first page to use. 0-based.
last_pg: int
The last page to use. 0-based. If less than the first_pg, then the
page order will be reversed.
rotations : {0, 90, -90, 180}
The integer rotation to apply to the document. Note that -90 will
rotate the document left (counter-clockwise).
"""
data = []
for row in range(self.GetNumberRows()):
data.append([
self.GetCellValue(row, 0),
int(self.GetCellValue(row, 1)) - 1,
int(self.GetCellValue(row, 2)) - 1,
int(self.GetCellValue(row, 3).split('°')[0]),
])
return data
class PDFMerger(wx.Frame):
"""
A frame for selecting PDF files to merge.
Also allows selecting the first page, last page, and rotation of each
individual PDF and viewing a preview of the output file.
Parameters
----------
parent : wx.Window, optional
The parent widget for this frame. Default is None.
**kwargs
Any additional keyword arguments for initializing wx.Frame.
"""
def __init__(self, parent=None, **kwargs):
super().__init__(parent, **kwargs)
self.SetSize(self.FromDIP((900, 500)))
self.preview = None
if LOGO is not None:
self.SetIcon(wx.Icon(wx.Image(io.BytesIO(base64.b64decode(LOGO))).ConvertToBitmap()))
self.menubar = wx.MenuBar()
self.options_menu = wx.Menu('Set Options')
self.menubar.Append(self.options_menu, "Options")
self.SetMenuBar(self.menubar)
self.panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(sizer_1, 0, wx.EXPAND, 0)
grid_sizer_1 = wx.GridSizer(1, 2, 0, 20)
sizer_1.Add(grid_sizer_1, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 10)
self.add_btn = wx.Button(self.panel, label='Add Files')
grid_sizer_1.Add(self.add_btn, 0, wx.EXPAND, 0)
self.remove_btn = wx.Button(self.panel, label='Remove Files')
grid_sizer_1.Add(self.remove_btn, 0, wx.EXPAND, 0)
label_1 = wx.StaticText(
self.panel,
label=('Use "Add Files" to add multiple files, "Remove Files" to'
'\nremove selected files, and ▲ or ▼ to reorder files.'),
style=wx.ALIGN_CENTER_HORIZONTAL
)
sizer_1.Add(label_1, 1, wx.BOTTOM | wx.TOP, 5)
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(sizer_2, 1, wx.ALL | wx.EXPAND, 8)
sizer_5 = wx.BoxSizer(wx.VERTICAL)
sizer_2.Add(sizer_5, 1, wx.EXPAND, 0)
self.grid = PagesGrid(self.panel, size=(1, 1))
sizer_5.Add(self.grid, 1, wx.ALL | wx.EXPAND, 0)
sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
sizer_5.Add(sizer_3, 0, wx.BOTTOM | wx.EXPAND | wx.TOP, 20)
self.output_file = wx.TextCtrl(self.panel, style=wx.TE_READONLY)
sizer_3.Add(self.output_file, 1, wx.EXPAND | wx.RIGHT, 5)
self.saveas_btn = wx.Button(self.panel, wx.ID_SAVEAS)
sizer_3.Add(self.saveas_btn, 0, wx.ALIGN_CENTER_VERTICAL, 30)
grid_sizer_2 = wx.GridSizer(2, 1, 0, 0)
sizer_2.Add(grid_sizer_2, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 10)
self.up_btn = wx.Button(self.panel, wx.ID_ANY, '▲', style=wx.BU_EXACTFIT)
grid_sizer_2.Add(self.up_btn, 0, wx.EXPAND, 0)
self.down_btn = wx.Button(self.panel, wx.ID_ANY, '▼', style=wx.BU_EXACTFIT)
grid_sizer_2.Add(self.down_btn, 0, wx.EXPAND, 0)
sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(sizer_4, 0, wx.ALL, 10)
self.save_btn = wx.Button(self.panel, wx.ID_SAVE)
sizer_4.Add(self.save_btn, 0, wx.RIGHT, 10)
self.preview_btn = wx.Button(self.panel, label='Preview')
sizer_4.Add(self.preview_btn, 0, 0, 0)
self.panel.SetSizer(sizer)
self.Layout()
self.add_btn.Bind(wx.EVT_BUTTON, self.on_add)
self.remove_btn.Bind(wx.EVT_BUTTON, self.on_remove)
self.up_btn.Bind(wx.EVT_BUTTON, self.move_up)
self.down_btn.Bind(wx.EVT_BUTTON, self.move_down)
self.saveas_btn.Bind(wx.EVT_BUTTON, self.on_saveas)
self.save_btn.Bind(wx.EVT_BUTTON, self.on_save)
self.preview_btn.Bind(wx.EVT_BUTTON, self.on_preview)
self.Bind(wx.EVT_MENU, self.set_options, self.menubar)
def set_options(self, event):
"""
Launches dialog to override the global settings.
If the dialog is confirmed, then the grid data will be reset for all file
types except for pdf and xps files so that the page information can be
updated. Image files will keep their rotations as well.
"""
reset_grid = False
with SettingsDialog(self, title='Options') as dialog:
if dialog.ShowModal() == wx.ID_OK:
dialog.set_options()
reset_grid = True
if reset_grid:
grid_data = self.grid.get_values()
rotations = {0: '0°', -90: '-90° (left)', 90: '90° (right)', 180: '180°'}
for row, row_data in enumerate(grid_data):
suffix = Path(row_data[0]).suffix.lower()
if re.search('.*xps|pdf', suffix) is None:
self.grid.DeleteRows(row)
self.grid.add_row(row_data[0], row)
if is_image(suffix):
self.grid.SetCellValue(row, 3, rotations[row_data[3]])
event.Skip()
def on_add(self, event):
"""Launches file dialog to select pdf files and adds them to the grid."""
paths = []
with wx.FileDialog(
self, 'Select files',
wildcard=(
'PDF Files|*.pdf|Image Files|*.jp*;*.png;*.tif*;*.svg;*.gif;*.bmp|'
'XPS Files|*.*xps|HTML Files|*.htm*|EPUB Files|*.epub|All Files|*.*'
),
style=wx.FD_OPEN | wx.FD_CHANGE_DIR | wx.FD_FILE_MUST_EXIST | wx.FD_MULTIPLE
) as dialog:
if dialog.ShowModal() == wx.ID_OK:
# maybe not necessary, but ensures paths are correct for all os
paths = [str(Path(path)) for path in dialog.GetPaths()]
for path in paths:
self.grid.add_row(path)
def on_remove(self, event):
"""Removes selected files from the grid."""
rows = self.grid.GetSelectedRows()
for row in reversed(rows):
self.grid.DeleteRows(row)
self.grid.ClearSelection()
def _move(self, move_down=False):
"""Moves all selected grid rows up or down, if possible."""
rows = self.grid.GetSelectedRows()
if not rows:
return
self.grid.ClearSelection()
failed_moves = []
if move_down:
new_row = lambda x: x + 2
old_row = lambda x: x
select_row = lambda x: x + 1
failed_row = lambda x: x + 1
rows = sorted(rows, reverse=True)
if rows[0] == self.grid.GetNumberRows() - 1:
failed_moves.append(rows.pop(0) + 1)
else:
new_row = lambda x: x - 1
old_row = lambda x: x + 1
select_row = lambda x: x - 1
failed_row = lambda x: x
if rows[0] == 0:
failed_moves.append(rows.pop(0))
grid_data = self.grid.get_values()
for row in rows:
if new_row(row) in failed_moves:
failed_moves.append(failed_row(row))
continue
self.grid.InsertRows(new_row(row))
file_path, first_pg, last_pg, rotation = grid_data[row]
self.grid.create_row(
new_row(row), file_path, int(self.grid.GetCellValue(old_row(row), 4)),
first_pg + 1, last_pg + 1, rotation
)
self.grid.DeleteRows(old_row(row))
self.grid.SelectRow(select_row(row), True)
def move_up(self, event):
"""Moves all selected grid rows up, if possible."""
self._move(False)
event.Skip()
def move_down(self, event):
"""Moves all selected grid rows down, if possible."""
self._move(True)
event.Skip()
def on_saveas(self, event):
"""Selects the output file name and directory."""
with wx.FileDialog(
self, 'Save As...',
wildcard='PDF Files (*.pdf)|*.pdf',
style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT
) as dialog:
if self.output_file.GetValue():
path = Path(self.output_file.GetValue())
dialog.SetFilename(path.name)
dialog.SetDirectory(str(path.parent))
if dialog.ShowModal() == wx.ID_OK:
self.output_file.SetValue(str(Path(dialog.GetPath())))
def on_save(self, event):
"""
Merges the selected pdfs and saves.
The frame is closed if the merged pdf is saved successfully.
"""
event.Skip()
error_msg = ''
output_path = self.output_file.GetValue()
grid_data = self.grid.get_values()
if not output_path:
error_msg = 'Need to select the output file name.'
elif not grid_data:
error_msg = 'No PDF files to merge.'
if error_msg:
with wx.MessageDialog(self, error_msg, 'Error') as dlg:
dlg.ShowModal()
return
try:
output_pdf = merge_pdfs(grid_data, True)
if len(output_pdf) == 0:
raise ValueError('The output pdf has no pages.')
# note: garbate > 2 will merge the same objects, which can cause issues viewing
# the pdf with Adobe (although the pdf can still be viewed with other software)
if SAFE_SAVE:
save_options = {}
else:
save_options = {'garbage': 4, 'deflate': 1}
output_pdf.save(output_path, **save_options)
except Exception:
with wx.MessageDialog(
self, f'Could not save file\n\n {traceback.format_exc()}',
'Error Saving'
) as dlg:
dlg.ShowModal()
else:
with wx.MessageDialog(
self, f'File successfully created\n\nFile at:\n {output_path}\n',
'Save Successful'
) as dlg:
dlg.ShowModal()
self.output_file.SetValue('')
finally:
try:
output_pdf.close()
except Exception:
pass
def on_preview(self, event):
"""Launches the pdf viewer to show a preview."""
grid_values = self.grid.get_values()
if self.preview or not grid_values:
return
try:
temp_pdf = merge_pdfs(grid_values)
except Exception:
with wx.MessageDialog(
self, f'Could not make preview\n\n {traceback.format_exc()}',
'Error with preview'
) as dlg:
dlg.ShowModal()
temp_pdf = None
return
self.preview = PDFViewer(self, temp_pdf, title='PDF Preview')
self.preview.Show()
try:
temp_pdf.close()
except ValueError:
pass
def merge_pdfs(grid_data, finalize=False):
"""
Merges all of the input PDF files into a single PDF and saves.
Parameters
----------
grid_data : list(list(str, int, int, int))
A list of lists. Each internal list should have four items, telling
the file path for each pdf to merge, the first page to use, the last
page to use, and the rotation. Each individual entry is as follows:
file_path: str
The collection of files to merge into a single document and saved.
first_pg: int
The first page to use. 0-based.
last_pg: int
The last page to use. 0-based. If less than the first_pg, then the
page order will be reversed.
rotations : {0, 90, -90, 180}
The integer rotation to apply to the document. Note that -90 will
rotate the document left (counter-clockwise).
finalize : bool, optional
If False (default), will ignore links when merging the pdfs and will not
create a table of contents. If True, will keep links and create the full
merged table of contents.
"""
current_pg = 0
output_file = fitz.Document()
total_toc = [] # collects the bookmarks from all of the files
for file_path, first_pg, last_pg, rotation in grid_data:
path = Path(file_path)
with get_pdf(path, finalize) as temp_file:
pages = len(temp_file)
# ensures pages are within the document
first_pg = min(max(0, first_pg), pages - 1)
if last_pg != -1:
last_pg = min(max(0, last_pg), pages - 1)
else:
last_pg = pages - 1
# only add unencrypted files
if not temp_file.needs_pass:
output_file.insert_pdf(
temp_file, from_page=first_pg, to_page=last_pg,
rotate=rotation, links=finalize, annots=True
)
else:
print(
f'\nThe following file is encrypted and cannot be processed:\n\n {str(path)}'
)
continue
if not finalize:
continue # skip creating the table of contents
# get file's table of contents
toc = temp_file.get_toc(simple=False)
if first_pg > last_pg:
increment = -1
toc = toc[::-1]
else:
increment = 1
pg_range = list(range(first_pg, last_pg + increment, increment))
# set starting bookmark level to 1
last_lvl = 1
for link in toc:
lnk_type = link[3]["kind"]
if lnk_type == fitz.LINK_NAMED:
# skip named links since pymupdf cannot process them
continue
elif lnk_type == fitz.LINK_GOTO:
if link[2] - 1 not in pg_range:
# skip the bookmark if it's not within the page range
continue
else:
page_num = pg_range.index(link[2] - 1) + current_pg + 1
# fix bookmark levels left by filler bookmarks
while (link[0] > last_lvl + 1):
total_toc.append([last_lvl + 1, "<>", page_num, link[3]])
last_lvl += 1
last_lvl = link[0]
link[2] = page_num
total_toc.append(link)
current_pg += len(pg_range)
if total_toc:
output_file.set_toc(total_toc)
return output_file
def get_page_layout():
"""Returns the selected page layout."""
page_layout = PAGE_LAYOUT
if USE_LANDSCAPE and not page_layout.endswith('-l'):
page_layout += '-l'
return page_layout
def get_pdf(file_name, finalize=False):
"""
Generates a pymupdf Document based on the file extension of the file.
Parameters
----------
file_name : str or os.Pathlike
The file path for the document.
finalize : bool, optional
If False (default), will not copy table of contents for xps, html, or
epub files. If True, will copy the table of contents.
Returns
-------
fitz.Document
The file converted to a pymupdf Document using the appropriate conversions.
"""
path = Path(file_name)
suffix = path.suffix.lower()
if suffix.startswith('.'):
suffix = suffix[1:]
if suffix == 'pdf':
return fitz.Document(file_name)
elif re.search('.*xps|epub|htm.*', suffix) is not None:
return document_to_pdf(file_name, finalize)
elif is_image(suffix):
return image_to_pdf(file_name)
else:
return text_to_pdf(file_name)
def document_to_pdf(file_name, finalize=False):
"""
Converts epub, html, and xps files to pdf while retaining links and table of contents.
Parameters
----------
file_name : str or os.Pathlike
The file path for the document.
finalize : bool, optional
If False (default), will not copy table of contents for xps, html, or
epub files. If True, will copy the table of contents.
Returns
-------
pdf : fitz.Document
The file converted to a pymupdf Document using the appropriate conversions.
Notes
-----
htm* and epub files are sized according to PAGE_LAYOUT and FONT_SIZE since
their size can be variable.
"""
path = Path(file_name)
width, height = fitz.PaperSize(get_page_layout())
with fitz.Document(str(path), width=width, height=height, fontsize=FONT_SIZE) as original:
pdf = fitz.Document(filetype='pdf', stream=original.convert_to_pdf())
if finalize:
pdf.set_toc(original.get_toc())
return pdf
def text_to_pdf(file_name):
"""
Converts text files to pdf.
Parameters
----------
file_name : str or os.Pathlike
The file path for the text document.
Returns
-------
pdf : fitz.Document
The file converted to a pymupdf Document.
Notes
-----
Uses the default pymupdf settings to determine the maximum lines per page and
characters per line (although the max lines per page is slightly different...?).
Works okay when using Helvetica and size 11 font, but is not guaranteed to work
for all cases. Best to just use Word to convert text to pdf, if available.
"""
width, height = fitz.PaperSize(get_page_layout())
# 108 is the header + footer spacing (72 is the botton of the first inserted
# row, and the footer is made to be 36). 1.4 designates that line spacing
# is 20% of the font size, above and below; should be just 1.2,
# but 1.4 works better.
page_lines = int((height - 108) / (1.4 * FONT_SIZE))
# estimate character width using 0 as the average character
# 100 is the left and right margins, 50 points each by default
max_chars = int((width - 100) / fitz.Font(FONT, FONT_PATH).text_length('0', FONT_SIZE))
line_count = 0
page_buffer = ''
pdf = fitz.Document()
with open(file_name) as text_file:
for item in text_file:
if len(item) > max_chars:
line_list = textwrap.wrap(item, width=max_chars, subsequent_indent='\n')
line_list[-1] += '\n'
else:
line_list = [item]
for line in line_list:
page_buffer += line
line_count += 1
if line_count >= page_lines:
pdf.insert_page(
-1, text=page_buffer, fontsize=FONT_SIZE,
width=width, height=height, fontname=FONT, fontfile=FONT_PATH
)
line_count = 0
page_buffer = ''
if page_buffer:
pdf.insert_page(
-1, text=page_buffer, fontsize=FONT_SIZE,
width=width, height=height, fontname=FONT, fontfile=FONT_PATH
)
return pdf
def image_to_pdf(file_name):
"""
Converts image files to pdf.
Expands the image to fit PAGE_LAYOUT, while retaining its aspect ratio.
Parameters