def showpdf(request): sign = os.path.join(settings.MEDIA_ROOT, "signature.png") mimetypes.init() response = None if 'f' in request.GET: fr = open( os.path.join(settings.MEDIA_ROOT, 'pdffiles', 'extracted', '%s' % request.GET['f']), "rb") imgTemp = StringIO() imgDoc = canvas.Canvas(imgTemp) if request.GET['o'] == 'l': imgDoc.drawImage(sign, 529, 40, 290 / 2, 154 / 2) else: imgDoc.drawImage(sign, 70, 40, 290 / 2, 154 / 2) imgDoc.save() overlay = PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0) page = PdfFileReader(fr).getPage(0) page.mergePage(overlay) pdf_out = PdfFileWriter() pdf_out.addPage(page) response = HttpResponse(mimetype='application/pdf') response[ 'Content-Disposition'] = 'attachment; filename=%s' % request.GET[ 'f'] pdf_out.write(response) return response
def applica_firma(file_firma, pdf_file): # Using ReportLab to insert image into PDF imgTemp = StringIO() imgDoc = canvas.Canvas(imgTemp) buff = 100 # Draw image on Canvas and save PDF in buffer if os.path.isfile(file_firma): imgPath = file_firma imgDoc.drawImage(imgPath, 200, 190 - buff, 200, 75) p = imgDoc.beginPath() p.moveTo(200, 210 - buff) p.lineTo(400, 210 - buff) imgDoc.drawPath(p, stroke=1, fill=1) imgDoc.setFont("Helvetica", 8) imgDoc.drawString(260, 195 - buff, "(Firma del Richiedente)") imgDoc.save() # Use PyPDF to merge the image-PDF into the template page = PdfFileReader(file(pdf_file, "rb")).getPage(0) overlay = PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0) page.mergePage(overlay) os.rename(pdf_file, pdf_file[:-4] + "_A.pdf") #Save the result output = PdfFileWriter() output.addPage(page) output.write(file(pdf_file, "w"))
def showpdf(request): sign = os.path.join(settings.MEDIA_ROOT, "signature.png") mimetypes.init() response = None if 'f' in request.GET: fr = open(os.path.join(settings.MEDIA_ROOT,'pdffiles','extracted','%s' % request.GET['f']), "rb") imgTemp = StringIO() imgDoc = canvas.Canvas(imgTemp) if request.GET['o'] == 'l': imgDoc.drawImage(sign, 529, 40, 290/2, 154/2) else: imgDoc.drawImage(sign, 70, 40, 290/2, 154/2) imgDoc.save() overlay = PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0) page = PdfFileReader(fr).getPage(0) page.mergePage(overlay) pdf_out = PdfFileWriter() pdf_out.addPage(page) response = HttpResponse(mimetype='application/pdf') response['Content-Disposition'] = 'attachment; filename=%s' % request.GET['f'] pdf_out.write(response) return response
def assembleBooklet(inFilename, outFilename, opts): """ """ opts.paperSize = getattr(pagesizes, opts.paperSize) blank, canvas = createBlankPDF(StringIO(), opts) reader, writer, stream = _prepFiles(inFilename, outFilename) pf = booklet.pechaFactory(reader.getNumPages()) for sheet in pf.sheets: page = side = None for block in sheet.blocks: if side != block.side: if page != None: # finish up the old page writer.addPage(page) # make new PDF page page = PdfFileReader(blank).getPage(0) side = block.side blockPDF = reader.getPage(block.number - 1) # crop the page to pecha size params = ( reader.getPage(index), 0, pechaHeight, float(opts.paperSize[1]), float(opts.paperSize[0])) blockPDF = cropPDFPage(*params) # rotate if need be if block.orientation == UPSIDEDOWN: blockPDF.rotateClockwise(180) # figure out where on the page to put it blockPDF = placeBlock(blockPDF, block.location) page.mergePage(blockPDF) writer.write(stream) stream.close() blank.close()
def build_ticket(ticket, owner = None, \ background = "ticket-template.pdf", \ ticket_x = 2.58, ticket_y = 5.93, ticket_size = 3.14, \ ticket_print_as_string = True, ticket_sx = 7.2, ticket_sy = 7.5, owner_x = 7.2, owner_y = 7): tempImg = tempfile.mkstemp() tempName = tempImg[1] qr_item = qrencode.encode_scaled(ticket, 200, level=qrencode.QR_ECLEVEL_H) qr_item[2].save(tempName, "PNG") overlayData = StringIO() overlay = canvas.Canvas(overlayData, pagesize=A4, bottomup=0) if owner != None: overlay.setFont('Helvetica', 20) overlay.drawString(owner_x * cm, owner_y * cm, owner) if ticket_print_as_string: overlay.setFont('Courier', 8) overlay.drawString(ticket_sx * cm, ticket_sy * cm, ticket) overlay.drawImage(tempName, ticket_x * cm, ticket_y * cm, ticket_size * cm, ticket_size * cm) overlay.save() page = PdfFileReader(file(background,"rb")).getPage(0) overlayPage = PdfFileReader(StringIO(overlayData.getvalue())).getPage(0) page.mergePage(overlayPage) outputFile = tempfile.mkstemp() output = PdfFileWriter() output.addPage(page) output.write(file(outputFile[1],"w")) os.remove(tempName) return outputFile
def applyBarcode(browser,volumeNum): bcImage = browser.find_element_by_xpath("/html/body/div/div[3]/div[2]/div[2]/div/div[2]/div[4]/a/img") location = bcImage.location size = bcImage.size print "taking screenshot" browser.save_screenshot('barcode.png') print "cropping screen screenshot" im = Image.open('barcode.png') left = location['x'] top = location['y'] right = location['x'] + size['width'] bottom = location['y'] + size['height'] im = im.crop((left, top, right, bottom)) # defines crop points im.save('barcode.png') # saves new cropped image imgWidth = (im.size[0])*.4 imgHeight = (im.size[1])*.4 # Using ReportLab to insert image into PDF imgTemp = StringIO() imgDoc = canvas.Canvas(imgTemp) # Draw image on Canvas and save PDF in buffer imgPath = pwd+"/barcode.png" imgDoc.drawImage(imgPath, 303, 115, imgWidth, imgHeight) ## at (399,760) with size 160x160 imgDoc.save() # Use PyPDF to merge the image-PDF into the template page = PdfFileReader(file(cFolder+"/volume&&&"+volumeNum+".pdf","rb")).getPage(0) overlay = PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0) page.mergePage(overlay) #Save the result output = PdfFileWriter() output.addPage(page) output.write(file(cFolder+"/bcVolume&&&"+volumeNum+".pdf","w")) print "reworked img success w/PIL :)"
def placeBarcode(volumeNum): #for adding the barcode to the pdf cover. # Using ReportLab to insert image into PDF imgTemp = StringIO() imgDoc = canvas.Canvas(imgTemp) # Draw image on Canvas and save PDF in buffer imgPath = "/Users/wiki/repos/printwikipedia/curl/barcode.png" imgDoc.drawImage(imgPath, 175, 60, 190, 80) ## at (399,760) with size 160x160 imgDoc.save() volumeNum = str(volumeNum) input_cp = "/Users/wiki/repos/printwikipedia/dist/covers/volume"+volumeNum+".pdf" output_cp = "/Users/wiki/repos/printwikipedia/dist/covers/bar_volume"+volumeNum+".pdf" print volumeNum + " this is volnum" # Use PyPDF to merge the image-PDF into the template page = PdfFileReader(file(input_cp,"rb")).getPage(0) overlay = PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0) page.mergePage(overlay) #Save the result output = PdfFileWriter() output.addPage(page) output.write(file(output_cp,"w"))
#debug #myCanvas = canvas.Canvas('myfile.pdf', pagesize=A4)# #wid, heig = A4 #keep for later #print wid, heig for sourceFile in os.listdir(str(args.input)): if sourceFile[-4:] != ".pdf" : continue #debug print sourceFile # Using ReportLab to insert image into PDF imgTemp = StringIO() imgDoc = canvas.Canvas(imgTemp) # Draw image on Canvas and save PDF in buffer imgPath = args.stamp imgDoc.drawImage(imgPath, 0, 0, width, height, mask='auto') ## at (399,760) with size width x height imgDoc.save() # Use PyPDF to merge the image-PDF into the template page = PdfFileReader(file(sourceFile,"rb")).getPage(0) overlay = PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0) page.mergePage(overlay) #Save the result outputFile = os.path.join(args.output, sourceFile) output = PdfFileWriter() output.addPage(page) output.write(file( outputFile,"w"))
def applyBarcode( self ): # gets the barcode from barcode page, saves it, applies it to both the copyright .pdf and the cover file. saves copyright file as mod####.pdf to appropriate folder if self.execution("/html/body/div/div[3]/div[2]/div[2]/div/div[2]/div[4]/a/img", 20, "find") is False: self.driver.quit() self.stage = 0 self.luluCruise() bcImage = self.driver.find_element_by_xpath("/html/body/div/div[3]/div[2]/div[2]/div/div[2]/div[4]/a/img") location = bcImage.location size = bcImage.size print "taking screenshot" self.driver.save_screenshot("barcode.png") print "cropping screen screenshot" im = Image.open("barcode.png") left = location["x"] top = location["y"] right = location["x"] + size["width"] bottom = location["y"] + size["height"] im = im.crop((left, top, right, bottom)) # defines crop points im.save("barcode.png") # saves new cropped image imgWidth = (im.size[0]) * 0.4 imgHeight = (im.size[1]) * 0.4 # Using ReportLab to insert image into PDF imgTemp = StringIO() imgDoc = canvas.Canvas(imgTemp) # Draw image on Canvas and save PDF in buffer print "draw up new page and place barcode on there" imgPath = self.local.pwd + "/barcode.png" imgDoc.drawImage(imgPath, 300, 115, imgWidth, imgHeight) ## at (303,115) with size 160x160 imgDoc.save() # Use PyPDF to merge the image-PDF into the template page = PdfFileReader(file(self.local.cFolder + "/volume&&&" + self.volume.num + ".pdf", "rb")).getPage(0) overlay = PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0) page.mergePage(overlay) output = PdfFileWriter() output.addPage(page) output.write(file(self.local.cFolder + "/bcVolume&&&" + self.volume.num + ".pdf", "w")) print "cover is up" im = Image.open("barcode.png") im.save("barcode.png") # saves new cropped image imgWidth = (im.size[0]) * 0.4 imgHeight = (im.size[1]) * 0.4 # Using ReportLab to insert image into PDF imgTemp = StringIO() imgDoc = canvas.Canvas(imgTemp) # Draw image on Canvas and save PDF in buffer print "seek to page and place barcode before main" imgPath = "barcode.png" imgDoc.drawImage(imgPath, 46, 40, imgWidth, imgHeight) imgDoc.save() # Use PyPDF to merge the image-PDF into the template original = pypdf.PdfFileReader( file(self.local.inFolder + self.volume.round_folder + "/pre" + self.volume.num + ".pdf", "rb") ) page = original.getPage(1) overlay = pypdf.PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0) page.mergePage(overlay) # add all pages to a writer writer = pypdf.PdfFileWriter() for i in range(original.getNumPages()): page = original.getPage(i) writer.addPage(page) with open(self.local.inFolder + self.volume.round_folder + "/mod" + self.volume.num + ".pdf", "wb") as outFile: writer.write(outFile)
def generate_pdf(fname,lname,org): pWidth,pHeight = A4 imgDoc = canvas.Canvas(temp_tag_pdf, pagesize=portrait(A4)) imgDoc.setFillColor(grey) imgDoc.rect(0,0,pWidth,pHeight, fill=1) imgPath = img1path imgDoc.drawImage(imgPath,1.5*inch,6*inch,5.5*inch,4*inch) imgPath = img2path imgDoc.drawImage(imgPath,1.5*inch,1.5*inch,5.5*inch,4*inch) imgDoc.save() c = canvas.Canvas(temp_name_pdf) c.setFont('Helvetica', 20) text = fname + ' ' + lname text_width = stringWidth(text, 'Helvetica', 20) y = 7.3*inch pdf_text_object = c.beginText((pWidth - text_width) / 2.0, y) pdf_text_object.textOut(text) c.drawText(pdf_text_object) c.setFont('Helvetica', 15) text = org text_width = stringWidth(text, 'Helvetica', 15) y = 7*inch c.setFillColorRGB(0.5,0.5,0.5) pdf_text_object = c.beginText((pWidth - text_width) / 2.0, y) pdf_text_object.textOut(text) c.drawText(pdf_text_object) c.setFont('Helvetica', 45) text = fname text_width = stringWidth(text, 'Helvetica', 45) y = 3*inch c.setFillColorRGB(1,1,1) pdf_text_object = c.beginText(1.7*inch, y) pdf_text_object.textOut(text) c.drawText(pdf_text_object) c.setFont('Helvetica', 35) text = lname text_width = stringWidth(text, 'Helvetica', 35) y = 2.5*inch c.setFillColorRGB(1,1,1) pdf_text_object = c.beginText(1.7*inch, y) pdf_text_object.textOut(text) c.drawText(pdf_text_object) c.setFont('Helvetica', 15) text = org text_width = stringWidth(text, 'Helvetica', 15) y = 2*inch c.setFillColorRGB(0.5,0.5,0.5) pdf_text_object = c.beginText(1.7*inch, y) pdf_text_object.textOut(text) c.drawText(pdf_text_object) c.save() # Use PyPDF to merge the image-PDF into the template overlay = PdfFileReader(file(temp_name_pdf,"rb")).getPage(0) page = PdfFileReader(file(temp_tag_pdf,"rb")).getPage(0) page.mergePage(overlay) #Save the result output = PdfFileWriter() output.addPage(page) output.write(file(final_folder + fname.replace(" ","_") +'_' + lname.replace(" ","_") +".pdf","w"))
imgDoc2 = canvas.Canvas(imgTemp2) imgPath2 = "logo.jpg" imgDoc2.drawImage(imgPath2, 120, 693, 356, 71) ## at (399,760) with size 160x160 imgDoc2.save() if os.path.exists('Trump Bingo Cards'): pass else: os.makedirs('Trump Bingo Cards') for i in range(int(n)): print i # Use PyPDF to merge the image-PDF into the template page = PdfFileReader(file("bingo_cards.pdf", "rb")).getPage(i) overlay = PdfFileReader(StringIO(imgTemp1.getvalue())).getPage(0) page.mergePage(overlay) overlay = PdfFileReader(StringIO(imgTemp2.getvalue())).getPage(0) page.mergePage(overlay) #Save the result output = PdfFileWriter() output.addPage(page) output.write(file("Trump Bingo Cards/card%s.pdf" % i, "w")) # Sources: # http://matthiaseisen.com/articles/bingo/ # http://stackoverflow.com/questions/2925484/place-image-over-pdf
def applyBarcode(self): #gets the barcode from barcode page, saves it, applies it to both the copyright .pdf and the cover file. saves copyright file as mod####.pdf to appropriate folder if self.execution(self.bc_image,20,"find") is False: self.driver.quit() self.stage = 0 self.luluCruise() bcImage = self.driver.find_element_by_xpath(self.bc_image) location = bcImage.location size = bcImage.size print "taking screenshot" self.driver.save_screenshot('barcode.png') print "cropping screen screenshot" im = Image.open('barcode.png') left = location['x'] top = location['y'] right = location['x'] + size['width'] bottom = location['y'] + size['height'] im = im.crop((left, top, right, bottom)) # defines crop points im.save('barcode.png') # saves new cropped image imgWidth = (im.size[0])*.4 imgHeight = (im.size[1])*.4 # Using ReportLab to insert image into PDF imgTemp = StringIO() imgDoc = canvas.Canvas(imgTemp) # Draw image on Canvas and save PDF in buffer print "draw up new page and place barcode on there" imgPath = self.local.pwd+"/barcode.png" imgDoc.drawImage(imgPath, 300, 115, imgWidth, imgHeight) ## at (303,115) with size 160x160 imgDoc.save() # Use PyPDF to merge the image-PDF into the template page = PdfFileReader(file(self.local.cFolder+"/volume&&&"+self.volume.num+".pdf","rb")).getPage(0) overlay = PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0) page.mergePage(overlay) output = PdfFileWriter() output.addPage(page) output.write(file(self.local.cFolder+"/bcVolume&&&"+self.volume.num+".pdf","w")) print "cover is up" im = Image.open('barcode.png') im.save('barcode.png') # saves new cropped image imgWidth = (im.size[0])*.4 imgHeight = (im.size[1])*.4 # Using ReportLab to insert image into PDF imgTemp = StringIO() imgDoc = canvas.Canvas(imgTemp) # Draw image on Canvas and save PDF in buffer print "seek to page and place barcode before main" imgPath = "barcode.png" imgDoc.drawImage(imgPath, 46, 40, imgWidth, imgHeight) imgDoc.save() # Use PyPDF to merge the image-PDF into the template original = pypdf.PdfFileReader(file(self.local.inFolder+self.volume.round_folder+"/pre"+self.volume.num+".pdf","rb")) page = original.getPage(1) overlay = pypdf.PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0) page.mergePage(overlay) # add all pages to a writer writer = pypdf.PdfFileWriter() for i in range(original.getNumPages()): page = original.getPage(i) writer.addPage(page) with open(self.local.inFolder+self.volume.round_folder+"/mod"+self.volume.num+".pdf", "wb") as outFile: writer.write(outFile)