How to use pikepdf - 10 common examples

To help you get started, we’ve selected a few pikepdf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jbarlow83 / OCRmyPDF / tests / test_graft.py View on Github external
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
        )
github pikepdf / pikepdf / tests / test_encrypt.py View on Github external
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
github jbarlow83 / OCRmyPDF / tests / test_ghostscript.py View on Github external
def linn(resources):
    path = resources / 'linn.pdf'
    return path, pikepdf.open(path)
github jbarlow83 / OCRmyPDF / tests / test_metadata.py View on Github external
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
github pikepdf / pikepdf / tests / test_pdf.py View on Github external
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()
github jbarlow83 / OCRmyPDF / tests / test_graft.py View on Github external
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
        )
github pikepdf / pikepdf / tests / test_encrypt.py View on Github external
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
github pikepdf / pikepdf / tests / test_pdf.py View on Github external
def test_open_pdf_no_password_but_needed(self, resources):
        with pytest.raises(PasswordError):
            Pdf.open(resources / 'graph-encrypted.pdf')
github pikepdf / pikepdf / tests / test_pdf.py View on Github external
def test_memory(self, resources):
        pdf = (resources / 'pal-1bit-trivial.pdf').read_bytes()
        with pytest.raises(Exception):
            pdf = Pdf.open(pdf)
github pikepdf / pikepdf / tests / test_io.py View on Github external
def sandwich(resources):
    # Has XMP, docinfo, , shorthand attribute XMP
    return Pdf.open(resources / 'sandwich.pdf')