-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlayered-pdf-merge.py
87 lines (64 loc) · 2.18 KB
/
layered-pdf-merge.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
"""
Layered PDF Merge for LearnMYOG
Combines multi-page A0 pdfs into a single file with Optional Content Groups
Useful for sewing patterns with multiple size options
PDF view such as Adobe Acrobat Reader required to view layers
Requires:
python 3.7
pymupdf - https://pymupdf.readthedocs.io/en/latest/intro.html
mupdf "brew install mupdf"
pip install wheel setuptools
pip install pymupdf
Workings:
1. Save a Copy of A0 layers from Inkscape:
Common elements and all text and pattern symbols to common.pdf
Each size garment pattern (by color) to individual file e.g. small.pdf
2. Copy pdf layers to path variable location e.g. patterns/
3. Run script. Output file has layers across multiple pages.
"""
import os, time, pprint
import fitz
# location for src pdfs
path = "patterns/"
paths = sorted(os.listdir(path))
# create empty output pdf
doc = fitz.open()
for i, obj in enumerate(paths):
if obj.endswith('.pdf'):
src = fitz.open(path+obj)
# add empty pages to doc to match page count of source pages
if len(doc) < len(src):
for pg in range(len(src)):
# default page size is A4 @ 72 dpi 595 x 842
# A0 @ 72 dpi 2384 x 3370
page = doc.new_page(-1, 3370, 2384)
#determine rect size for placing layers later
r0 = page.rect
# create optional content group for pattern size
name = os.path.splitext(obj)[0]
xref0 = doc.add_ocg(name, on=True)
# iterate over each page
for pg in range(len(src)):
doc.load_page(pg).show_pdf_page(r0, src, pg, oc = xref0)
src.close()
print('------------------------')
print('Option Content Groups')
print('------------------------')
pprint.pprint(doc.get_ocgs())
timestr = str('D:' + time.strftime("%Y%m%d%H%M%S"))
doc.set_metadata({})
doc.set_metadata({
'author': 'Tim @ LearnMYOG.com',
'creationDate': timestr
})
print('------------------------')
print('metadata')
print('------------------------')
pprint.pprint(doc.metadata)
doc.save('new_pattern.pdf',
garbage=3,
pretty=True,
deflate=True,
linear=True,
clean=True,)
doc.close()