|
30 | 30 | import os
|
31 | 31 | import re
|
32 | 32 | import sys
|
| 33 | +import hashlib |
33 | 34 | import subprocess
|
34 | 35 | import qrencode
|
35 | 36 | from tempfile import mkstemp
|
|
46 | 47 | text_x_offset = 0
|
47 | 48 | text_y_offset = 8.2
|
48 | 49 |
|
| 50 | +plaintext_maxlinechars = 73 |
| 51 | + |
49 | 52 | # the paperformat to use, activate the one you want
|
50 | 53 | paperformat_obj = document.paperformat.A4
|
51 | 54 | paperformat_str = "A4"
|
@@ -140,18 +143,58 @@ def finish_page(pdf, canv, pageno):
|
140 | 143 | fd, temp_text_path = mkstemp('.ps', 'text_', '.')
|
141 | 144 | input_file_modification = datetime.fromtimestamp(os.path.getmtime(input_path)).strftime("%Y-%m-%d %H:%M:%S")
|
142 | 145 |
|
| 146 | +# split lines on plaintext_maxlinechars - ( checksum_size + separator size) |
| 147 | +splitat=plaintext_maxlinechars - 8 |
| 148 | +splitlines=[] |
| 149 | +for line in ascdata.splitlines(): |
| 150 | + while len(line) > splitat: |
| 151 | + splitlines.append(line[:splitat]) |
| 152 | + # add a ^ at the beginning of the broken line to mark the linebreak |
| 153 | + line="^"+line[splitat:] |
| 154 | + splitlines.append(line) |
| 155 | + |
| 156 | +# add checksums to each line |
| 157 | +chksumlines=[] |
| 158 | +for line in splitlines: |
| 159 | + # remove the linebreak marks for checksumming |
| 160 | + if len(line) > 1 and line[0] == "^": |
| 161 | + sumon=line[1:] |
| 162 | + else: |
| 163 | + sumon=line |
| 164 | + |
| 165 | + # use the first 6 bytes of MD5 as checksum |
| 166 | + chksum = hashlib.md5(sumon.encode('utf-8')).hexdigest()[:6] |
| 167 | + |
| 168 | + # add the checksum right-justified to the line |
| 169 | + line+=" "*(splitat-len(line)) |
| 170 | + line+=" |"+chksum |
| 171 | + |
| 172 | + chksumlines.append(line) |
| 173 | + |
| 174 | +# add some documentation around the plaintest |
| 175 | +outlines=[] |
| 176 | +coldoc=" "*splitat |
| 177 | +coldoc+=" | MD5" |
| 178 | +outlines.append(coldoc) |
| 179 | +outlines.extend(chksumlines) |
| 180 | +outlines.append("") |
| 181 | +outlines.append("") |
| 182 | +outlines.append("") |
| 183 | +outlines.append("--") |
| 184 | +outlines.append("Created with paperbackup.py") |
| 185 | +outlines.append("See https://github.com/intra2net/paperbackup/ for instructions") |
| 186 | + |
143 | 187 | # use "enscript" to create postscript with the plaintext
|
144 | 188 | p = subprocess.Popen(
|
145 | 189 | ["enscript", "-p"+temp_text_path, "-f", "Courier12",
|
146 | 190 | "-M" + paperformat_str, "--header",
|
147 | 191 | just_filename + "|" + input_file_modification + "|Page $%"],
|
148 | 192 | stdout=subprocess.PIPE, stdin=subprocess.PIPE)
|
149 | 193 |
|
150 |
| -# send the file and a tag line to enscript |
151 |
| -p.stdin.write(ascdata.encode('utf-8')) |
152 |
| -p.stdin.write("\n\n\n--\n".encode('utf-8')) |
153 |
| -p.stdin.write("Created with paperbackup.py\n".encode('utf-8')) |
154 |
| -p.stdin.write("See https://github.com/intra2net/paperbackup/ for instructions\n".encode('utf-8')) |
| 194 | +# send the text to enscript |
| 195 | +for line in outlines: |
| 196 | + p.stdin.write(line.encode('utf-8')) |
| 197 | + p.stdin.write(os.linesep.encode('utf-8')) |
155 | 198 |
|
156 | 199 | p.communicate()[0]
|
157 | 200 | p.stdin.close()
|
|
0 commit comments