'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()) startfile(fname) ##with open((fname[:-4] if fname[-4:]=='.pdf' else fname) + '.ps', 'w', ## encoding='utf-8') as f: ## f.write(ps)