def convert(pdf_filename): wd = '/tmp' number_of_pages = "identify -ping -format %n\n \"{input_file}\"".format( input_file=pdf_filename) number_of_pages = cmd_line_to_args(number_of_pages) number = int( subprocess.check_output(number_of_pages, cwd=wd).decode('ascii').split('\n')[0]) for i in range(number): cmds = [ "convert -density 300 -crop 158x132+029+142 -quality 00 \"{input_file}[{i}]\" A{i}.png", "convert -density 300 -crop 217x117+190+170 -quality 00 \"{input_file}[{i}]\" B{i}.png", "convert -density 400 -crop 150x110+420+41 -quality 00 \"{input_file}[{i}]\" C{i}.png", "convert +append +repage A{i}.png B{i}.png C{i}.png ABC{i}.png", "convert ABC{i}.png -background white -gravity center -extent 566x165 out{i}.png", ] for cmd in cmds: cmd = cmd.format(input_file=pdf_filename, i=i) cmd = cmd_line_to_args(cmd) try: subprocess.check_call(cmd, cwd=wd) except subprocess.CalledProcessError as e: raise NameError('Could not convert the PDF file: ' + str(e)) in_files = ' '.join('out{0}.png'.format(i) for i in range(number)) cmd = "convert {in_files} out.pdf".format(in_files=in_files) cmd = cmd_line_to_args(cmd) subprocess.check_call(cmd, cwd=wd) return os.path.join(wd, 'out.pdf')
def convert(pdf_filename): wd = '/tmp' number_of_pages = "identify -ping -format %n\n \"{input_file}\"".format(input_file=pdf_filename) number_of_pages = cmd_line_to_args(number_of_pages) number = int(subprocess.check_output(number_of_pages, cwd=wd).decode('ascii').split('\n')[0]) for i in range(number): cmds = [ "convert -density 300 -crop 158x132+029+142 -quality 00 \"{input_file}[{i}]\" A{i}.png", "convert -density 300 -crop 217x117+190+170 -quality 00 \"{input_file}[{i}]\" B{i}.png", "convert -density 400 -crop 150x110+420+41 -quality 00 \"{input_file}[{i}]\" C{i}.png", "convert +append +repage A{i}.png B{i}.png C{i}.png ABC{i}.png", "convert ABC{i}.png -background white -gravity center -extent 566x165 out{i}.png", ] for cmd in cmds: cmd = cmd.format(input_file=pdf_filename, i=i) cmd = cmd_line_to_args(cmd) try: subprocess.check_call(cmd, cwd=wd) except subprocess.CalledProcessError as e: raise NameError('Could not convert the PDF file: ' + str(e)) in_files = ' '.join('out{0}.png'.format(i) for i in range(number)) cmd = "convert {in_files} out.pdf".format(in_files=in_files) cmd = cmd_line_to_args(cmd) subprocess.check_call(cmd, cwd=wd) return os.path.join(wd, 'out.pdf')
def convert(filename): wd = '/tmp' cmds = [] input_file = filename if filename.endswith('.pdf'): cmds += ['pdfimages -all "{pdf_file}" /tmp/pdfimages'] input_file = '/tmp/pdfimages-000.png' elif filename.endswith('.gif') or filename.endswith('.png'): pass else: raise NotImplementedError() cmds += [ 'convert -rotate 90 -crop 600x300+100+0 "{input_file}" a.png', 'convert -rotate 90 -crop 600x350+280+358 "{input_file}" b.png', 'convert -rotate 90 -crop 600x350+0+722 "{input_file}" c.png', 'convert -rotate 90 -crop 600x346+25+1124 "{input_file}" d.png', 'convert -rotate 90 -crop 850x350+175+1450 "{input_file}" e.png', 'convert -append -gravity Center -background white +repage a.png b.png ab.png', 'convert -append -gravity Center -background white +repage c.png d.png cd.png', 'convert +append -gravity Center -background white +repage ab.png cd.png e.png output.png', 'convert -density 300 -set units PixelsPerInch output.png output.pdf', ] for cmd in cmds: cmd = cmd.format(input_file=input_file, pdf_file=filename) cmd = cmd_line_to_args(cmd) try: logger.debug('running the following command: ' + ' '.join(cmd)) subprocess.check_call(cmd, cwd=wd) except subprocess.CalledProcessError as e: raise NameError('Could not convert the PDF file: ' + str(e)) return os.path.join(wd, 'output.pdf')
def convert(filename): wd = '/tmp' cmds = [] input_file = filename if filename.endswith('.pdf'): cmds += ['pdfimages -all "{pdf_file}" /tmp/pdfimages'] input_file = '/tmp/pdfimages-000.png' elif filename.endswith('.gif') or filename.endswith('.png'): pass else: raise NotImplementedError() cmds += [ 'convert -crop 588x150+1148+56 "{input_file}" a.png', 'convert -crop 609x696+1000+200 "{input_file}" b.png', 'convert -crop 676x480+110+66 "{input_file}" c.png', 'convert -crop 198x210+290+648 "{input_file}" d.png', 'convert -crop 676x20+530+906 -rotate 90 "{input_file}" e.png', 'convert -crop 800x220+530+930 "{input_file}" f.png', 'convert +append -gravity Center -background white +repage d.png a.png da.png', 'convert -append -gravity Center -background white +repage da.png c.png dac.png', 'convert +append -gravity Center -background white +repage dac.png b.png e.png f.png output.png', 'convert -density 300 -set units PixelsPerInch output.png output.pdf', ] for cmd in cmds: cmd = cmd.format(input_file=input_file, pdf_file=filename) cmd = cmd_line_to_args(cmd) try: logger.debug('running the following command: ' + ' '.join(cmd)) subprocess.check_call(cmd, cwd=wd) except subprocess.CalledProcessError as e: raise NameError('Could not convert the PDF file: ' + str(e)) return os.path.join(wd, 'output.pdf')
def render(pdf_filename, wd='/tmp', density = 72): ret = {'success': False, 'pages': [], 'pdf': pdf_filename} # get info on pages cmd = 'gm identify -verbose -density {density} "{pdf}"' cmd = cmd.format(pdf=pdf_filename, density=density) args = cmd_line_to_args(cmd) logger.debug(cmd) logger.debug(str(args)) try: info_text = subprocess.check_output(args, cwd=wd).decode('utf-8') except subprocess.CalledProcessError as e: logger.error("Running the command '{}' failed with the return code {}.".format(cmd, e.returncode)) return ret size_matcher = re.compile(r'\s*Geometry:\s+(?P<size>(\d+(\.\d*)?)x(\d+(\.\d*)?))') page_sizes = [match[0].split('x') for match in size_matcher.findall(info_text)] page_sizes = [(float(size[0]), float(size[1])) for size in page_sizes] # parse floats page_sizes = [(size[0] / density * 25.4, size[1] / density * 25.4) for size in page_sizes] # dots to mm num_pages = len(page_sizes) # render pages cmd = """ gm convert \ -verbose \ -density {density} \ -quality 00 \ -sharpen 0x1.0 \ +adjoin \ "{pdf}" \ "{outfile_tpl}" """.replace('\n', ' ') cmd = cmd.format(pdf=pdf_filename, density=density, outfile_tpl='single%03d.png') args = cmd_line_to_args(cmd) logger.debug(cmd) logger.debug(str(args)) try: subprocess.check_call(args, cwd=wd) except subprocess.CalledProcessError as e: logger.error("Running the command '{}' failed with the return code {}.".format(' '.join(cmd), e.returncode)) return ret for i in range(num_pages): img_file = os.path.join(wd, 'single{:03d}.png'.format(i)) ret['pages'].append({'size': page_sizes[i], 'img_file': img_file}) ret['success'] = True return ret
def convert(pdf_filename): wd = '/tmp' resolution = 300 cmds = [ 'convert -rotate 90 -crop 1170x688+1658+160 -quality 00 -density 270 "{input_file}" A.png', 'convert -rotate 90 -crop 810x115+1700+850 -quality 00 -density 270 "{input_file}" B.png', 'convert -rotate 90 -crop 560x696+1815+1207 -quality 00 -density 250 "{input_file}" C.png', 'composite -geometry +49+575 B.png A.png AB.png', 'convert +append +repage AB.png C.png out.png', 'convert -density 300 out.png out.pdf', ] for cmd in cmds: cmd = cmd.format(input_file=pdf_filename) cmd = cmd_line_to_args(cmd) try: subprocess.check_call(cmd, cwd=wd) except subprocess.CalledProcessError as e: raise NameError('Could not convert the PDF file: ' + str(e)) return os.path.join(wd, 'out.pdf')
def convert(pdf_filename, variant='normal'): wd = '/tmp' resolution = 300 if variant == 'normal': cmds = [ 'convert -crop 1130x586+1750+168 -quality 00 -density 270 "{input_file}" A.png', 'convert -crop 1130x595+1750+751 -quality 00 -density 270 "{input_file}" B.png', 'convert -crop 1046x667+1620+1244 -quality 00 -density 250 "{input_file}" C.png', 'convert +append +repage A.png B.png C.png out.png', 'convert -density 300 out.png out.pdf', ] elif variant == 'old': cmds = [ 'convert -rotate 90 -crop 1170x688+1658+160 -quality 00 -density 270 "{input_file}" A.png', 'convert -rotate 90 -crop 810x115+1700+850 -quality 00 -density 270 "{input_file}" B.png', 'convert -rotate 90 -crop 560x696+1815+1207 -quality 00 -density 250 "{input_file}" C.png', 'composite -geometry +49+575 B.png A.png AB.png', 'convert +append +repage AB.png C.png out.png', 'convert -density 300 out.png out.pdf', ] elif variant == 'extended': cmds = [ 'convert -rotate 90 -crop 1270x634+1880+180 -quality 00 -density 300 "{input_file}" A.png', 'convert -rotate 90 -crop 1270x650+1880+811 -quality 00 -density 300 "{input_file}" B.png', 'convert -rotate 90 -crop 1240x696+1895+1550 -quality 00 -density 300 "{input_file}" C.png', 'convert +append +repage A.png B.png AB.png', 'convert +append +repage AB.png C.png out.png', 'convert -density 300 out.png out.pdf', ] else: raise ValueError('unknown variant: ' + str(variant)) for cmd in cmds: cmd = cmd.format(input_file=pdf_filename) cmd = cmd_line_to_args(cmd) try: subprocess.check_call(cmd, cwd=wd) except subprocess.CalledProcessError as e: raise NameError('Could not convert the PDF file: ' + str(e)) return os.path.join(wd, 'out.pdf')