This is my views.py to convert html template to pdf
from datetime import datetime
from io import BytesIO
import os
from django.conf import settings
from django.http import HttpResponse
from frontend.views.task_shifts import valid_task_date
from frontend.views.utils import *
from django.contrib.auth import logout
from django.shortcuts import redirect, render
from django.contrib.auth import logout
from xhtml2pdf import pisa
from django.template.loader import get_template
from django.contrib.auth.decorators import login_required
def render_to_pdf(template_src, context_dict):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf_path = os.path.join(
settings.BASE_DIR, 'pdf_files', 'Rating_report.pdf')
pisa_status = pisa.CreatePDF(html, dest=result)
if pisa_status.err:
return HttpResponse('We had some error')
with open(pdf_path, 'wb') as f:
f.write(result.getvalue())
return pdf_path
@login_required(login_url='login_user')
def get_pdf(request):
if request.user.is_authenticated == False or request.session.get('station_code') is None or request.session['station_code'] is None:
logout(request)
return redirect("login_user")
if (request.method == 'POST'):
date1 = request.POST.get('date')
date = datetime.strptime(date1, '%Y-%m-%d').date()
context = {'date': date }
pdf_path = render_to_pdf('front/pdf/pdf.html', context)
pdf_file = os.path.basename(pdf_path)
with open(pdf_path, 'rb') as f:
response = HttpResponse(f.read(), content_type='application/pdf')
response['Content-Disposition'] = f'inline; filename="{pdf_file}"'
return response
else:
messages = []
messages.append('Enter date to see pdf')
return render(request, 'home.html', {'messages': messages})
and my html template is
{% load custom %} {% load static %} {% block body %}
<h1> pdf demo</h1>
<p>for : {{date}}</p>
{% endblock %}
and the form to submit the date to views.py is
<div class="modal fade" id="exampleModalForPdf" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Select Date</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="{% url 'getpdf' %}" method="POST">
{% csrf_token %}
<input type="date" name="date" value='$date' required >
<input type="submit" value="submit">
</form>
</div>
</div>
</div>
</div>
It is working fine on mobile devices but when I try to submit the form on mobile device the pdf is not showing it just shows white screen. and if we open it on mobile using oprt like
python manage.py runserver 0.0.0.0:8000 then btn is not clicking it just runs the function in background.
Does anybody now why it is happening? I tried few methods but none are working!
I tried using weayprint and reportlab but the issue presist in them too! I am using python 3.9, django 4.15 and xHTML2PDF of 0.2.8