Error generating PDF in Django

0

Good afternoon I have an application in Django 1.10, in which one of the functionalities is to generate some PDF documents for which weasyprint use. When I run the server and access from the machine locally the PDF is generated without any problem; but when I access from a remote machine the PDF generation does not succeed. When reviewing the error it generates, I get the following:

/home/pvca/virtual/lib/python3.5/site-packages/html5lib/_ihatexml.py:257: DataLossWarning: Coercing non-XML name
  warnings.warn("Coercing non-XML name", DataLossWarning)

I do not know what may be causing this error. I appreciate your collaboration

    
asked by jhon1946 11.06.2017 в 00:04
source

1 answer

0

as the author of this website comments: The author's website

you could use this code. The Portable Document Format (PDF, by Portable Document Format) is a format developed by Adobe that is used to represent printable documents, complete with perfect format to a level of detail measured in pixels, embedded fonts and 2D vector graphics. You can think of a PDF document as the digital equivalent of a printed document; effectively, PDFs are normally used when you need to deliver a document to someone to print it.

You can easily generate PDFs with Python and Django thanks to the excellent free software library ReportLab. The advantage of generating PDF files dynamically is that you can create customized PDFs for different purposes - say, for different users or different content.

For example, we have used Django and ReportLab on KUSports.com to generate customized NCAA tournament programs, ready to be printed.

Install ReportLab

Before you can generate any PDF, you must install ReportLab. This step is very simple, since you should only download the library on the reportlab.org/downloads.html site.

The user guide, which is only available in PDF format, contains additional installation instructions.

NOTE If you are using a modern Linux distribution, you might want to check with the software package management utility before installing ReportLab. Most package repositories already include ReportLab.

For example, if you are using the (excellent) Ubuntu distribution, a simple apt-get install python-reportlab will do the magic necessary.

Test your installation by importing it into the interactive Python interpreter:

>>> import reportlab

If that command does not throw an error, the installation worked.

Write your View

In the same way as CSV, the generation of PDFs in dynamic form with Django is simple because the ReportLab API acts on objects similar to files (file-like according to the Python jargon).

Below is an example "Hello World":

from reportlab.pdfgen import canvas
from django.http import HttpResponse
 
def hello_pdf(request):
    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=hello.pdf'
 
    # Create the PDF object, using the response object as its "file."
    p = canvas.Canvas(response)
 
    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(100, 100, "Hello world.")
 
    # Close the PDF object cleanly, and we're done.
    p.showPage()
    p.save()
    return response

Some notes are necessary:

  • We use the MIME type application / pdf. This tells the browser that the document is a PDF file and not an HTML file. If you do not include this information, web browsers will probably interpret the response as HTML, which will result in hieroglyphs in the browser window.
  • Interacting with the ReportLab API is simple: just pass the response as the first argument to canvas.Canvas. The Canvas class expects a file-like object, and the HttpResponse objects will conform to the norm.
  • All subsequent PDF generation methods are called by passing the PDF object (in this case p), not response.
  • Finally, it is important to call the showPage () and save () methods of the PDF object (otherwise you will get a corrupt PDF file).

from cStringIO import StringIO
from reportlab.pdfgen import canvas
from django.http import HttpResponse
 
def hello_pdf(request):
    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=hello.pdf'
 
    temp = StringIO()
 
    # Create the PDF object, using the StringIO object as its "file."
    p = canvas.Canvas(temp)
 
    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(100, 100, "Hello world.")
 
    # Close the PDF object cleanly.
    p.showPage()
    p.save()
 
    # Get the value of the StringIO buffer and write it to the response.
    response.write(temp.getvalue())
    return response

Complex PDFs

If you are creating a complex PDF document (or any large piece of data), consider using the cStringIO library as a temporary storage location for your PDF file. The cStringIO library provides an interface via file-like objects that is written in C for maximum efficiency.

That's the "Hello World" example above modified to use cStringIO:

from cStringIO import StringIO
from reportlab.pdfgen import canvas
from django.http import HttpResponse
 
def hello_pdf(request):
    # Create the HttpResponse object with the appropriate PDF headers.
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=hello.pdf'
 
    temp = StringIO()
 
    # Create the PDF object, using the StringIO object as its "file."
    p = canvas.Canvas(temp)
 
    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(100, 100, "Hello world.")
 
    # Close the PDF object cleanly.
    p.showPage()
    p.save()
 
    # Get the value of the StringIO buffer and write it to the response.
    response.write(temp.getvalue())
    return response

You could also see the doc of django here you have the link to your official doc: your doc

I hope you will be a friend.

    
answered by 11.06.2017 в 03:27