Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def concatenate(n):
output_pdf = Pdf.new()
for i in range(n):
print(i)
pdf_page = Pdf.open(resources / 'pal.pdf')
output_pdf.pages.extend(pdf_page.pages)
output_pdf.save(outdir / '{}.pdf'.format(n))
def test_add_foreign_twice(graph, outpdf):
out = Pdf.new()
out.pages.append(out.copy_foreign(graph.pages[0]))
assert len(out.pages) == 1
out.pages.append(out.copy_foreign(graph.pages[0]))
assert len(out.pages) == 2
out.save(outpdf)
def _repeat_page(pdf_in, page, count, pdf_out):
for dup in range(count):
pdf_new = Pdf.new()
pdf_new.pages.append(pdf_in.pages[page])
pdf_out.pages.extend(pdf_new.pages)
return pdf_out
def test_stack_abuse():
p = pikepdf.Pdf.new()
stream = pikepdf.Stream(p, b'q ' * 35)
with pytest.warns(None) as record:
pdfinfo.info._interpret_contents(stream)
assert 'overflowed' in str(record[0].message)
stream = pikepdf.Stream(p, b'q Q Q Q Q')
with pytest.warns(None) as record:
pdfinfo.info._interpret_contents(stream)
assert 'underflowed' in str(record[0].message)
stream = pikepdf.Stream(p, b'q ' * 135)
with pytest.warns(None):
with pytest.raises(RuntimeError):
pdfinfo.info._interpret_contents(stream)
def test_create_form_xobjects(outdir):
pdf = Pdf.new()
font = pdf.make_indirect(
Object.parse(
b"""
<<
/Type /Font
/Subtype /Type1
/Name /F1
/BaseFont /Helvetica
/Encoding /WinAnsiEncoding
>>
"""
)
)
width, height = 100, 100
def test_repeat_using_intermediate(graph, outpdf):
def _repeat_page(pdf_in, page, count, pdf_out):
for dup in range(count):
pdf_new = Pdf.new()
pdf_new.pages.append(pdf_in.pages[page])
pdf_out.pages.extend(pdf_new.pages)
return pdf_out
with Pdf.new() as out:
_repeat_page(graph, 0, 3, out)
assert len(out.pages) == 3
out.save(outpdf)
def test_image_roundtrip(outdir, w, h, pixeldata, cs, bpc):
pdf = Pdf.new()
image_data = pixeldata * (w * h)
image = Stream(pdf, image_data)
image.Type = Name('/XObject')
image.Subtype = Name('/Image')
image.ColorSpace = Name(cs)
image.BitsPerComponent = bpc
image.Width = w
image.Height = h
xobj = {'/Im1': image}
resources = {'/XObject': xobj}
mediabox = [0, 0, 100, 100]
stream = b'q 100 0 0 100 0 0 cm /Im1 Do Q'
contents = Stream(pdf, stream)
def export(input_files, pages, file_out):
pdf_output = pikepdf.Pdf.new()
pdf_input = [pikepdf.open(p.copyname) for p in input_files]
for row in pages:
current_page = pdf_input[row[2] - 1].pages[row[3] - 1]
angle = row[6]
angle0 = current_page.Rotate if '/Rotate' in current_page else 0
if angle != 0:
current_page.Rotate = angle + angle0
cropped = _mediabox(row, angle, angle0, current_page.MediaBox)
if cropped:
current_page.MediaBox = cropped
pdf_output.pages.append(current_page)
with pdf_output.open_metadata() as outmeta:
outmeta.load_from_docinfo(pdf_input[0].docinfo)
for k, v in pdf_input[0].open_metadata().items():
if _pikepdf_meta_is_valid(v):
outmeta[k] = v