Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_no_glyphless_graft(resources, outdir):
pdf = pikepdf.open(resources / 'francais.pdf')
pdf_aspect = pikepdf.open(resources / 'aspect.pdf')
pdf_cmyk = pikepdf.open(resources / 'cmyk.pdf')
pdf.pages.extend(pdf_aspect.pages)
pdf.pages.extend(pdf_cmyk.pages)
pdf.save(outdir / 'test.pdf')
with patch('ocrmypdf._graft.MAX_REPLACE_PAGES', 2):
ocrmypdf.ocr(
outdir / 'test.pdf', outdir / 'out.pdf', deskew=True, tesseract_timeout=0
)
def test_encrypt_info(trivial, outpdf):
trivial.save(outpdf, encryption=dict(R=4, owner='foo', user='bar'))
pdf = pikepdf.open(outpdf, password='foo')
assert pdf.encryption.user_password == b'bar'
assert pdf.encryption.bits == 128
def linn(resources):
path = resources / 'linn.pdf'
return path, pikepdf.open(path)
def test_preserve_metadata(spoof_tesseract_noop, output_type, resources, outpdf):
pdf_before = pikepdf.open(resources / 'graph.pdf')
output = check_ocrmypdf(
resources / 'graph.pdf',
outpdf,
'--output-type',
output_type,
env=spoof_tesseract_noop,
)
pdf_after = pikepdf.open(output)
for key in ('/Title', '/Author'):
assert pdf_before.docinfo[key] == pdf_after.docinfo[key]
pdfa_info = file_claims_pdfa(str(output))
assert pdfa_info['output'] == output_type
def test_with_block_abuse(resources):
with pikepdf.open(resources / 'pal-1bit-trivial.pdf') as pdf:
im0 = pdf.pages[0].Resources.XObject['/Im0']
with pytest.raises(PdfError):
im0.read_bytes()
def test_no_glyphless_graft(resources, outdir):
pdf = pikepdf.open(resources / 'francais.pdf')
pdf_aspect = pikepdf.open(resources / 'aspect.pdf')
pdf_cmyk = pikepdf.open(resources / 'cmyk.pdf')
pdf.pages.extend(pdf_aspect.pages)
pdf.pages.extend(pdf_cmyk.pages)
pdf.save(outdir / 'test.pdf')
with patch('ocrmypdf._graft.MAX_REPLACE_PAGES', 2):
ocrmypdf.ocr(
outdir / 'test.pdf', outdir / 'out.pdf', deskew=True, tesseract_timeout=0
)
def test_encrypt_basic(trivial, outpdf, R, owner, user):
trivial.save(outpdf, encryption=dict(R=R, owner=owner, user=user))
pdf_owner = pikepdf.open(outpdf, password=owner)
assert pdf_owner.is_encrypted
pdf_user = pikepdf.open(outpdf, password=user)
assert pdf_user.is_encrypted
def test_open_pdf_no_password_but_needed(self, resources):
with pytest.raises(PasswordError):
Pdf.open(resources / 'graph-encrypted.pdf')
def test_memory(self, resources):
pdf = (resources / 'pal-1bit-trivial.pdf').read_bytes()
with pytest.raises(Exception):
pdf = Pdf.open(pdf)
def sandwich(resources):
# Has XMP, docinfo, , shorthand attribute XMP
return Pdf.open(resources / 'sandwich.pdf')