def get_page_heirarchy(filename): pdf_data = sh.pdftk(filename, "dump_data") rows = [] record = {} for row in pdf_data.split('\n'): if row.startswith('BookmarkTitle'): title = row.split(": ")[1] record['title'] = title.strip() if row.startswith('BookmarkLevel'): level = int(row.split(": ")[1]) record['level'] = level if row.startswith('BookmarkPageNumber'): pn = int(row.split(": ")[1]) record['page'] = pn record['children'] = [] rows.append(record) record = {} heir = {'title': "Root", "level": 0, 'pn': 0, "children": []} build_heirarchy(heir, rows) monsters = None after = None for rec in heir['children']: if monsters != None: after = rec break if rec['title'].startswith('Monsters'): monsters = rec fix_letters(monsters) fix_introduction(monsters) fix_duplicate_names(monsters) fix_apostrophe(monsters) fix_leaf_ray(monsters) fix_submonsters(monsters) fix_dragons(monsters) fix_assassin_vine(monsters) return monsters, after['page']
def generate_pdfs(persons, maßnahmenzeitraum, datum, kif_ev, maßname, leer_blaetter): seite = 1 data_file = "data_fdf.bin" output_file_name = "output" personen_pro_seite = 14 #check if vorlage exits test_file = Path(vorlage) if not test_file.is_file(): print("[X] File {} not found!".format(vorlage)) exit(-1) anzahl_seiten = ceil(len(persons) / personen_pro_seite) pdfs = [] for j in range(anzahl_seiten + leer_blaetter): felder = [] i = 0 for item in form_mapping: if personen_pro_seite * j + i < len(persons): felder.append((item[0], str(j * personen_pro_seite + i + 1))) felder.append( (item[1], persons[j * personen_pro_seite + i]["Name"])) felder.append( (item[2], persons[j * personen_pro_seite + i]["Hochschule"])) i += 1 if i == personen_pro_seite: output_file = output_file_name + str(seite) + ".pdf" headers = [("1", seite), ("2", maßnahmenzeitraum), ("3", datum), ("4", kif_ev), ("5", maßname)] fields = felder + headers fdf = forge_fdf("", fields, [], [], []) fdf_file = open(data_file, "wb") fdf_file.write(fdf) fdf_file.close() sh.pdftk(vorlage, "fill_form", data_file, "output", output_file, "flatten") pdfs.append(output_file) seite += 1 return pdfs
def exec_pdftk(filename): stdout = BytesIO() stderr = StringIO() sh.pdftk( filename, 'output', '-', 'dont_ask', 'flatten', _out=stdout, _err=stderr ) stderr = stderr.getvalue() if stderr.strip(): raise IOError(stderr) return stdout
def _exec_pdftk(pdf, items, outfile): stderr = StringIO() fdf = forge_fdf("", items, [], [], []) # pylint: disable=E1101 sh.pdftk(pdf, "fill_form", "-", "output", outfile, "dont_ask", "flatten", _in=fdf, _err=stderr) stderr = stderr.getvalue() if stderr.strip(): raise IOError(stderr)
def save_the_beasts(filename, directory, heir, last_page): if not os.path.exists(directory): os.mkdir(directory) for i in range(0, len(heir['children'])): row = heir['children'][i] next_page = last_page if len(heir['children']) > i+1: next_page = heir['children'][i+1]['page'] last_page = next_page - 1 if last_page < row['page']: last_page = row['page'] title = row['title'] pagedir = directory subdir = title[0] pagedir = "%s/%s" % (directory, subdir) if not os.path.exists(pagedir): os.mkdir(pagedir) newfile = "%s/%s.pdf" %(pagedir, row['title']) print "%s: %s-%s" %(newfile, row['page'], last_page) if os.path.exists(newfile): sys.stderr.write("!!!!%s Already Exists\n" % newfile) else: sh.pdftk(filename, "cat", "%s-%s" %( row['page'], last_page), "output", newfile)
latex_footer = """ \end{document} """ if len(sys.argv) < 3: print(usage) sys.exit(1) input_file = sys.argv[1] output_file = input_file[:-4] + '_titles' + '.pdf' # Get a list of all the titles # Make sure that there are at least as many titles as input pages titles = sys.argv[2:] pdfinfo = pdftk(input_file, 'dump_data') pdfinfol = pdfinfo.strip().split('\n') pdfpages = pdfinfol[-1].split(':') numpages = int(pdfpages[1].strip()) while numpages > len(titles): titles.append('~') # Combine the headers and create a .tex file with the headers latex_titles = ' \\newpage\n'.join(titles) latex_out = latex_header + latex_titles + latex_footer with open(TITLES_TEX, 'w') as f: f.write(latex_out)
latex_footer = """ \\end{document} """ if len(sys.argv) < 3: print(usage) sys.exit(1) input_file = sys.argv[1] output_file = input_file[:-4] + '_titles' + '.pdf' # Get a list of all the titles # Make sure that there are at least as many titles as input pages titles = sys.argv[2:] pdfinfo = pdftk(input_file, 'dump_data') pdfinfol = pdfinfo.strip().split('\n') for line in pdfinfol: items = line.split(':') if len(items) == 2 and items[0] == 'NumberOfPages': numpages = int(items[1].strip()) break while numpages > len(titles): titles.append('~') # Combine the headers and create a .tex file with the headers latex_titles = ' \\newpage\n'.join(titles) latex_out = latex_header + latex_titles + latex_footer
color: black; font-size: 16px; display: block; text-align: center; } em { color: black; font-size: 0.9em; } """ prog = u""" <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> </head> <body> <h1 id="maintitle">SciPy Latin America 2015<br>Schedule</h1> {prog} <style>{style}</style> </body> </html>""".format(style=style, prog=prog) with codecs.open(HTML_PATH, "w", encoding="utf8") as fp: fp.write(prog) sh.wkhtmltopdf(HTML_PATH, PDF_PATH) sh.pdftk(PDF_PATH, PUBS_PATH, "cat", "output", PROG_PATH)
# pdf is bigger than the test pdf test_size = os.stat('{}_layer_test.pdf'.format(sch_name)).st_size layer2_size = os.stat('{}_layer2_copper.pdf'.format(sch_name)).st_size layer3_size = os.stat('{}_layer3_copper.pdf'.format(sch_name)).st_size if layer2_size > test_size or layer3_size > test_size: layer_count = 4 # Combine them pdfs = [] titles = [] for pdf, title in pdf_files: if ('layer2' in pdf or 'layer3' in pdf) and layer_count == 2: continue pdfname = '{}_{}.pdf'.format(sch_name, pdf) if os.path.exists(pdfname): pdfinfo = pdftk(pdfname, 'dump_data') pdfinfol = pdfinfo.strip().split('\n') pdfpages = pdfinfol[-1].split(':') numpages = int(pdfpages[1].strip()) titles += [title]*numpages pdfs.append(pdfname) if include_bom: pdfs.append('{}_bom.pdf'.format(sch_name)) args = pdfs + ['cat', 'output', '{}.pdf'.format(sch_name)] pdftk(*args) # Delete the generated pdfs if they exist for pdf, title in pdf_files + [('layer_test', '~')]: try:
def _invoke_pdftk(*args, outname): assert outname sh.pdftk(*args, "cat", "output", outname)
output_file = output_file_name + str(seite) + ".pdf" headers = [("1", seite), ("2", maßnahmenzeitraum), ("3", datum), ("4", kif_ev), ("5", maßname)] fields = felder + headers fdf = forge_fdf("", fields, [], [], []) fdf_file = open(data_file, "wb") fdf_file.write(fdf) fdf_file.close() sh.pdftk(vorlage, "fill_form", data_file, "output", output_file, "flatten") pdfs.append(output_file) seite += 1 return pdfs if __name__ == "__main__": person_list = readcsv(csv_file_name) for datum in datum_list: final_pdf_name = "teilnehmendenliste_{}.pdf".format( datum.replace(". ", "_")) output_files = generate_pdfs(person_list, maßnahmenzeitraum, datum, kif_ev, maßname, leer_blaetter) sh.pdftk(output_files, "cat", "output", final_pdf_name)
def run_pdftk(file): buf = StringIO() sh.pdftk(file, 'dump_data_fields_utf8', _out=buf) return buf.getvalue()
# pdf is bigger than the test pdf test_size = os.stat('{}_layer_test.pdf'.format(sch_name)).st_size layer2_size = os.stat('{}_layer2_copper.pdf'.format(sch_name)).st_size layer3_size = os.stat('{}_layer3_copper.pdf'.format(sch_name)).st_size if layer2_size > test_size or layer3_size > test_size: layer_count = 4 # Combine them pdfs = [] titles = [] for pdf, title in pdf_files: if ('layer2' in pdf or 'layer3' in pdf) and layer_count == 2: continue pdfname = '{}_{}.pdf'.format(sch_name, pdf) if os.path.exists(pdfname): pdfinfo = pdftk(pdfname, 'dump_data') pdfinfol = pdfinfo.strip().split('\n') for line in pdfinfol: items = line.split(':') if len(items) == 2 and items[0] == 'NumberOfPages': numpages = int(items[1].strip()) break titles += [title]*numpages pdfs.append(pdfname) if include_bom: pdfs.append('{}_bom.pdf'.format(sch_name)) args = pdfs + ['cat', 'output', '{}.pdf'.format(sch_name)] pdftk(*args) # Delete the generated pdfs if they exist
text-align: center; } em { color: black; font-size: 0.9em; } """ prog = u""" <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> </head> <body> <h1 id="maintitle">SciPy Latin America 2015<br>Schedule</h1> {prog} <style>{style}</style> </body> </html>""".format(style=style, prog=prog) with codecs.open(HTML_PATH, "w", encoding="utf8") as fp: fp.write(prog) sh.wkhtmltopdf(HTML_PATH, PDF_PATH) sh.pdftk(PDF_PATH, PUBS_PATH, "cat", "output", PROG_PATH)