Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
adjuntar archivo img en pdf weasyprint

Necesito ayuda para adjuntar archivos img en pdf. Usamos WeasyPrint lib para generar pdf desde html.

en html conecta un archivo img como este

 <img src="1.png" alt=""> <img src="2.png" alt=""> <img src="3.png" alt="">

Pero no está funcionando. no veo la imagen

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

use static para la ruta del archivo de imagen

 {% load static %} <img src="{% static 'images/static.jpg' %}" alt="">

y pase base_url en la clase HTML en views.py

 pdf_file = HTML(string=rendered_html, base_url=request.build_absolute_uri())

archivo html

 <!DOCTYPE html> <html lang="en"> {% load static %} <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div> <img src="{% static 'images/static.jpg' %}" alt=""> </div> </body> </html>

vistas.py

 from django.template.loader import get_template from weasyprint import HTML, CSS from django.conf import settings from django.http import HttpResponse def generate_pdf(request): html_template = get_template('latest/html_pdf.html') user = request.user rendered_html = html_template.render().encode(encoding="UTF-8") pdf_file = HTML(string=rendered_html, base_url=request.build_absolute_uri()).write_pdf(stylesheets=[CSS(settings.STATIC_ROOT + '/css/generate_html.css')]) http_response = HttpResponse(pdf_file, content_type='application/pdf') http_response['Content-Disposition'] = 'filename="generate_html.pdf"' return http_response
about 4 years ago · Santiago Trujillo Denunciar

0

En caso de enviar correos electrónicos con pdf adjunto, es posible pasar la ruta de la vista a la función dedicada a los correos electrónicos. vistas.py

 [...] path = request.build_absolute_uri() # build absolute path order_confirmation.delay(order.id, path) # pass to func [...]

tareas.py

 @app.task def order_confirmation(order_id, path): # receive path order = Order.objects.get(id=order_id) subject = f"Order nr. {order.id}" email_from = settings.EMAIL email_to = order.get_email message = (...) email = EmailMessage(subject, message, email_from, [email_to]) html = render_to_string('pdf.html', {'order' : order, 'company': company}) out = BytesIO() stylesheets=[weasyprint.CSS(settings.STATIC_ROOT + '/css/pdf.css')] weasyprint.HTML(string=html, base_url=path).write_pdf(out, stylesheets=stylesheets) # use here email.attach(f'order_{order.id}.pdf', out.getvalue(), 'application/pdf') email.send()
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda