def translate(self, label=False): # begin translate self.pageProperties = Sla2pdfUtils.pageProFunc(self.slaDocumentTag()) self.canvas = Canvas(filename = self.slafile + '.pdf', pagesize=(self.pageProperties[0][8],self.pageProperties[0][7])) # Page's table reiter = False self.pdfPage = 0 tablepropertys = self.findTablesProperties() iteratable = self.getIteratableGroups(tablepropertys) for self.pdfPage in xrange(0, self.numPages): print("PAGINA", self.pdfPage, "DI", self.numPages) if label: group = tablepropertys[self.pdfPage].keys()[0] tabpro = tablepropertys[self.pdfPage][group] reiter = True # Closing pages (elements of the same page must be near) if "noGroup" in group and tabpro["pfile"] != "": self.drawImage(group=group, tabpro=tabpro) # IMMAGINE elif "noGroup" in group and tabpro["pfile"] == "": self.drawTable(group=group, monocell=True, tabpro=tabpro) # MONOCELLA else: self.drawTable(group=group, reiter=reiter, tabpro=tabpro) # TABELLA self.canvas.saveState() self.canvas.showPage() else: for tableproperty in tablepropertys: for group in tableproperty.keys(): if self.pbar: pbar(self.pbar,pulse=True,text="GEN STAMPA ATTENDERE 2 di 2") tabpro = tableproperty[group] try: group= group.strip().split('%%%')[0] except: group= group.strip() if group in iteratable: colu = int(tabpro['columns']) # tabpro['iterproper'] = tabpro['parasobj'][colu:(colu*2)] reiter = True n_cells = int(tabpro['n_cells']) # Closing pages (elements of the same page must be near) if "noGroup" in group and tabpro["pfile"] != "" : self.drawImage(group=group, tabpro=tabpro) # IMMAGINE elif "noGroup" in group and tabpro["pfile"] == "": self.drawTable(group =group, monocell=True, tabpro=tabpro)# MONOCELLA else: self.drawTable(group =group, reiter = reiter, tabpro = tabpro) # TABELLA self.canvas.saveState() self.canvas.showPage() self.canvas.save() # temporary pdf file is removed immediately filename = self.slafile + '.pdf' f = file(filename, 'rb') result = f.read() f.close() # os.remove(filename) return result
def duplicateElementLabel(self): """ Funzione base per la gestione delle frontaline: La gestione frontaline ha problematiche differenti rispetto ai report ed alle stampe singole. Il template contiene una frontalina di base che deve essere replicata enne volte quante dovuto uguale a se stessa per poi gestirne i tag rendendoli "iteranti". La parte meno complessa è quella della traduzione in pdf in quanto lo sla finito non rappresenta complessità particolari """ numPages = self.slaPage() document = self.slaDocumentTag() self.pageProperties = Sla2pdfUtils.pageProFunc(document) group = self.tablesProperties[0].keys()[0] self.tablesPropertie = self.tablesProperties[0][group] widths = [float(x.get("WIDTH")) for x in self.tablesPropertie['cells']] heights = [float(x.get("HEIGHT")) for x in self.tablesPropertie['cells']] cells = int(self.tablesPropertie['n_cells']) columns = int(self.tablesPropertie['columns']) rows = int(self.tablesPropertie['rows']) sumRows = reduce(operator.add, heights[:rows]) sumColumns = reduce(operator.add, widths[:columns]) otherColumn = sumColumns + (int(setconf("Label", "sistemacolonnafrontaline") or 0)) otherRows = sumRows + (int(setconf("Label", "sistemarigafrontaline") or 0 )) pageYpos = float(numPages[0].get('PAGEYPOS')) pageXpos = float(numPages[0].get('PAGEXPOS')) pageHeight = float(numPages[0].get('PAGEHEIGHT')) borderTop = float(numPages[0].get('BORDERTOP')) borderBottom = float(numPages[0].get('BORDERBOTTOM')) borderLeft = float(numPages[0].get('BORDERLEFT')) borderRight = float(numPages[0].get('BORDERRIGHT')) pageWidth = float(numPages[0].get('PAGEWIDTH')) realHeightPage = pageHeight - (borderTop + borderBottom) realWidthPage = pageWidth - (borderLeft + borderRight) NumMaxRowLabelForPage = int(realHeightPage/otherRows) NumMaxColumnLabelForPage = int(realWidthPage /otherColumn) NumMaxLabelForPageTotal = NumMaxRowLabelForPage*NumMaxColumnLabelForPage NumLabelInDao = len(self.objects) pagesNumber = int((NumLabelInDao/NumMaxLabelForPageTotal)) +1 self.createPageTag(pagesNumber) labelObj = self.slaPageObjects() for j in range(1, NumLabelInDao): for pageObject in labelObj: ## Creating dictionary attributes pageobject attributes = pageObject.items() dictionary = {} for k in range(0, len(attributes)): dictionary[attributes[k][0]] = attributes[k][1] ## Applying attributes app = pageObject.makeelement('PAGEOBJECT', dictionary) ## Creating dictionary attributes itext of the pageobject itexts = pageObject.findall('ITEXT') for itext in itexts: attributes = itext.items() dictionary = {} for kk in range(0, len(attributes)): dictionary[attributes[kk][0]] = attributes[kk][1] ## Applying attributes ElementTree.SubElement(app, 'ITEXT', dictionary) trails = pageObject.findall('trail') for trai in trails: attributes = trai.items() dictTrai = {} for attrrr in attributes: dictTrai[attrrr[0]] = attrrr[1] ElementTree.SubElement(app, 'trail', dictTrai) paras = pageObject.findall('para') for para in paras: attributes = para.items() dictPara = {} for kkk in range(0, len(attributes)): dictPara[attributes[kkk][0]] = attributes[kkk][1] ## Applying attributes ElementTree.SubElement(app, 'para', dictPara) pageItemAttributes = pageObject.findall('pageItemAttributes') ElementTree.SubElement(app, 'pageItemAttributes') ## Number page inigroup = str(pageObject.get('GROUPS')).strip() x = str(j+int(inigroup))+ " " app.set('GROUPS', x) app.set('OwnPage', str(j)) ## Coordinates ypos = pageObject.get('YPOS') height = pageObject.get('HEIGHT') xpos = pageObject.get('XPOS') if (j/NumMaxLabelForPageTotal) >= 1: page = int(j/NumMaxLabelForPageTotal) else: page = 0 colonna = NumMaxColumnLabelForPage- ((NumMaxLabelForPageTotal*(page+1) - (j+1)) / NumMaxRowLabelForPage)-1 riga = j- (NumMaxRowLabelForPage * (j/NumMaxRowLabelForPage)) if not j%NumMaxRowLabelForPage: app.set('YPOS',str(float(ypos)+((float(pageHeight)+float(borderTop))*page))) else: app.set('YPOS',str(float(ypos)+((float(pageHeight)+float(borderTop))*page)+ (otherRows* riga))) app.set('XPOS',str(float(xpos) + otherColumn* (colonna))) self.slaDocumentTag().append(app) app = {} # self.doc.write(self.pdfFolder+"_tempppPrima.sla") self.labelSla()