
Last Updated on 14 December 2019 by Roberto De Pedrini
Reading the last post, also poetic, of our IBM Champion Roberto De Pedrini (“Merge (append) several PDFs into one (Python on i)“), regarding the merge of different pdfs, I got the inspiration to tell a bit like making a pdf in Python.
It is very simple, just install the fpdf package (pip install fpdf) and then with a few instructions you will get fantastic pdf files with which we can build nice reports, with the data coming from our IbmI.
Index
Python program
from fpdf import FPDF
import webbrowser
import os.path
from DbPoweri import DbPoweri
from time import gmtime, strftime
class StampaPeso():
def __init__(self):
pass
def testata(self, datai, dataf, datain , datafi):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=20)
self.page=1
pdf.cell(200, 10, txt="Riepilogo produzione:" +datai+"-"+dataf , ln=1, align="C")
pdf.set_font("Arial", size=12)
self.dettaglio(pdf, datain , datafi)
a=strftime("%Y-%m-%d %H-%M-%S", gmtime())
I add the variable "a" to unambiguously decode the pdf with date and time
if os.path.exists("C:\\temp\\StampaProduzionePeso"+a+".pdf") is False:
#WRITE PDF TO A FOLDER
pdf.output( "C:\\temp\\StampaProduzionePeso"+a+".pdf","F").encode('latin-1')
#OPEN PDF IN A BROWSER
webbrowser.open_new_tab(r'' 'C://temp//StampaProduzionePeso'+a+'.pdf')
else:
print ("error")
return
def dettaglio(self,pdf,datain, datafi):
pdf.set_font('Arial', '', 10)
c=DbPoweri()
c.connection()
#ROWS COLUMNS FETCH
#ROWS1 TOTALS
rows, col_names=c.peso(c.c1, datain, datafi)
rows1, col_namest=c.totpeso(c.c1, datain ,datafi)
pdf.set_fill_color(192,192,192)
#PDF.CELL CREATE A CELL
#(WIDTH,HEIGHT, TXT, BORDER 0:NO BORDER 1:BORDER,
# 0 CONTINUA A DESTRA, 1 INIZIA UNA NUOVA LINEA
# ALIGN L:LEFT C:CENTER R:RIGHT)
# SET COLORS
pdf.cell(10, 10, 'Cod', 1, 0, 'C',fill = True)
pdf.cell(40, 10, 'Qta_Grammatura', 1, 0, 'C',fill = True)
pdf.cell(40, 10, 'Qta_KG', 1, 1, 'C',fill = True)
pdf.ln(0)
nr=0
posizione=0
for riga in rows:
w_x=0
for col in riga:
w_x+=1
if w_x==1:
cod=col
if w_x==2:
gramm=col
if w_x==3:
peso=col
pdf.set_fill_color(192,192,192)
pdf.cell(10, 8, str(cod), 1, 0, 'L')
pdf.cell(40, 8, str(gramm), 1, 0, 'R')
pdf.cell(40, 8, str(peso), 1, 1, 'R')
for riga in rows1:
w_x=0
for col in riga:
w_x=w_x+1
if w_x==1:
gramm=col
if w_x==2:
peso=col
pdf.cell(10, 8, "", 1, 0, 'L')
pdf.cell(40, 8, str(gramm), 1, 0, 'R')
pdf.cell(40, 8, str(peso), 1, 1, 'R')
Conclusions
Here is the development of the pdf. With a few instructions you can make excellent prints. This technique can also be used to launch the printing of pdfs linked to particular production drawings or perform printing functions for certifications and so on.
In short, this language proves to be very versatile and integrates perfectly with our IbmI.
Happy coding!
