@@ -18,36 +18,36 @@ def EnclosedString(d, starts, ends):
18
18
return d [off :d .find (ends , off )]
19
19
20
20
def getCount (d ):
21
- s = EnclosedString (d , "/Count " , "/" )
21
+ s = EnclosedString (d , b "/Count " , b "/" )
22
22
count = int (s )
23
23
return count
24
24
25
25
def procreate (l ): # :p
26
- return " 0 R " .join (l ) + " 0 R"
26
+ return b " 0 R " .join (l ) + b " 0 R"
27
27
28
28
def adjustPDF (contents ):
29
29
"""dumb [start]xref fix: fixes old-school xref with no holes, with hardcoded \\ n"""
30
- startXREF = contents .find ("\n xref\n 0 " ) + 1
31
- endXREF = contents .find (" \n \n " , startXREF ) + 1
30
+ startXREF = contents .find (b "\n xref\n 0 " ) + 1
31
+ endXREF = contents .find (b " \n \n " , startXREF ) + 1
32
32
origXref = contents [startXREF :endXREF ]
33
- objCount = int (origXref .splitlines ()[1 ].split (" " )[1 ])
33
+ objCount = int (origXref .splitlines ()[1 ].split (b " " )[1 ])
34
34
print ("object count: %i" % objCount )
35
35
36
36
xrefLines = [
37
- "xref" ,
38
- "0 %i" % objCount ,
37
+ b "xref" ,
38
+ b "0 %i" % objCount ,
39
39
# mutool declare its first xref like this
40
- "0000000000 00001 f "
40
+ b "0000000000 00001 f "
41
41
]
42
42
43
43
i = 1
44
44
while i < objCount :
45
45
# doesn't support comments at the end of object declarations
46
- off = contents .find ("\n %i 0 obj\n " % i ) + 1
47
- xrefLines .append ("%010i 00000 n " % (off ))
46
+ off = contents .find (b "\n %i 0 obj\n " % i ) + 1
47
+ xrefLines .append (b "%010i 00000 n " % (off ))
48
48
i += 1
49
49
50
- xref = "\n " .join (xrefLines )
50
+ xref = b "\n " .join (xrefLines )
51
51
52
52
# XREF length should be unchanged
53
53
try :
@@ -58,8 +58,8 @@ def adjustPDF(contents):
58
58
59
59
contents = contents [:startXREF ] + xref + contents [endXREF :]
60
60
61
- startStartXref = contents .find ("\n startxref\n " , endXREF ) + len ("\n startxref\n " )
62
- endStartXref = contents .find ("\n %%%%EOF" , startStartXref )
61
+ startStartXref = contents .find (b "\n startxref\n " , endXREF ) + len (b "\n startxref\n " )
62
+ endStartXref = contents .find (b "\n %%%%EOF" , startStartXref )
63
63
contents = contents [:startStartXref ] + "%i" % startXREF + contents [endStartXref :]
64
64
65
65
return contents
@@ -88,12 +88,12 @@ def adjustPDF(contents):
88
88
COUNT2 = getCount (d2 )
89
89
90
90
91
- kids = EnclosedString (dm , "/Kids[" , "]" )
91
+ kids = EnclosedString (dm , b "/Kids[" , b "]" )
92
92
93
93
# we skip the first dummy, and the last " 0 R" string
94
- pages = kids [:- 4 ].split (" 0 R " )[1 :]
94
+ pages = kids [:- 4 ].split (b " 0 R " )[1 :]
95
95
96
- template = """%%PDF-1.4
96
+ template = b """%%PDF-1.4
97
97
98
98
1 0 obj
99
99
<<
@@ -132,7 +132,7 @@ def adjustPDF(contents):
132
132
contents = template % locals ()
133
133
134
134
# adjust parents for the first set of pages
135
- contents += dm [dm .find ("5 0 obj" ):].replace ("/Parent 2 0 R" , "/Parent 3 0 R" , COUNT1 )
135
+ contents += dm [dm .find (b "5 0 obj" ):].replace (b "/Parent 2 0 R" , b "/Parent 3 0 R" , COUNT1 )
136
136
137
137
# not necessary (will be fixed by mutool anyway) but avoids warnings
138
138
contents = adjustPDF (contents )
@@ -148,8 +148,8 @@ def adjustPDF(contents):
148
148
149
149
# some mutool versions do different stuff :(
150
150
cleaned = cleaned .replace (
151
- " 65536 f \n 0000000016 00000 n \n " ,
152
- " 65536 f \n 0000000018 00000 n \n " ,
151
+ b " 65536 f \n 0000000016 00000 n \n " ,
152
+ b " 65536 f \n 0000000018 00000 n \n " ,
153
153
1 )
154
154
155
155
with open ("pdf1.bin" , "rb" ) as f :
@@ -158,8 +158,8 @@ def adjustPDF(contents):
158
158
with open ("pdf2.bin" , "rb" ) as f :
159
159
prefix2 = f .read ()
160
160
161
- file1 = prefix1 + "\n " + cleaned [192 :]
162
- file2 = prefix2 + "\n " + cleaned [192 :]
161
+ file1 = prefix1 + b "\n " + cleaned [192 :]
162
+ file2 = prefix2 + b "\n " + cleaned [192 :]
163
163
164
164
with open ("collision1.pdf" , "wb" ) as f :
165
165
f .write (file1 )
0 commit comments