Mar-15-2017, 05:50 PM
I need to generate pdf report Avery 5160 format. I am having trouble displaying report on multiple pages.
I am using json_list as a sample.
I think I am doing something wrong in the label_generator. The output is just one page with 30 labels
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
[ / font][ / color] [color = #222222][font=Verdana, arial, sans-serif]""" [ / font][ / color] Created by Egor Kozitski """ from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import LETTER from reportlab.lib.units import inch,mm from reportlab.graphics.barcode import code128 import json,sys,operator,itertools """ @class LabelGenerator will generate Lbels using reportlab @param filelocation is the location of the file where file will be saved """ class LabelGenerator(): def __init__( self ,fileLocation): self .fileLocation = fileLocation self .canv = canvas.Canvas( self .fileLocation,pagesize = LETTER) self .canv.setPageCompression( 0 ) #LABELW is the width of the label self .LABELW = 2.6 * inch #LABELSEP is the label separation self .LABELSEP = 2.75 * inch #LABELH is the height of the label self .LABELH = 1 * inch #helper method to split list into chunks of 30 elements def grouper( self ,n, iterable): #create iterable object out of iterable it = iter (iterable) #iterate infinitely and create chunks while True : #slice iterable by n ammount of elements chunk = list (itertools.islice(it, n)) if not chunk: return #yield back the result yield chunk def label_position( self ,ordinal): """ @method label_position will find the coordinates for the next label @param ordinal is the label number for which to find postion """ y,x = divmod (ordinal , 3 ) x = 14 + x * self .LABELSEP y = 756 - y * self .LABELH #return the coordinates return x , y def label_generator( self ,json_list): """ @method label_generator will generate labels for n ammount of items """ # self.generate_label(json_list) if len (json_list) < 30 : self .generate_label(json_list) else : for label in list ( self .grouper( 30 ,json_list)): self .generate_label(label) self .canv.showPage() self .canv.save() def generate_label( self ,json_list): """ @method generate_label will iterate over list of labels and generate labels """ #iterate over list of labels for i in range ( 1 , len (json_list) + 1 ): #iterate over the label to extract Order and make sure that its matches index for item in json_list: #if we find the match than generate label if item[ 'Order' ] = = str (i): #set barcode width barcode_setBarWidth = 1 #set coordinates for label x,y = self .label_position(i - 1 ) #while true iterate infinitely and generate the right baarcode while True : barcode = code128.Code128(item[ 'UPC' ],barHeight = 6 * mm,\ barWidth = barcode_setBarWidth) if barcode.width > = 300 : barcode_setBarWidth = barcode_setBarWidth - 0.01 else : break #get image from the item and scale it to the size of the label #postion it to the right position self .canv.drawImage( "image.png" ,x,y,width = self .LABELW,height = - self .LABELH + 10 ) #create text object which will hold UPC number #position text in the label text = self .canv.beginText(x + 22 ,y - 55 ) #draw barcode on the label barcode.drawOn( self .canv,x - 15 ,y - 48 ) #set text style and fond text.setFont( 'Helvetica' , 7 , 7 ) #set the text to be drawn text.textLines(item[ 'UPC' ]) #draw text on the canvas self .canv.drawText(text) #save it into pdf # self.canv.save() if __name__ = = '__main__' : json_list = """[{"TemplateID":"Microsoft 4YM-0016","UPC":"854024427546","Order":"1"},{"TemplateID":"Microsoft 4YM-0016","UPC":"896070896551","Order":"2"},{"TemplateID":"Microsoft 4YM-0016","UPC":"492535039795","Order":"3"},{"TemplateID":"Microsoft 4YM-0016","UPC":"980144179532","Order":"4"},{"TemplateID":"Microsoft 4YM-0016","UPC":"953237474752","Order":"5"},{"TemplateID":"Microsoft 4YM-0016","UPC":"800403475381","Order":"6"},{"TemplateID":"Microsoft 4YM-0016","UPC":"459867062992","Order":"7"},{"TemplateID":"Microsoft 4YM-0016","UPC":"314882354925","Order":"8"},{"TemplateID":"Microsoft 4YM-0016","UPC":"421298698843","Order":"9"},{"TemplateID":"Microsoft 4YM-0016","UPC":"146153461080","Order":"10"},{"TemplateID":"Microsoft 4YM-0016","UPC":"854805864303","Order":"11"},{"TemplateID":"Microsoft 4YM-0016","UPC":"678579770722","Order":"12"},{"TemplateID":"Microsoft 4YM-0016","UPC":"168480935247","Order":"13"},{"TemplateID":"Microsoft 4YM-0016","UPC":"668791157220","Order":"14"},{"TemplateID":"Microsoft 4YM-0016","UPC":"740939140828","Order":"15"},{"TemplateID":"Microsoft 4YM-0016","UPC":"001570965556","Order":"16"},{"TemplateID":"Microsoft 4YM-0016","UPC":"324975221753","Order":"17"},{"TemplateID":"Microsoft 4YM-0016","UPC":"120476838669","Order":"18"},{"TemplateID":"Microsoft 4YM-0016","UPC":"598077900477","Order":"19"},{"TemplateID":"Microsoft 4YM-0016","UPC":"250843881374","Order":"20"},{"TemplateID":"Microsoft 4YM-0016","UPC":"811257091846","Order":"21"},{"TemplateID":"Microsoft 4YM-0016","UPC":"915409953650","Order":"22"},{"TemplateID":"Microsoft 4YM-0016","UPC":"881926369040","Order":"23"},{"TemplateID":"Microsoft 4YM-0016","UPC":"062532812772","Order":"24"},{"TemplateID":"Microsoft 4YM-0016","UPC":"073714100107","Order":"25"},{"TemplateID":"Microsoft 4YM-0016","UPC":"212960117066","Order":"26"},{"TemplateID":"Microsoft 4YM-0016","UPC":"506050029642","Order":"27"},{"TemplateID":"Microsoft 4YM-0016","UPC":"016286646554","Order":"28"},{"TemplateID":"Microsoft 4YM-0016","UPC":"294969321128","Order":"29"},{"TemplateID":"Microsoft 4YM-0016","UPC":"929011126614","Order":"30"},{"TemplateID":"Microsoft 4YM-0016","UPC":"529476319910","Order":"31"},{"TemplateID":"Microsoft 4YM-0016","UPC":"727535059435","Order":"32"},{"TemplateID":"Microsoft 4YM-0016","UPC":"986364488579","Order":"33"},{"TemplateID":"Microsoft 4YM-0016","UPC":"395522038237","Order":"34"},{"TemplateID":"Microsoft 4YM-0016","UPC":"229477467590","Order":"35"},{"TemplateID":"Microsoft 4YM-0016","UPC":"918033470486","Order":"36"},{"TemplateID":"Microsoft 4YM-0016","UPC":"800393354664","Order":"37"},{"TemplateID":"Microsoft 4YM-0016","UPC":"088054124430","Order":"38"},{"TemplateID":"Microsoft 4YM-0016","UPC":"324938407073","Order":"39"},{"TemplateID":"Microsoft 4YM-0016","UPC":"830941102460","Order":"40"},{"TemplateID":"Microsoft 4YM-0016","UPC":"941487562347","Order":"41"},{"TemplateID":"Microsoft 4YM-0016","UPC":"801340683962","Order":"42"},{"TemplateID":"Microsoft 4YM-0016","UPC":"189323958875","Order":"43"},{"TemplateID":"Microsoft 4YM-0016","UPC":"746227509638","Order":"44"},{"TemplateID":"Microsoft 4YM-0016","UPC":"055060376660","Order":"45"},{"TemplateID":"Microsoft 4YM-0016","UPC":"446781569032","Order":"46"},{"TemplateID":"Microsoft 4YM-0016","UPC":"423709974662","Order":"47"},{"TemplateID":"Microsoft 4YM-0016","UPC":"587053879378","Order":"48"},{"TemplateID":"Microsoft 4YM-0016","UPC":"747103897209","Order":"49"},{"TemplateID":"Microsoft 4YM-0016","UPC":"258132557428","Order":"50"},{"TemplateID":"Microsoft 4YM-0016","UPC":"488228790214","Order":"51"},{"TemplateID":"Microsoft 4YM-0016","UPC":"425761168401","Order":"52"},{"TemplateID":"Microsoft 4YM-0016","UPC":"827175725340","Order":"53"},{"TemplateID":"Microsoft 4YM-0016","UPC":"560804923240","Order":"54"},{"TemplateID":"Microsoft 4YM-0016","UPC":"161877874043","Order":"55"},{"TemplateID":"Microsoft 4YM-0016","UPC":"886087107202","Order":"56"},{"TemplateID":"Microsoft 4YM-0016","UPC":"211523352048","Order":"57"},{"TemplateID":"Microsoft 4YM-0016","UPC":"818659045277","Order":"58"},{"TemplateID":"Microsoft 4YM-0016","UPC":"960161455387","Order":"59"},{"TemplateID":"Microsoft 4YM-0016","UPC":"802917018499","Order":"60"},{"TemplateID":"Microsoft 4YM-0016","UPC":"656937238692","Order":"61"},{"TemplateID":"Microsoft 4YM-0016","UPC":"694357455377","Order":"62"},{"TemplateID":"Microsoft 4YM-0016","UPC":"735951196240","Order":"63"},{"TemplateID":"Microsoft 4YM-0016","UPC":"678074643581","Order":"64"},{"TemplateID":"Microsoft 4YM-0016","UPC":"551659479192","Order":"65"},{"TemplateID":"Microsoft 4YM-0016","UPC":"527330347299","Order":"66"},{"TemplateID":"Microsoft 4YM-0016","UPC":"637794765129","Order":"67"},{"TemplateID":"Microsoft 4YM-0016","UPC":"806857974484","Order":"68"},{"TemplateID":"Microsoft 4YM-0016","UPC":"303689331607","Order":"69"}]""" #create object label = LabelGenerator( "OttoVonBismark" ) #generate pdf file [color = #222222][font=Verdana, arial, sans-serif] label.label_generator(json.loads(json_list)) [ / font][ / color] [color = #222222][font=Verdana, arial, sans-serif] |
I am using json_list as a sample.
I think I am doing something wrong in the label_generator. The output is just one page with 30 labels