def make_pdf(new_data, new_list, css_file, css_file_contents, css_file_cover, version, up_dir, title, gen_date): # write_data = new_data.replace("assets/media", "README/assets/media") # doc = HTML(string=write_data, base_url=os.path.join(up_dir)).render(stylesheets=[css_file]) doc.make_bookmark_tree('convert') # data = make_bookmark_tree_b(doc, 'last') # output = os.path.join(up_dir, "README.pdf") # add_intro(doc, new_data, new_list, css_file, css_file_contents, css_file_cover, version, up_dir, title, gen_date) # doc.write_pdf(output)
def save(self): # PDF report is a list of all documents self.summary = '<h1>%s</h1>' % (self.locale['STR_SUMMARY']) # To make one PDF report, we have to get all pages of all documents... # First step , we obtain a list of sublists like this : # [ # [doc1.page1, doc1, page2], # [doc2.page1, doc2.page2], # [doc3.page1, doc3.page2, doc3.page3] # ] # Rendering content content = HTML(string=self.content, encoding="utf-8").render(stylesheets=[self.content_css]) #Priting summary table BEGIN self.summary += '<table style="border:0">' def _printSummary(bookmarks, indent=0, numer=''): for i, (label, (page, _, _), children) in enumerate(bookmarks, 1): tr_style = 'style="border-top:1px solid #CCC;border-bottom:1px solid #CCC"' title_td_style = 'style="border:0;text-align:left;width:550px;padding:10px;"' page_num_td_style = 'style="border:0;width:50px"' if indent == 0 and i == 1: tr_style = 'style="border-bottom:1px solid #CCC"' self.summary += ( '<tr %s><td %s>%s%s. %s</td><td %s>%d</td></tr>' % (tr_style, title_td_style, ' ' * indent, numer + str(i), label.lstrip('0123456789. '), page_num_td_style, page + 1)) _printSummary(children, indent + 2, numer + str(i) + '.') _printSummary(content.make_bookmark_tree()) #Priting summary table END self.summary += '</table>' homepage = HTML( string=self.homepage, encoding="utf-8").render(stylesheets=[self.homepage_css]) summary = HTML( string=self.summary, encoding="utf-8").render(stylesheets=[self.homepage_css]) pdf_report = [homepage, summary, content] all_pages = [doc.pages for doc in pdf_report] # Second step, clean sublist and make a simple list # http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python all_pages = [item for sublist in all_pages for item in sublist] # ...And combine these pages into a single report Document pdf_report[0].copy(all_pages).write_pdf(self.path) chmod(self.path, 0644) return self.path
def save(self): # PDF report is a list of all documents self.summary = '<h1>%s</h1>' % (self.locale['STR_SUMMARY']) # To make one PDF report, we have to get all pages of all documents... # First step , we obtain a list of sublists like this : # [ # [doc1.page1, doc1, page2], # [doc2.page1, doc2.page2], # [doc3.page1, doc3.page2, doc3.page3] # ] # Rendering content content = HTML(string=self.content, encoding="utf-8").render(stylesheets=[self.content_css]) #Priting summary table BEGIN self.summary += '<table style="border:0">' def _printSummary(bookmarks, indent=0, numer=''): for i, (label, (page, _, _), children) in enumerate(bookmarks, 1): tr_style = 'style="border-top:1px solid #CCC;border-bottom:1px solid #CCC"' title_td_style = 'style="border:0;text-align:left;width:550px;padding:10px;"' page_num_td_style = 'style="border:0;width:50px"' if indent == 0 and i == 1: tr_style = 'style="border-bottom:1px solid #CCC"' self.summary += ('<tr %s><td %s>%s%s. %s</td><td %s>%d</td></tr>' % ( tr_style, title_td_style, ' ' * indent, numer + str(i), label.lstrip('0123456789. '), page_num_td_style, page + 1)) _printSummary(children, indent + 2, numer + str(i) + '.') _printSummary(content.make_bookmark_tree()) #Priting summary table END self.summary += '</table>' homepage = HTML(string=self.homepage, encoding="utf-8").render(stylesheets=[self.homepage_css]) summary = HTML(string=self.summary, encoding="utf-8").render(stylesheets=[self.homepage_css]) pdf_report = [homepage, summary, content] logging.getLogger().warning(pdf_report[2].make_bookmark_tree()) all_pages = [doc.pages for doc in pdf_report] # Second step, clean sublist and make a simple list # http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python all_pages = [item for sublist in all_pages for item in sublist] # ...And combine these pages into a single report Document pdf_report[0].copy(all_pages).write_pdf(self.path) chmod(self.path, 0644) return self.path
def render_and_send_app(pk): app = import_module('venueapp.models').Application.objects.get(pk=pk) cover = import_module('venueapp.views').make_cover_page(app) max_bytes = config.get_int("max_inline_attachment_bytes", 0) for venue in app.venues.all(): html = render_to_string( "venueapp/pdf_app.html", { "object": app, "cover": cover, "venue": venue, "logo": finders.find("logo.png"), "pdf": True, "max_attachment_size": max_bytes, }) doc = HTML(string=html, base_url=settings.SITE_URL).render() bookmark_tree = doc.make_bookmark_tree() bookmarks = list([_Bookmark(i) for i in bookmark_tree]) app_pdf = BytesIO() doc.write_pdf(app_pdf) merger = PdfFileMerger() merger.append(app_pdf, import_bookmarks=False) for staff in app.staffmember_set.signed_on().filter( role__accepts_attachment=True).exclude( Q(attachment=None) | Q(attachment="")): name = "{} {}'s ".format(staff.role_name, staff.person) try: if staff.attachment.size < max_bytes: reader = PdfFileReader(staff.attachment.open(), False) attachment_pages = reader.getNumPages() page = None for i, bookmark in enumerate(bookmarks): if bookmark.label == name + "Supplement": page = bookmarks[i + 1].location if page: merger.merge(page, staff.attachment.open(), import_bookmarks=False) for i in bookmarks: if i.location >= page: i.location += attachment_pages else: merger.append(staff.attachment.open(), bookmark=name + "Attachment", import_bookmarks=False) except Exception as e: tb.print_exc() for i in bookmarks: merger.addBookmark(i.label, i.location) pdf = BytesIO() # open("/tmp/{}.pdf".format(venue.venue), "wb") merger.write(pdf) msg = render_msg("venueapp/email/submission.html", locals(), to=[ "{} <{}>".format(i.get_full_name(False), i.email) for i in venue.managers.all() ], cc=[ "{} <{}>".format(i.get_full_name(False), i.email) for i in app.show.staff.all() ], subject="Application for {} in {} Submitted".format( app, venue.venue), tags=["venueapp", "venueapp-submission"]) msg.attach("{} - {}.pdf".format(app, venue), BytesIO(pdf.getbuffer()), "application/pdf") try: msg.send() except Exception as err: LOGGER.error("Application submission sending failed: {}".format( repr(err))) tb.print_exc() finally: merger.close()