'459 649': '459.212598 649.133858', '499 709': '498.897638 708.661417', '516 729': '515.905512 728.503937', '595 842': '595.275591 841.889764', '649 918': '649.133858 918.425197', '709 1001': '708.661417 1000.629921', '709 499': '708.661417 498.897638', '729 1032': '728.503937 1031.811024', '74 105': '73.700787 104.88189', '842 1191': '841.889764 1190.551181', '842 595': '841.889764 595.275591', '918 1298': '918.425197 1298.267717'} ps = TextIOWrapper(stdin.buffer, encoding='utf-8').read() psHead, _, psTail = ps.partition(stEnd) pPS = re.compile(r'/PageSize \[(.+?)\]') m = pPS.search(psHead) if m: g = m.group(1) if g in dPS: psHead = pPS.sub('/PageSize [' + dPS[g] + ']', psHead, 1) ps = psHead + (nstEnd if _ else docinfo) + psTail proc = Popen('gs.cmd -sDEVICE=pdfwrite -dSAFER -o "' + fname + '" -c .setpdfwrite -f -', stdin=PIPE) proc.communicate(input=ps.encode())