I'm using Platypus (part of reportlab) to generate pdfs without
micromanagement of Canvas elements. reportlab's documentation was very
helpful to learn how to work with Paragraph, Table, Image, etc. Some code:

# views.py
@login_required
def view_pdf(request, experiment_id):
    experiment =
get_object_or_404(Experiment.objects.filter(pk=experiment_id))
    response = HttpResponse(mimetype='application/pdf')
    filename = slugify(experiment.title) + '.pdf'
    response['Content-Disposition'] = 'filename=' + filename
    generate_pdf([experiment], response)
    return response

# functions.py
def generate_pdf(experiments, output):
    doc = SimpleDocTemplate(output, pagesize=A4)
    Document = []
    for experiment in experiments:
        Document.extend(pdf_elements(experiment))
    doc.build(Document)

pdf_elements is a function that adds bits and pieces... This is just an
excerpt:

def pdf_elements(experiment):
    styles = getSampleStyleSheet()
    Document = [Paragraph(escape(experiment.title), styles["Title"])]
    Document.append(Spacer(1,50))
    date = unicode(experiment.date.strftime("%B %d, %Y"))
    Document.append(Paragraph("Acquired on " + date, styles["Normal"]))
    date_now = unicode(datetime.now().strftime("%B %d, %Y"))
    Document.append(Paragraph("PDF created on " + date_now,
styles["Normal"]))
    Document.append(Spacer(1,10))
    return Document

Read the Platypus documentation for many more elements and options.

These are my reportlab imports:

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer,
TableStyle
from reportlab.platypus import KeepTogether
from reportlab.platypus import Table as Platypus_Table
from reportlab.platypus import Image as Platypus_Image
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet

- Paulo


On Tue, Mar 30, 2010 at 5:08 PM, Sven Richter <sver...@googlemail.com>wrote:

> Hi everybody,
>
> i am looking for a way to get BaseDocTemplate from the reportlab
> library working, connected with a httpresponse.
> I found the documentation for a simple reportlab page, using canvas.
> But to me it seems, that i need more options than basic Canvas offers.
> Like a page header or footer, tables continuing over pages etc.
> I have not found a working example to use httpresponse:
>
> response = HttpResponse(mimetype='application/pdf')
> response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'
>
> in conjunction with:
>
> BaseDocTemplate('basedoc.pdf',showBoundary=1)
>
> So if somebody could provide some working code, or a hint to
> repository or an application with some working code, i'd really
> appreciate that.
>
>
> Greetings
> Sven
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to