本文整理汇总了Python中pyPdf.PdfFileReader.mergePage方法的典型用法代码示例。如果您正苦于以下问题:Python PdfFileReader.mergePage方法的具体用法?Python PdfFileReader.mergePage怎么用?Python PdfFileReader.mergePage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyPdf.PdfFileReader
的用法示例。
在下文中一共展示了PdfFileReader.mergePage方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: showpdf
# 需要导入模块: from pyPdf import PdfFileReader [as 别名]
# 或者: from pyPdf.PdfFileReader import mergePage [as 别名]
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
示例2: assembleBooklet
# 需要导入模块: from pyPdf import PdfFileReader [as 别名]
# 或者: from pyPdf.PdfFileReader import mergePage [as 别名]
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()
示例3: build_ticket
# 需要导入模块: from pyPdf import PdfFileReader [as 别名]
# 或者: from pyPdf.PdfFileReader import mergePage [as 别名]
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
示例4: applyBarcode
# 需要导入模块: from pyPdf import PdfFileReader [as 别名]
# 或者: from pyPdf.PdfFileReader import mergePage [as 别名]
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 :)"
示例5: placeBarcode
# 需要导入模块: from pyPdf import PdfFileReader [as 别名]
# 或者: from pyPdf.PdfFileReader import mergePage [as 别名]
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"))
示例6: StringIO
# 需要导入模块: from pyPdf import PdfFileReader [as 别名]
# 或者: from pyPdf.PdfFileReader import mergePage [as 别名]
#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"))
示例7: applyBarcode
# 需要导入模块: from pyPdf import PdfFileReader [as 别名]
# 或者: from pyPdf.PdfFileReader import mergePage [as 别名]
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)
示例8: generate_pdf
# 需要导入模块: from pyPdf import PdfFileReader [as 别名]
# 或者: from pyPdf.PdfFileReader import mergePage [as 别名]
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"))