I'm encountering a peculiar issue in my Django project involving xhtml2pdf. When I attempt to generate a PDF using xhtml2pdf, the images, which load correctly when viewing the Django page, fail to display in the generated PDF. in my setting:
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
My models
class Reserva(models.model):
ImagenCarnet = models.ImageField(upload_to='images/', default='App/carnets/example.jpg')`
My views.py
def DescargarReserva(request, id):
reservas = Reserva.objects.filter(idSolicitud=id)
Orden=Reserva()
data={'reservas':reservas, 'Orden':Orden}
pdf_response=genpdf('templatesApp/pdf.html', data)
if pdf_response:
response = HttpResponse(pdf_response, content_type='application/pdf')
#agrecar variable con id como nombre de archivo quizas se usa {}
response['Content-Disposition'] = 'attachment; filename="reserva.pdf"'
return response
return HttpResponse("Error al generar el PDF", status=500)
In my template
<img src="{{reserva.ImagenCarnet.url}}" style="max-width: 300px; max-height: 200px;" />
I've tried using {{ MEDIA_URL }}{{ reserva.ImagenCarnet.url }}
, but it doesn't seem to resolve the issue. The images load correctly when accessing the Django page, but in the PDF generated by xhtml2pdf, they are not displayed.
Here are the images illustrating the problem:
With Django, loads correctly
With xhtml2pdf, does not load correctly
File source (for reference)
I'm seeking guidance on how to resolve this discrepancy between image loading in Django and xhtml2pdf. Any insights or suggestions would be greatly appreciated.