def do_print(self, evt): # Generate the pdf with the in_memory template paper_size = self.paper_size or Constants.DEFAULT_PAPER_SIZE orient = self.paper_orientation or Constants.DEFAULT_PAPER_ORIENTATION t = Template( format=paper_size, orientation=orient, elements=[e.as_dict() for e in self.elements if not e.static] ) t.add_page() if not t['logo'] or not os.path.exists(t['logo']): # put a default logo so it doesn't throw an exception logo = os.path.join( os.path.dirname(__file__), 'tutorial', 'logo.png' ) t.set('logo', logo) try: t.render(self.filename + ".pdf") except Exception: if __debug__ and False: import pdb pdb.pm() else: raise if sys.platform.startswith("linux"): os.system('xdg-open "%s.pdf"' % self.filename) else: os.startfile(self.filename + ".pdf")
def Generate(qr,code,payload,buyer): from fpdf import Template import Supplier #test implementation print(buyer.name) elements = [ {'name':'invoice','type': 'T', 'x1': 0.0, 'y1': 0.0, 'x2': 10.0, 'y2': 30.0, 'font': 'Sans', 'size': 10.0, 'bold': 0, 'italic': 0, 'underline': 1, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'logo', 'priority': 2, }, { 'name': 'company_logo', 'type': 'I', 'x1': 15.0, 'y1': 15.0, 'x2': 25.0, 'y2': 25.0, 'font': None, 'size': 0.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'logo', 'priority': 2, }, { 'name': 'person', 'type': 'T', 'x1': 17.0, 'y1': 32.5, 'x2': 115.0, 'y2': 37.5, 'font': 'Sans', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'company_name', 'type': 'T', 'x1': 30.0, 'y1': 32.5, 'x2': 130.0, 'y2': 37.5, 'font': 'Sans', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'box', 'type': 'B', 'x1': 15.0, 'y1': 15.0, 'x2': 185.0, 'y2': 260.0, 'font': 'Sans', 'size': 0.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': None, 'priority': 0, }, { 'name': 'box_x', 'type': 'B', 'x1': 95.0, 'y1': 15.0, 'x2': 105.0, 'y2': 25.0, 'font': 'Sans', 'size': 0.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': None, 'priority': 2, }, { 'name': 'line1', 'type': 'L', 'x1': 100.0, 'y1': 25.0, 'x2': 100.0, 'y2': 57.0, 'font': 'Sans', 'size': 0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': None, 'priority': 3, }, { 'name': 'QR', 'type': 'I', 'x1': 30.0, 'y1': 30, 'x2': 60.0, 'y2': 60.0, 'font': None, 'size': 0.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': None, 'priority': 3, }, { 'name': 'company_name', 'type': 'T', 'x1': 50.0, 'y1': 32.5, 'x2': 115.0, 'y2': 37.5, 'font': 'Sans', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'company_street', 'type': 'T', 'x1': 60.0, 'y1': 32.5, 'x2': 115.0, 'y2': 37.5, 'font': 'Sans', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'company_town', 'type': 'T', 'x1': 70.0, 'y1': 32.5, 'x2': 115.0, 'y2': 37.5, 'font': 'Sans', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'company_postal', 'type': 'T', 'x1': 80.0, 'y1': 32.5, 'x2': 115.0, 'y2': 37.5, 'font': 'Sans', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'company_ico', 'type': 'T', 'x1': 90.0, 'y1': 32.5, 'x2': 115.0, 'y2': 37.5, 'font': 'Sans', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'company_dic', 'type': 'T', 'x1': 100.0, 'y1': 32.5, 'x2': 115.0, 'y2': 37.5, 'font': 'Sans', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'company_website', 'type': 'T', 'x1': 110.0, 'y1': 32.5, 'x2': 115.0, 'y2': 37.5, 'font': 'Sans', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'company_email', 'type': 'T', 'x1': 120.0, 'y1': 32.5, 'x2': 115.0, 'y2': 37.5, 'font': 'Sans', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'company_phone', 'type': 'T', 'x1': 130.0, 'y1': 32.5, 'x2': 115.0, 'y2': 37.5, 'font': 'Sans', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, ] #here we instantiate the template and define the HEADER f = Template(format="A4", elements=elements, title="Invoice") f.add_page() #we FILL some of the fields of the template with the information we want #note we access the elements treating the template instance as a "dict" f["invoice"] = f'Invoice #{code}' f["person"] = Supplier.name f["company_name"] = Supplier.company_name f["company_logo"] = Supplier.logo f["company_name"] = Supplier.company_name f["company_street"] = Supplier.company_street f["company_town"] = Supplier.company_town f["company_postal"] = Supplier.company_postal f["company_ico"] = Supplier.company_ico f["company_dic"] = Supplier.company_dic f["company_website"] = Supplier.company_website f["company_email"] = Supplier.company_email f["company_phone"] = Supplier.company_phone f["QR"] = qr #and now we render the page f.render(f'./invoice{code}.pdf')
def post(self, request): renderer = Renderer() result = renderer.render(request.data) form_uuid = result['uuid'] elements = result['items'] saveElements(elements, './back_end/crops/uploads/' + form_uuid + '.json') media_name = form_uuid + '.png' qrcode = os.path.join(settings.MEDIA_ROOT, media_name) f = Template(format="letter", elements=elements, title="DocuScan") f.add_page() f['company_logo'] = qrcode f.render("./template.pdf") os.system('open ' + './template.pdf') return Response({'uuid': "Suss"})
def create_pdf(username): #here we instantiate the template and define the HEADER f = Template(format="A4", elements=elements, title="Sample Invoice") f.add_page() #we FILL some of the fields of the template with the information we want #note we access the elements treating the template instance as a "dict" f["company_name"] = "user: "******" spry report v.1" try: f["company_logo"] = "./" + username + ".jpg" except: f["company_logo"] = "no instagram found" #and now we render the page f.render("./" + username + "-report.pdf")
def create_pdf(smth): creation_time = smth['created_time'] if int(creation_time) < int(current_print_interval_begin): print int(creation_time), int(current_print_interval_begin) sys.exit(0) f = Template(elements=elements, title="Instaprinter", format=(w, h)) f.pdf.add_font('DejaVu', '', 'DejaVuSansCondensed.ttf', uni=True) f.pdf.add_font('ProximaNova', '', 'ProximaNova-Reg.ttf', uni=True) f.add_page() musername = smth['user']['username'] mprofilepicture = smth['user']['profile_picture'] mphotourl = smth['images']['standard_resolution']['url'] if 'caption' in smth: if smth['caption'] != None: mtext = smth['caption']['text'] else: return else: return if len(mtext) > 230: mtext = mtext[:230]+ '...' mtext = re.sub(u'[^\u0020-\u00BF\u0410-\u0451]', '', mtext) mtype = smth['type'] mid = smth['id'] if mtype != 'image': return else: os.popen ('curl %s > images/content.jpg' %mphotourl) os.popen ('curl %s > images/profile.jpg' %mprofilepicture) f["address"] = config.address f["avatar"] = '''.\images\profile.jpg''' f["photo"] = '''.\images\content.jpg''' f["username"] = musername f["logo"] = '''.\images\logo.jpg''' f["place"] = '''.\images\place.png''' f["clubname"] = config.clubname f["phone"] = config.phone f["description"] = mtext f.render('''.\results\%s.pdf''' %mid) # os.popen ("lpr -P%s results/%s-5.pdf" %(config.printer_name, mid)) # os.popen ("move .\%s.pdf .\yesref" %mid) os.popen ("acrord32.exe /n /t .\results\%s.pdf" %mid) print "done %s.pdf" %mid
def create_pdf(which): recipetitle = recipe_table_dat[which][1] recipeid = recipe_table_dat[which][0] f = Template(format="Letter", elements=elements, title="Recipe Printout") print(f'Elements is a {type(elements)} structure') f.add_page() #we FILL some of the fields of the template with the information we want #note we access the elements treating the template instance as a "dict" f["header"] = f"Greg's cookbook - {recipetitle} - Recipe ID {recipeid}" f["title"] = recipetitle # 'Mongolian Beef and Spring Onions' f["recipeimage"] = images_table_dat[which][2] f["description"] = recipe_table_dat[which][8] f["source"] = recipe_table_dat[which][2] f['servings'] = f'Servings: {recipe_table_dat[which][3]}' f['time'] = f'Total Time: {recipe_table_dat[which][4]}' f['rating'] = f'Rating: {recipe_table_dat[which][5]}' itms = len(ingredients_table_dat[which]) ings = '' for itm in range(itms): ings = ings + ingredients_table_dat[which][itm][5] + "\n" f["ingredientshead"] f["ingredientitems"] = ings f["instructionhead"] f["instructions"] = instructions_table_dat[which][2] #and now we render the page filename = f'./{recipetitle}.pdf' f.render(filename) print(f'\n\n{"=" * 45}') print(' PDF has been generated') print(' Please open the PDF manually') print('=' * 45) print('\n\n')
def dotest(outputname, nostamp): # generate sample invoice (according Argentina's regulations) from decimal import Decimal f = Template(format="A4", title="Sample Invoice", author="Sample Company", subject="Sample Customer", keywords="Electronic TAX Invoice") if nostamp: f.pdf._putinfo = lambda: common.test_putinfo(f.pdf) random = randomfake() else: import random csvpath = os.path.join(common.basepath, "invoice.csv") f.parse_csv(infile=csvpath, delimiter=";", decimal_sep=",") detail = "Lorem ipsum dolor sit amet, consectetur. " * 30 items = [] for i in range(1, 30): ds = "Sample product %s" % i qty = random.randint(1,10) price = round(random.random()*100,3) code = "%s%s%02d" % (chr(random.randint(65,90)), chr(random.randint(65,90)),i) items.append(dict(code=code, unit='u', qty=qty, price=price, amount=qty*price, ds="%s: %s" % (i,ds))) # divide and count lines lines = 0 li_items = [] for it in items: qty = it['qty'] code = it['code'] unit = it['unit'] for ds in f.split_multicell(it['ds'], 'item_description01'): # add item description line (without price nor amount) li_items.append(dict(code=code, ds=ds, qty=qty, unit=unit, price=None, amount=None)) # clean qty and code (show only at first) unit = qty = code = None # set last item line price and amount li_items[-1].update(amount = it['amount'], price = it['price']) obs="\n<U>Detail:</U>\n\n" + detail for ds in f.split_multicell(obs, 'item_description01'): li_items.append(dict(code=code, ds=ds, qty=qty, unit=unit, price=None, amount=None)) # calculate pages: lines = len(li_items) max_lines_per_page = 24 pages = int(lines / (max_lines_per_page - 1)) if lines % (max_lines_per_page - 1): pages = pages + 1 # completo campos y hojas for page in range(1, int(pages)+1): f.add_page() f['page'] = 'Page %s of %s' % (page, pages) if pages>1 and page<pages: s = 'Continues on page %s' % (page+1) else: s = '' f['item_description%02d' % (max_lines_per_page+1)] = s f["company_name"] = "Sample Company" f["company_logo"] = os.path.join(common.basepath, "../tutorial/logo.png") f["company_header1"] = "Some Address - somewhere -" f["company_header2"] = "http://www.example.com" f["company_footer1"] = "Tax Code ..." f["company_footer2"] = "Tax/VAT ID ..." f['number'] = '0001-00001234' f['issue_date'] = '2010-09-10' f['due_date'] = '2099-09-10' f['customer_name'] = "Sample Client" f['customer_address'] = "Siempreviva 1234" # print line item... li = 0 k = 0 total = Decimal("0.00") for it in li_items: k = k + 1 if k > page * (max_lines_per_page - 1): break if it['amount']: total += Decimal("%.6f" % it['amount']) if k > (page - 1) * (max_lines_per_page - 1): li += 1 if it['qty'] is not None: f['item_quantity%02d' % li] = it['qty'] if it['code'] is not None: f['item_code%02d' % li] = it['code'] if it['unit'] is not None: f['item_unit%02d' % li] = it['unit'] f['item_description%02d' % li] = it['ds'] if it['price'] is not None: f['item_price%02d' % li] = "%0.3f" % it['price'] if it['amount'] is not None: f['item_amount%02d' % li] = "%0.2f" % it['amount'] if pages == page: f['net'] = "%0.2f" % (total/Decimal("1.21")) f['vat'] = "%0.2f" % (total*(1-1/Decimal("1.21"))) f['total_label'] = 'Total:' else: f['total_label'] = 'SubTotal:' f['total'] = "%0.2f" % total f.render(outputname)
def saveOrdem(): ordem = { "name": EntryName.get(), "certified": EntryCertified.get(), "phone": EntryPhone.get(), "email": EntryEmail.get(), "adress": EntryAdress.get(), "cep": EntryCEP.get(), "date": LabelDateShow['text'], "city": EntryCity.get(), "district": EntryDistrict.get(), "service": ServicePick.get(), "local": LocalPick.get(), "priority": PriorityPick.get(), "model": EntryModel.get(), "call": EntryCall.get(), "serialNumber": EntrySerialNumber.get(), "acesories": EntryAcessories.get(), "relatedProblem": EntryRelatedProblem.get(1.0, END), "problem": EntryProblem.get(1.0, END), "parts": EntryParts.get(1.0, END), "observations": EntryObservations.get(1.0, END), "payment": PaymentPick.get(), "status": StatusPick.get(), "servicePrice": EntryServicePrice.get(), "productPrice": EntryProductPrice.get() } database.append(ordem) with open('database.json', 'w') as BD: json.dump(database, BD) BD.close() #creating pdf #this will define the ELEMENTS that will compose the template. elements = [ { 'name': 'company_logo', 'type': 'I', 'x1': 20.0, 'y1': 17.0, 'x2': 78.0, 'y2': 30.0, 'font': None, 'size': 0.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'logo', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 20.0, 'y1': 42, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Nome:', 'priority': 2, }, { 'name': 'client_name', 'type': 'T', 'x1': 35.0, 'y1': 42, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 20.0, 'y1': 52, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'CPF/CNPJ:', 'priority': 2, }, { 'name': 'client_certified', 'type': 'T', 'x1': 45.0, 'y1': 52, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 85.0, 'y1': 52, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Telefone:', 'priority': 2, }, { 'name': 'client_phone', 'type': 'T', 'x1': 105, 'y1': 52, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 20.0, 'y1': 62, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'E-mail:', 'priority': 2, }, { 'name': 'client_email', 'type': 'T', 'x1': 35.0, 'y1': 62, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 120, 'y1': 62, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Endereco:', 'priority': 2, }, { 'name': 'client_adress', 'type': 'T', 'x1': 142, 'y1': 62, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 20.0, 'y1': 72, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'CEP:', 'priority': 2, }, { 'name': 'client_cep', 'type': 'T', 'x1': 32.0, 'y1': 72, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 150.0, 'y1': 0, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Data:', 'priority': 2, }, { 'name': 'date', 'type': 'T', 'x1': 161.0, 'y1': 0, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 55.0, 'y1': 72, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Cidade:', 'priority': 2, }, { 'name': 'client_city', 'type': 'T', 'x1': 72.0, 'y1': 72, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 120.0, 'y1': 72, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Bairro:', 'priority': 2, }, { 'name': 'client_district', 'type': 'T', 'x1': 135.0, 'y1': 72, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 20.0, 'y1': 102, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Servico:', 'priority': 2, }, { 'name': 'service_type', 'type': 'T', 'x1': 37.0, 'y1': 102, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 70.0, 'y1': 102, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Local:', 'priority': 2, }, { 'name': 'service_local', 'type': 'T', 'x1': 83.0, 'y1': 102, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 110.0, 'y1': 102, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Prioridade:', 'priority': 2, }, { 'name': 'service_priority', 'type': 'T', 'x1': 133.0, 'y1': 102, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 20.0, 'y1': 112, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Modelo:', 'priority': 2, }, { 'name': 'service_model', 'type': 'T', 'x1': 37.0, 'y1': 112, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 70, 'y1': 112, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Chamado:', 'priority': 2, }, { 'name': 'service_call', 'type': 'T', 'x1': 92, 'y1': 112, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 20.0, 'y1': 122, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'N/S:', 'priority': 2, }, { 'name': 'serial_number', 'type': 'T', 'x1': 29.0, 'y1': 122, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 83.0, 'y1': 122, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Acessorios:', 'priority': 2, }, { 'name': 'acessories', 'type': 'T', 'x1': 108.0, 'y1': 122, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 20.0, 'y1': 132, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Problema Ralatado:', 'priority': 2, }, { 'name': 'related_problem', 'type': 'T', 'x1': 20.0, 'y1': 142, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 20.0, 'y1': 152, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Problema Constatado:', 'priority': 2, }, { 'name': 'problem', 'type': 'T', 'x1': 20.0, 'y1': 162, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 20.0, 'y1': 172, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Pecas:', 'priority': 2, }, { 'name': 'service_parts', 'type': 'T', 'x1': 20.0, 'y1': 182, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 20.0, 'y1': 192, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Observacoes:', 'priority': 2, }, { 'name': 'observation', 'type': 'T', 'x1': 20.0, 'y1': 202, 'x2': 25.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 20.0, 'y1': 250, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Pagamento:', 'priority': 2, }, { 'name': 'payment_type', 'type': 'T', 'x1': 45.0, 'y1': 250, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 77.0, 'y1': 250, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Status:', 'priority': 2, }, { 'name': 'status', 'type': 'T', 'x1': 92.0, 'y1': 250, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 20.0, 'y1': 262, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Servico(s):', 'priority': 2, }, { 'name': 'service_value', 'type': 'T', 'x1': 42.0, 'y1': 262, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'label', 'type': 'T', 'x1': 77.0, 'y1': 262, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': 'Produtos(s):', 'priority': 2, }, { 'name': 'service_products', 'type': 'T', 'x1': 102.0, 'y1': 262, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, ] #here we instantiate the template and define the HEADER f = Template(format="A4", elements=elements, title="Ordem de Servico") f.add_page() #we FILL some of the fields of the template with the information we want #note we access the elements treating the template instance as a "dict" f["company_name"] = "Inova Service" f["company_logo"] = "img/logo.png" f["client_name"] = EntryName.get() f["client_certified"] = EntryCertified.get() f["client_phone"] = EntryPhone.get() f["client_email"] = EntryEmail.get() f["client_adress"] = EntryAdress.get() f["client_cep"] = EntryCEP.get() f["date"] = LabelDateShow['text'] f["client_city"] = EntryCity.get() f["client_district"] = EntryDistrict.get() f["service_type"] = ServicePick.get() f["service_local"] = LocalPick.get() f["service_priority"] = PriorityPick.get() f["service_model"] = EntryModel.get() f["service_call"] = EntryCall.get() f["serial_number"] = EntrySerialNumber.get() f["acessories"] = EntryAcessories.get() f["related_problem"] = EntryRelatedProblem.get(1.0, END) f["problem"] = EntryProblem.get(1.0, END) f["service_parts"] = EntryParts.get(1.0, END) f["observation"] = EntryObservations.get(1.0, END) f["payment_type"] = PaymentPick.get() f["status"] = StatusPick.get() f["service_value"] = EntryServicePrice.get() f["service_products"] = EntryProductPrice.get() #and now we render the page f.render("./Servico.pdf")
li_items[-1].update(amount = it['amount'], price = it['price']) obs="\n<U>Detail:</U>\n\n" + detail for ds in f.split_multicell(obs, 'item_description01'): li_items.append(dict(code=code, ds=ds, qty=qty, unit=unit, price=None, amount=None)) # calculate pages: lines = len(li_items) max_lines_per_page = 24 pages = int(lines / (max_lines_per_page - 1)) if lines % (max_lines_per_page - 1): pages = pages + 1 # completo campos y hojas for page in range(1, int(pages)+1): f.add_page() f['page'] = 'Page %s of %s' % (page, pages) if pages>1 and page<pages: s = 'Continues on page %s' % (page+1) else: s = '' f['item_description%02d' % (max_lines_per_page+1)] = s f["company_name"] = "Sample Company" f["company_logo"] = "../tutorial/logo.png" f["company_header1"] = "Some Address - somewhere -" f["company_header2"] = "http://www.example.com" f["company_footer1"] = "Tax Code ..." f["company_footer2"] = "Tax/VAT ID ..." f['number'] = '0001-00001234' f['issue_date'] = '2010-09-10'
def imprimirPlanilla(): from fpdf import Template from pyPdf import PdfFileWriter, PdfFileReader from datetime import date import cStringIO import os.path user = db(db.usuario.username==auth.user.username).select().first() estudiante = db(db.estudiante.ci == auth.user.username).select().first() buffer = cStringIO.StringIO() ####################################### # Primera Hoja ####################################### elements = [ { 'name': 'nombre', 'type': 'T', 'x1': 18.0, 'y1': 67.5, 'x2': 200, 'y2': 79, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'cedula', 'type': 'T', 'x1': 55.0, 'y1': 78, 'x2': 95, 'y2': 85, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'edad', 'type': 'T', 'x1': 110.0, 'y1': 78, 'x2': 140, 'y2': 85, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'sexo', 'type': 'T', 'x1': 155, 'y1': 78, 'x2': 200, 'y2': 85, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'fecha_nacimiento', 'type': 'T', 'x1': 56.0, 'y1': 87, 'x2': 69, 'y2': 94, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'correo', 'type': 'T', 'x1': 130.5, 'y1': 86.6, 'x2': 200, 'y2': 93.6, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'direccion', 'type': 'T', 'x1': 72, 'y1': 95, 'x2': 200, 'y2': 102, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'telefono_habitacion', 'type': 'T', 'x1': 60, 'y1': 113, 'x2': 110, 'y2': 120, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'telefono_otro', 'type': 'T', 'x1': 133, 'y1': 113, 'x2': 200, 'y2': 120, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'nombre_representante', 'type': 'T', 'x1': 51.0, 'y1': 130, 'x2': 69.0, 'y2': 137, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'ci_representante', 'type': 'T', 'x1': 145, 'y1': 130, 'x2': 200, 'y2': 137, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'direccion_representante', 'type': 'T', 'x1': 59, 'y1': 138, 'x2': 200, 'y2': 145, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'telefono_representante', 'type': 'T', 'x1': 36, 'y1': 156, 'x2': 69.0, 'y2': 163, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'correo_representante', 'type': 'T', 'x1': 128, 'y1': 156, 'x2': 200, 'y2': 163, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'lugar_trabajo_representante', 'type': 'T', 'x1': 50, 'y1': 164.5, 'x2': 200, 'y2': 171.5, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'direccion_trabajo_representante', 'type': 'T', 'x1': 54, 'y1':173.5, 'x2': 200, 'y2': 180.5, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'telefono_representante_oficina', 'type': 'T', 'x1': 48, 'y1': 183, 'x2': 69.0, 'y2': 190, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'si_sufre_enfermedad', 'type': 'T', 'x1': 76, 'y1': 200, 'x2': 80, 'y2': 207, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'no_sufre_enfermedad', 'type': 'T', 'x1': 97, 'y1': 200, 'x2': 107, 'y2': 207, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'enfermedad', 'type': 'T', 'x1': 119, 'y1': 200, 'x2': 200, 'y2': 207, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'indicaciones_enfermedad', 'type': 'T', 'x1': 18, 'y1': 215, 'x2': 200, 'y2': 224, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, } ] # Inicializamos el Template f = Template(format="letter", elements=elements, title="Sample Invoice") f.add_page() today = date.today() born = estudiante.fecha_nacimiento # Se llenan los campos como un "dict". f["nombre"] = user.last_name + ", " + user.first_name f["cedula"] = user.username f["edad"] = today.year - born.year - ((today.month, today.day) < (born.month, born.day)) f["sexo"] = estudiante.sexo f["fecha_nacimiento"] = born.strftime("%d/%m/%Y") f["correo"] = user.email f["direccion"] = estudiante.direccion f["telefono_habitacion"] = estudiante.telefono_habitacion f["telefono_otro"] = estudiante.telefono_otro f["nombre_representante"] = estudiante.apellido_representante + ", " + estudiante.nombre_representante f["ci_representante"] = estudiante.ci_representante f["direccion_representante"] = estudiante.direccion_representante f["telefono_representante"] = estudiante.telefono_representante_otro f["correo_representante"] = estudiante.correo_representante f["lugar_trabajo_representante"] = estudiante.trabajo_representante f["direccion_trabajo_representante"] = estudiante.direccion_trabajo_representante f["telefono_representante_oficina"] = estudiante.telefono_representante_oficina if estudiante.sufre_enfermedad: f["si_sufre_enfermedad"] = "X" f["enfermedad"] = estudiante.enfermedad f["indicaciones_enfermedad"] = estudiante.indicaciones_enfermedad else: f["no_sufre_enfermedad"] = "X" # Escribimos la hoja en el Buffer. buffer.write(f.render("template.pdf", dest='S')) buffer.seek(0) new_pdf = PdfFileReader(buffer) # Leemos la planilla original. existing_pdf = PdfFileReader(file(os.path.join(request.folder, "static", "pdfs", 'planillas.pdf'), "rb")) # Juntamos los datos junto con la planilla. output = PdfFileWriter() page = existing_pdf.getPage(0) page.mergePage(new_pdf.getPage(0)) output.addPage(page) ####################################### # Segunda Hoja ####################################### elements = [ { 'name': 'nombre_representante', 'type': 'T', 'x1': 24, 'y1': 73, 'x2': 69.0, 'y2': 80, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'ci_representante', 'type': 'T', 'x1': 151.5, 'y1': 73, 'x2': 200, 'y2': 80, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'nombre', 'type': 'T', 'x1': 123, 'y1': 82, 'x2': 170, 'y2': 89, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'cedula', 'type': 'T', 'x1': 27, 'y1': 88, 'x2': 40, 'y2': 95, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'dia', 'type': 'T', 'x1': 65, 'y1': 159, 'x2': 90, 'y2': 166, 'font': 'Arial', 'size': 13.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'mes', 'type': 'T', 'x1': 135, 'y1': 159, 'x2': 130, 'y2': 166, 'font': 'Arial', 'size': 13.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, ] # Inicializamos el Template f = Template(format="letter", elements=elements, title="Sample Invoice") f.add_page() meses = ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"] # Se llenan los campos como un "dict". f["nombre_representante"] = estudiante.apellido_representante + ", " + estudiante.nombre_representante f["ci_representante"] = estudiante.ci_representante f["nombre"] = user.last_name + ", " + user.first_name f["cedula"] = user.username f["dia"] = today.day f["mes"] = meses[today.month - 1] buffer = cStringIO.StringIO() # Escribimos la hoja en el Buffer. buffer.write(f.render("template1.pdf", dest='S')) buffer.seek(0) new_pdf2 = PdfFileReader(buffer) page = existing_pdf.getPage(1) page.mergePage(new_pdf2.getPage(0)) output.addPage(page) ####################################### # Tercera Hoja ####################################### elements = [ { 'name': 'nombre', 'type': 'T', 'x1': 27, 'y1': 62.5, 'x2': 50, 'y2': 69.5, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'cedula', 'type': 'T', 'x1': 23, 'y1': 69, 'x2': 50, 'y2': 76, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'liceo', 'type': 'T', 'x1': 125, 'y1': 69, 'x2': 200, 'y2': 76, 'font': 'Arial', 'size': 11.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, ] # Inicializamos el Template f = Template(format="letter", elements=elements, title="Sample Invoice") f.add_page() # Se llenan los campos como un "dict". f["nombre"] = user.last_name + ", " + user.first_name f["cedula"] = user.username f["liceo"] = estudiante.nombre_liceo buffer = cStringIO.StringIO() # Escribimos la hoja en el Buffer. buffer.write(f.render("template2.pdf", dest='S')) buffer.seek(0) new_pdf3 = PdfFileReader(buffer) page = existing_pdf.getPage(2) page.mergePage(new_pdf3.getPage(0)) output.addPage(page) ####################################### # Cuarta Hoja ####################################### output.addPage(existing_pdf.getPage(3)) # Guardamos el PDF final en forma de String y lo obtenemos para poderlo mostrar en pantalla. output.write(buffer) pdf = buffer.getvalue() buffer.close() header = {'Content-Type': 'application/pdf'} response.headers.update(header) return pdf
def dotest(outputname, nostamp): # generate sample invoice (according Argentina's regulations) from decimal import Decimal f = Template(format="A4", title="Sample Invoice", author="Sample Company", subject="Sample Customer", keywords="Electronic TAX Invoice") if nostamp: f.pdf._putinfo = lambda: common.test_putinfo(f.pdf) random = randomfake() else: import random csvpath = os.path.join(common.basepath, "invoice.csv") f.parse_csv(infile=csvpath, delimiter=";", decimal_sep=",") detail = "Lorem ipsum dolor sit amet, consectetur. " * 30 items = [] for i in range(1, 30): ds = "Sample product %s" % i qty = random.randint(1, 10) price = round(random.random() * 100, 3) code = "%s%s%02d" % (chr(random.randint( 65, 90)), chr(random.randint(65, 90)), i) items.append( dict(code=code, unit='u', qty=qty, price=price, amount=qty * price, ds="%s: %s" % (i, ds))) # divide and count lines lines = 0 li_items = [] for it in items: qty = it['qty'] code = it['code'] unit = it['unit'] for ds in f.split_multicell(it['ds'], 'item_description01'): # add item description line (without price nor amount) li_items.append( dict(code=code, ds=ds, qty=qty, unit=unit, price=None, amount=None)) # clean qty and code (show only at first) unit = qty = code = None # set last item line price and amount li_items[-1].update(amount=it['amount'], price=it['price']) obs = "\n<U>Detail:</U>\n\n" + detail for ds in f.split_multicell(obs, 'item_description01'): li_items.append( dict(code=code, ds=ds, qty=qty, unit=unit, price=None, amount=None)) # calculate pages: lines = len(li_items) max_lines_per_page = 24 pages = int(lines / (max_lines_per_page - 1)) if lines % (max_lines_per_page - 1): pages = pages + 1 # completo campos y hojas for page in range(1, int(pages) + 1): f.add_page() f['page'] = 'Page %s of %s' % (page, pages) if pages > 1 and page < pages: s = 'Continues on page %s' % (page + 1) else: s = '' f['item_description%02d' % (max_lines_per_page + 1)] = s f["company_name"] = "Sample Company" f["company_logo"] = os.path.join(common.basepath, "../tutorial/logo.png") f["company_header1"] = "Some Address - somewhere -" f["company_header2"] = "http://www.example.com" f["company_footer1"] = "Tax Code ..." f["company_footer2"] = "Tax/VAT ID ..." f['number'] = '0001-00001234' f['issue_date'] = '2010-09-10' f['due_date'] = '2099-09-10' f['customer_name'] = "Sample Client" f['customer_address'] = "Siempreviva 1234" # print line item... li = 0 k = 0 total = Decimal("0.00") for it in li_items: k = k + 1 if k > page * (max_lines_per_page - 1): break if it['amount']: total += Decimal("%.6f" % it['amount']) if k > (page - 1) * (max_lines_per_page - 1): li += 1 if it['qty'] is not None: f['item_quantity%02d' % li] = it['qty'] if it['code'] is not None: f['item_code%02d' % li] = it['code'] if it['unit'] is not None: f['item_unit%02d' % li] = it['unit'] f['item_description%02d' % li] = it['ds'] if it['price'] is not None: f['item_price%02d' % li] = "%0.3f" % it['price'] if it['amount'] is not None: f['item_amount%02d' % li] = "%0.2f" % it['amount'] if pages == page: f['net'] = "%0.2f" % (total / Decimal("1.21")) f['vat'] = "%0.2f" % (total * (1 - 1 / Decimal("1.21"))) f['total_label'] = 'Total:' else: f['total_label'] = 'SubTotal:' f['total'] = "%0.2f" % total f.render(outputname)
def generateMultiPDF(sampleLabels, size, index): """ Actually create the PDF for multiple sample labels. """ now = datetime.utcnow() printableFile = "multi_" + size.name + "_" + now.strftime("%Y%m%d_%H%M%S") + "_temp" + str(index) + ".pdf" # clear out any old version outputFilename = os.path.join(settings.MEDIA_ROOT, settings.XGDS_SAMPLE_PDF_DIR, printableFile) # get the template elements = getattr(labelTemplates, 'multi' + size.name) template = Template(format=[215.9, 279.4], orientation="P", elements=elements) template.add_page() qrCodeImages = [] i = 0 paragraph = size.paragraphWidth for sampleLabel in sampleLabels: if i == 10: break # make the qr code image qrCodeImages.append(generateQRCode(sampleLabel.url, sampleLabel.number)) # populate the templatto ae template[str(i) + "_qrcode"] = qrCodeImages[i] if sampleLabel.number: template[str(i) + "_id"] = sampleLabel.number try: sample = sampleLabel.sample except: sample = None # display description next to the qr code if sample: rows = [sample.name] if sample.description: rows.append(sample.description) samplePosition = sample.getPositionDict() try: if samplePosition: if samplePosition['lat'] and samplePosition['lon']: rows.append('lat, lon: ' + str(samplePosition['lat']) + ' ' + str(samplePosition['lon'])) except: pass if sample.collection_time: rows.append('collected at ' + sample.collection_time.strftime('%Y-%m-%d %H:%i %e')) finalrows = [] for row in rows: if row is not None: if len(row) > paragraph: wrapped = textwrap.wrap(row, paragraph) for l in wrapped: finalrows.append(l) else: finalrows.append(row) for j, row in enumerate(finalrows): key = "%d_row%d" % (i, j + 1) if (key) in template.keys: template[key] = row # update the record. This is also a lie, we don't know if you actually printed it, but whatever. sampleLabel.printTime = now sampleLabel.printableFile = printableFile sampleLabel.save() i = i + 1 while i < 10: template[str(i) + "_qrcode"] = os.path.join(settings.STATIC_ROOT, "xgds_sample/images/ipx.gif") i = i + 1 # make the PDF template.render(outputFilename) # remove the qr code image for qrCodeImage in qrCodeImages: os.remove(qrCodeImage) return outputFilename
def report02(self): #this will define the ELEMENTS that will compose the template. elements = [ { 'name': 'company_name', 'type': 'T', 'x1': 17.0, 'y1': 32.5, 'x2': 115.0, 'y2': 37.5, 'font': 'Arial', 'size': 12.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '', 'priority': 2, }, { 'name': 'box', 'type': 'B', 'x1': 15.0, 'y1': 15.0, 'x2': 185.0, 'y2': 260.0, 'font': 'Arial', 'size': 0.0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': None, 'priority': 0, }, { 'name': 'box_x', 'type': 'B', 'x1': 95.0, 'y1': 15.0, 'x2': 105.0, 'y2': 25.0, 'font': 'Arial', 'size': 0.0, 'bold': 1, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': None, 'priority': 2, }, { 'name': 'line1', 'type': 'L', 'x1': 100.0, 'y1': 25.0, 'x2': 100.0, 'y2': 57.0, 'font': 'Arial', 'size': 0, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': None, 'priority': 3, }, { 'name': 'barcode', 'type': 'BC', 'x1': 20.0, 'y1': 246.5, 'x2': 140.0, 'y2': 254.0, 'font': 'Interleaved 2of5 NT', 'size': 0.75, 'bold': 0, 'italic': 0, 'underline': 0, 'foreground': 0, 'background': 0, 'align': 'I', 'text': '200000000001000159053338016581200810081', 'priority': 3, }, ] #here we instantiate the template and define the HEADER f = Template(format="A4", elements=elements, title="Sample Invoice") f.add_page() #we FILL some of the fields of the template with the information we want #note we access the elements treating the template instance as a "dict" f["company_name"] = "Sample Company" #f["company_logo"] = "pyfpdf/tutorial/logo.png" #and now we render the page #f.render("/tmp/template.pdf") f.render('/tmp/template.pdf') return send_file('/tmp/template.pdf', mimetype='application/pdf')