Multiple pages using Reportlab – Django

showPage(), despite its confusing name, will actually end the current page, so anything you draw on the canvas after calling it will go on the next page. In your example, you can just use p.showPage() after each p.drawString example and they will all appear on their own page. def Print_PDF(request): response = HttpResponse(content_type=”application/pdf”) response[‘Content-Disposition’] = … Read more

dict_items object has no attribute ‘sort’

Haven’t tested but a theory: you are using python3! From https://docs.python.org/3/whatsnew/3.0.html dict methods dict.keys(), dict.items() and dict.values() return “views” instead of lists. For example, this no longer works: k = d.keys(); k.sort(). Use k = sorted(d) instead (this works in Python 2.5 too and is just as efficient). as I understand it a “view” is … Read more

Export Pandas DataFrame into a PDF file using Python

First plot table with matplotlib then generate pdf import pandas as pd import numpy as np import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages df = pd.DataFrame(np.random.random((10,3)), columns = (“col 1”, “col 2”, “col 3″)) #https://stackoverflow.com/questions/32137396/how-do-i-plot-only-a-table-in-matplotlib fig, ax =plt.subplots(figsize=(12,4)) ax.axis(‘tight’) ax.axis(‘off’) the_table = ax.table(cellText=df.values,colLabels=df.columns,loc=”center”) #https://stackoverflow.com/questions/4042192/reduce-left-and-right-margins-in-matplotlib-plot pp = PdfPages(“foo.pdf”) pp.savefig(fig, bbox_inches=”tight”) pp.close() reference: How do I … Read more

How to set any font in reportlab Canvas in python?

Perhabs Tahoma is a TrueType font, and you need to register it first. According to the user guide of ReportLab you need to do this: from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont pdfmetrics.registerFont(TTFont(‘Vera’, ‘Vera.ttf’)) pdfmetrics.registerFont(TTFont(‘VeraBd’, ‘VeraBd.ttf’)) pdfmetrics.registerFont(TTFont(‘VeraIt’, ‘VeraIt.ttf’)) pdfmetrics.registerFont(TTFont(‘VeraBI’, ‘VeraBI.ttf’)) canvas.setFont(‘Vera’, 32) canvas.drawString(10, 150, “Some text encoded in UTF-8”) canvas.drawString(10, 100, “In the Vera … Read more

tech