简体   繁体   中英

Django Reportlab using HTML

Hello guys I'm trying to make a little PDF with python django using the reportlab library I've made some pdf with just some text but I have no idea how to do it with html, I wonder if you guys can give me an example using something like <h1>Hello</h1> or something with html, because if I use drawString it show me '<h1>HELLO</h1>"

Let me show you my source.

from reportlab.pdfgen import canvas
from django.http import HttpResponse
from reportlab.lib.pagesizes import letter
from reportlab.lib.utils import ImageReader
import os
from io import BytesIO
import PIL.Image

def index(request):
    return HttpResponse('Hola Marcos :D')


def reporte(request):
    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="informe.pdf"'


    # Create the PDF object, using the response object as its "file."
    buffer = BytesIO()
    p = canvas.Canvas(response, pagesize=letter)

    logo = ImageReader('http://django-unfriendly.readthedocs.io/en/latest/_static/img/python-logo-256.png')

    numero =150
    uno = 204 - numero
    dos = uno

    p.drawImage(logo, 250, 500,uno,dos, mask='auto')

    p.setLineWidth(.1)
    p.setFont('Helvetica',22)
    p.drawString(30,750,'Company')

    p.setFont('Helvetica',22)
    p.drawString(30,725,'Report')

    p.setFont('Helvetica-Bold', 12)
    p.drawString(480,759,"7/01/1986")
    p.line(460,747,560,747)

    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    suma = (7*75675678567856785)*70+2*9090
    suma = str(suma)
    resta = 100-9
    resta = str(resta)

    p.drawString(100, 630, 'Este podria ser el primer informe de empresa con python Django')
    p.drawString(100, 600, suma)
    p.drawString(100, 590, resta)
    p.drawString(100, 570, 'O2A5X1996A3B4B4A6')


    # Close the PDF object cleanly, and we're done.
    p.showPage()
    p.save()
    pdf = buffer.getvalue()
    buffer.close()
    response.write(pdf)
    return response

# Create your views here.

不直接回答您的问题,但是您可以通过HTML创建PDF, https://pypi.python.org/pypi/django-wkhtmltopdf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM