Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def print_pdf():
- from string import Template
- from xhtml2pdf.pisa import CreatePDF
- from io import BytesIO
- html = """
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Django HTMLtoPDF Tutorial</title>
- </head>
- <body>
- <h1>HTML To PDF</h1>
- <b>I'm building web-based, data-driven apps using Django. <br>
- Eventually (or unfortunately), I will need to generate some reports that are printer-friendly. <b>
- Logically, PDF is the format for such files... <br>
- so how am I going to convert my xHTML and CSS to a nice-looking PDF document?<br>
- The Django Book has a whole chapter dedicated to Generating Non-HTML Content.<br>
- They seem to to be fond of ReportLab ToolKit.<br>
- The caveat here, though, is that you need to know a bit about the internals of a PDF document.<br>
- If you're familiar with this, the ReportLab toolkit seems to be the way to go! It has many features, and it seems to be a powerful PDF-generating tool.
- </b>
- </body>
- </html>
- """
- response.headers['Content-Type'] = 'application/pdf'
- in_mem_stream = BytesIO() # type: BytesIO
- CreatePDF(html, dest=in_mem_stream)
- pdf = in_mem_stream.getvalue() # type: bytes
- in_mem_stream.close()
- return pdf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement