def view_song_pdf(request, song_id): """Views a specific song PDF representation.""" from pdf import pdf_set_locale, SongTemplate import locale o = Song.objects.get(id=song_id) # sets the locale so the dates and such get correctly printed old_locale = pdf_set_locale(request) response = HttpResponse(mimetype='application/pdf') response['Content-Disposition'] = 'attachment; filename="%s.pdf"' % \ o.title.encode('ascii', 'ignore') doc = SongTemplate(response) doc.author = o.user.get_full_name() + u'<' + o.user.email + u'>' doc.title = o.title doc.subject = ugettext(u'Lyrics and Chords') story = o.pdf_story(doc) o.pdf_add_page_template(doc) doc.build(story) # restore default language locale.setlocale(locale.LC_ALL, old_locale) return response
def view_collection_songbook_pdf(request, collection_id): """Returns a PDF book of all songs available in the website, in the requested order. """ from reportlab.platypus.tableofcontents import TableOfContents from reportlab.platypus import NextPageTemplate, PageBreak from pdf import SongBookTemplate, style, pdf_set_locale import locale collection = Collection.objects.get(id=collection_id) objects = collection.song.all() # A get request determines the order. The default is -updated try: order = request.GET.get('o', '-updated').strip() objects = objects.order_by(order) except FieldError: raise Http404 # sets the locale so the dates and such get correctly printed old_locale = pdf_set_locale(request) response = HttpResponse(mimetype='application/pdf') response['Content-Disposition'] = 'attachment; filename="%s.pdf"' % \ collection.name.encode('ascii', 'ignore') doc = SongBookTemplate(response) doc.author = collection.owner.get_full_name() + u'<' + \ collection.owner.email + u'>' doc.title = collection.name doc.subject = ugettext(u'Lyrics and Chords Book') story = collection.pdf_cover_page(request) #appends and prepares table of contents story.append(NextPageTemplate('TOC')) story.append(PageBreak()) story.append(TableOfContents()) story[-1].levelStyles[0] = style['toc-entry'] story[-1].dotsMinLevel = 0 #connecting dots #adds the lyrics objects = list(objects) for o in objects: o.pdf_add_page_template(doc) story.append(NextPageTemplate(o.pdf_template_id())) story.append(PageBreak()) story += o.pdf_story(doc) #multi-pass builds are necessary to handle TOCs correctly doc.multiBuild(story) # restore default language locale.setlocale(locale.LC_ALL, old_locale) return response