本文整理汇总了Python中pyPdf.PdfFileWriter.addBlankPage方法的典型用法代码示例。如果您正苦于以下问题:Python PdfFileWriter.addBlankPage方法的具体用法?Python PdfFileWriter.addBlankPage怎么用?Python PdfFileWriter.addBlankPage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyPdf.PdfFileWriter
的用法示例。
在下文中一共展示了PdfFileWriter.addBlankPage方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __call__
# 需要导入模块: from pyPdf import PdfFileWriter [as 别名]
# 或者: from pyPdf.PdfFileWriter import addBlankPage [as 别名]
def __call__(self, data, attachments=[], pages=None):
self.rendered = {}
for field, ctx in self.fields.items():
if "template" not in ctx:
continue
self.context = ctx
kwargs = self.template_args(data)
template = self.context["template"]
try:
rendered_field = template.render(**kwargs)
except Exception as err:
logger.error("%s: %s %s", field, template, err)
else:
# Skip the field if it is already rendered by filter
if field not in self.rendered:
self.rendered[field] = rendered_field
filled = PdfFileReader(self.exec_pdftk(self.rendered))
for pagenumber, watermark in self.watermarks:
page = filled.getPage(pagenumber)
page.mergePage(watermark)
output = PdfFileWriter()
pages = pages or xrange(filled.getNumPages())
for p in pages:
output.addPage(filled.getPage(p))
for attachment in attachments:
output.addBlankPage().mergePage(attachment.pdf())
return output
示例2: _convert_pdf
# 需要导入模块: from pyPdf import PdfFileWriter [as 别名]
# 或者: from pyPdf.PdfFileWriter import addBlankPage [as 别名]
def _convert_pdf(self, pdf_data):
"""
Converts all pages of PDF in A4 format if communication is
printed.
:param pdf_data: binary data of original pdf
:return: binary data of converted pdf
"""
if self.send_mode != 'physical':
return pdf_data
pdf = PdfFileReader(BytesIO(base64.b64decode(pdf_data)))
convert = PdfFileWriter()
a4_width = 594.48
a4_height = 844.32 # A4 units in PyPDF
for i in xrange(0, pdf.numPages):
# translation coordinates
tx = 0
ty = 0
page = pdf.getPage(i)
corner = [float(x) for x in page.mediaBox.getUpperRight()]
if corner[0] > a4_width or corner[1] > a4_height:
page.scaleBy(max(a4_width / corner[0], a4_height / corner[1]))
elif corner[0] < a4_width or corner[1] < a4_height:
tx = (a4_width - corner[0]) / 2
ty = (a4_height - corner[1]) / 2
convert.addBlankPage(a4_width, a4_height)
convert.getPage(i).mergeTranslatedPage(page, tx, ty)
output_stream = BytesIO()
convert.write(output_stream)
output_stream.seek(0)
return base64.b64encode(output_stream.read())
示例3: reorder
# 需要导入模块: from pyPdf import PdfFileWriter [as 别名]
# 或者: from pyPdf.PdfFileWriter import addBlankPage [as 别名]
def reorder(outfile, inp, page_order):
dims = lambda: (inp.getPage(0).mediaBox.getWidth(), inp.getPage(0).mediaBox.getHeight())
output = PdfFileWriter()
for page_index in page_order:
if page_index:
output.addPage(inp.getPage(page_index-1))
else:
output.addBlankPage(*dims())
# output.addPage(blank_page())
write(outfile, output)
示例4: run
# 需要导入模块: from pyPdf import PdfFileWriter [as 别名]
# 或者: from pyPdf.PdfFileWriter import addBlankPage [as 别名]
def run(self):
def getSrcDim(srcPage):
return (float(srcPage.mediaBox.getWidth()),
float(srcPage.mediaBox.getHeight()))
def getDestDim():
if self.opts.orientation == const.PORTRAIT:
return self.opts.size
elif self.opts.orientation == const.LANDSCAPE:
return (self.opts.size[1], self.opts.size[0])
def getScale(srcPage):
destWidth, destHeight = getDestDim()
return (getSrcDim(srcPage)[const.WIDTH]/float(destWidth))
def getScaledDestDim(srcPage):
return [x * int(getScale(srcPage)) for x in getDestDim()]
reader = PdfFileReader(file(self.infile, "rb"))
writer = PdfFileWriter(
documentInfo=reader.getDocumentInfo(), authors=["Vimala"])
#self.opts.count
srcPage = reader.getPage(0)
height = getSrcDim(srcPage)[const.HEIGHT]
totalHeight = self.opts.count * height
destPage = writer.addBlankPage(*getScaledDestDim(srcPage))
print totalHeight
fitScale = getScaledDestDim(srcPage)[const.HEIGHT] / float(totalHeight)
print fitScale
srcPage.scale(fitScale, fitScale)
#scale = getScale(srcPage)
#srcPage.scale(scale, scale)
destPage.mergeTranslatedPage(srcPage, 0, height * 2 - .2 * height)
srcPage = reader.getPage(1)
srcPage.scale(fitScale, fitScale)
destPage.mergeTranslatedPage(srcPage, 0, height - .1 * height)
srcPage = reader.getPage(3)
srcPage.scale(fitScale, fitScale)
destPage.mergeTranslatedPage(srcPage, 0, 0)
#import pdb;pdb.set_trace()
writer.write(open(self.outfile, "wb"))
示例5: combine_pdf_pages
# 需要导入模块: from pyPdf import PdfFileWriter [as 别名]
# 或者: from pyPdf.PdfFileWriter import addBlankPage [as 别名]
def combine_pdf_pages(pdfpath, pagesgroups, verbose=False):
"""
Combines vertically groups of pages of a pdf file
@type pdfpath: str or unicode
@type pagesgroups: list of (list of int)
"""
# opening input file
if verbose:
print "Opening file " + pdfpath
fi = open(pdfpath, 'rb')
pdf = PdfFileReader(fi)
# opening output pdf
pdfout = PdfFileWriter()
# loop on groups of pages tom combine
for pagesgroup in pagesgroups:
if verbose:
print "Combining pages:",
# heights and widths
heights = [pdf.pages[i].mediaBox.getHeight() for i in pagesgroup]
widths = [pdf.pages[i].mediaBox.getWidth() for i in pagesgroup]
# adding new blank page
page_out = pdfout.addBlankPage(width=max(widths), height=sum(heights))
# merging pages of group
for i, p in enumerate(pagesgroup):
if verbose:
print p,
page_out.mergeTranslatedPage(pdf.pages[p], tx=0, ty=sum(heights[i+1:]))
print
# exporting merged pdf into temporary output file
fo = create_tmpfile('wb')
if verbose:
print "Exporting merged pdf in file {}".format(fo.name)
pdfout.write(fo)
# closing files
fi.close()
fo.close()
# removing original file and replacing it with merged pdf
if verbose:
print "Moving exported pdf to: " + pdfpath
os.remove(pdfpath)
os.rename(fo.name, pdfpath)
示例6: PdfFileWriter
# 需要导入模块: from pyPdf import PdfFileWriter [as 别名]
# 或者: from pyPdf.PdfFileWriter import addBlankPage [as 别名]
#!/usr/bin/env python
# requires PyPdf library, version 1.13 or above -
# its homepage is http://pybrary.net/pyPdf/
# runing: ./this-script-name file-with-pdf-list > output.pdf
import copy, sys
from pyPdf import PdfFileWriter, PdfFileReader
output = PdfFileWriter()
output_page_number = 0
# every new file should start on (n*alignment + 1)th page
# (with value 2 this means starting always on an odd page)
alignment = 2
listoffiles = open(sys.argv[1]).read().splitlines()
for filename in listoffiles:
# This code is executed for every file in turn
input = PdfFileReader(open(filename))
for p in [input.getPage(i) for i in range(0,input.getNumPages())]:
# This code is executed for every input page in turn
output.addPage(p)
output_page_number += 1
while output_page_number % alignment != 0:
output.addBlankPage()
output_page_number += 1
output.write(sys.stdout)
示例7: ProgressYay
# 需要导入模块: from pyPdf import PdfFileWriter [as 别名]
# 或者: from pyPdf.PdfFileWriter import addBlankPage [as 别名]
split_count = 0
pages = input_pdf.getNumPages()
progress = ProgressYay(total=pages)
for i in range(pages):
p = input_pdf.getPage(i)
x = p.mediaBox.getUpperRight_x()
y = p.mediaBox.getUpperRight_y()
if x > y:
# Landscape page, split it in 2
page1 = output_pdf.addBlankPage(x/2, y)
page1.mergePage(p)
page2 = output_pdf.addBlankPage(x/2, y)
page2.mergeTranslatedPage(p, -x/2, 0)
split_count = split_count + 1
else:
# Portrait, fine on its own
output_pdf.addPage(p)
progress.animate(amount=i)
progress.animate(amount=pages)
print 'Total pages: %d -> %d ' % (