Django models to HTML. Then, HTML to PDF

Viewed 784

I am trying to export django models data (admin side data) into a PDF file. For that first I created a HTML file to render the data from models. The HTML file I created

It worked successfully and showed the data from the models correctly. Successfully worked (I create a url for it to check whether it is working or not)

Then I tried to render the same html file to PDF. I ran the server and I generated a pdf file. PDF file I expected it will show the data also. But It only showed the table border.

You can see my folders and names in the 1st photo. I thing it is enough to add this code. If you need full code please tell me. This is my views.py from app.

def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html  = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
    return HttpResponse(result.getvalue(), content_type='application/pdf')
return None


class ViewPDF(View):
    def get(self, request, *args, **kwargs):

        pdf = render_to_pdf('app/pdf_template.html')
        return HttpResponse(pdf, content_type='application/pdf')

Can't I use the same html file to get the data as pdf? Can anyone tell me what wrong I did?

pdf_template.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>This is my first pdf</title>
</head>
<body>
 <center>
    <h2>User Table</h2>
    <table border="1">
        <tr>
            <th>Username</th>
            <th>E-mail</th>
            <th>Country</th>
            <th>City</th>
        </tr>
        {% for result in user %}
        <tr>
            <td>
                {{result.username}}
            </td>
            <td>
                {{result.email}}
            </td>
            <td>
                {{result.country}}
            </td>
            <td>
                {{result.city}}
            </td>
        </tr>
        {% endfor %}
    </table>
</center>
</body>
</html>
2 Answers

I got the correct out put. I am posting this as an answer so that it will use for someone else too.

class ViewPDF(View):
def get(self, request, *args, **kwargs):
    context={}
    context['user'] =user.objects.all()
    pdf = render_to_pdf('app/pdf_template.html',context_dict=context)
    return HttpResponse(pdf, content_type='application/pdf')

In the views.py I modified the code like above.

The PDF output

i see you used 'user' as a list for your 'for' condition but you never add it to your context. i think it's working fine but there is no data to show

update: in the 'render_to_pdf' you get 'context_dict' argument to render your template by that. but you never pass this argument when you call your function. that's why you can' see anything except borders. because there is no data

update 2: in this line :

pdf = render_to_pdf('app/pdf_template.html')

just add context dict. something like this:

pdf = render_to_pdf('app/pdf_template.html',context)
Related