How to use the pikepdf.PdfError function in pikepdf

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 pikepdf / pikepdf / tests / test_filters.py View on Github external
def test_tokenfilter_is_abstract(pal):
    page = pikepdf.Page(pal.pages[0])
    try:
        result = page.get_filtered_contents(pikepdf.TokenFilter())
    except pikepdf.PdfError:
        assert 'Tried to call pure virtual' in pal.get_warnings()[0]
github pikepdf / pikepdf / tests / test_private_pdfs.py View on Github external
def test_pypdf2_issue_361(private):
    with gzip.open(str(private / 'pypdf2_issue_361.pdf.gz'), 'rb') as gz:
        with pytest.raises(PdfError, match=r'trailer'):
            Pdf.open(gz)
github pikepdf / pikepdf / tests / test_image_access.py View on Github external
def test_lowlevel_jpeg(congress):
    raw_bytes = congress[0].read_raw_bytes()
    with pytest.raises(PdfError):
        congress[0].read_bytes()

    im = Image.open(BytesIO(raw_bytes))
    assert im.format == 'JPEG'

    pim = PdfImage(congress[0])
    b = BytesIO()
    pim.extract_to(stream=b)
    b.seek(0)
    im = Image.open(b)
    assert im.size == (congress[0].Width, congress[0].Height)
    assert im.mode == 'RGB'
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 pikepdf / pikepdf / tests / test_object.py View on Github external
d = Decimal(f)
    if isfinite(f) and d.is_finite():
        try:
            # PDF is limited to ~5 sig figs
            decstr = str(d.quantize(Decimal('1.000000')))
        except InvalidOperation:
            return  # PDF doesn't support exponential notation
        try:
            py_d = Object.parse(decstr)
        except RuntimeError as e:
            if 'overflow' in str(e) or 'underflow' in str(e):
                py_d = Object.parse(str(f))

        assert isclose(py_d, d, abs_tol=1e-5), (d, f.hex())
    else:
        with pytest.raises(PdfError, message=repr(f)):
            Object.parse(str(d))
github pikepdf / pikepdf / tests / test_io.py View on Github external
        (NegativeOneBytesIO, PdfError),
    ],
)
def test_invalid_output_stream(sandwich, bio_class, exc_type):
    bio = bio_class()
    with pytest.raises(exc_type):
        sandwich.save(bio, static_id=True)
github jbarlow83 / OCRmyPDF / src / ocrmypdf / _pipeline.py View on Github external
def get_pdfinfo(input_file, detailed_page_analysis=False, progbar=False):
    try:
        return PdfInfo(
            input_file, detailed_page_analysis=detailed_page_analysis, progbar=progbar
        )
    except pikepdf.PasswordError:
        raise EncryptedPdfError()
    except pikepdf.PdfError:
        raise InputFileError()
github jbarlow83 / OCRmyPDF / src / ocrmypdf / _pipeline_simple.py View on Github external
def get_pdfinfo(input_file, detailed_page_analysis=False):
    try:
        return PdfInfo(
            input_file, detailed_page_analysis=detailed_page_analysis
        )
    except pikepdf.PasswordError:
        raise EncryptedPdfError()
    except pikepdf.PdfError:
        raise InputFileError()
github jbarlow83 / OCRmyPDF / src / ocrmypdf / _graft.py View on Github external
possible_font_names = ('/f-0-0', '/F1')
    try:
        with pikepdf.open(text) as pdf_text:
            try:
                pdf_text_fonts = pdf_text.pages[0].Resources.get('/Font', {})
            except (AttributeError, IndexError, KeyError):
                return None, None
            for f in possible_font_names:
                pdf_text_font = pdf_text_fonts.get(f, None)
                if pdf_text_font is not None:
                    font_key = f
                    break
            if pdf_text_font:
                font = pdf_base.copy_foreign(pdf_text_font)
            return font, font_key
    except (FileNotFoundError, pikepdf.PdfError):
        # PdfError occurs if a 0-length file is written e.g. due to OCR timeout
        return None, None
github pikepdf / pikepdf / src / pikepdf / models / __init__.py View on Github external
raise TypeError("stream must a PDF object")

    if (
        page_or_stream._type_code != ObjectType.stream
        and page_or_stream.get('/Type') != '/Page'
    ):
        raise TypeError("parse_content_stream called on page or stream object")

    try:
        if page_or_stream.get('/Type') == '/Page':
            page = page_or_stream
            instructions = page._parse_page_contents_grouped(operators)
        else:
            stream = page_or_stream
            instructions = Object._parse_stream_grouped(stream, operators)
    except PdfError as e:
        # This is the error message for qpdf >= 7.0. It was different in 6.x
        # but we no longer support 6.x
        if 'ignoring non-stream while parsing' in str(e):
            raise TypeError("parse_content_stream called on non-stream Object")
        raise e from e

    return instructions