Example #1
0
def test_list_apis():
    a = pikepdf.Array([1, 2, 3])
    a[1] = None
    assert a[1] is None
    assert len(a) == 3
    del a[1]
    assert len(a) == 2
    a[-1] = Name('/Foo')
    with pytest.raises(IndexError):
        a[-5555] = Name.Foo
    assert a == pikepdf.Array([1, Name.Foo])
    a.append(4)
    assert a == pikepdf.Array([1, Name.Foo, 4])
    a.extend([42, 666])
    assert a == pikepdf.Array([1, Name.Foo, 4, 42, 666])
Example #2
0
def attach_notebook(pdf_in, pdf_out, notebook):
    N = pikepdf.Name

    main_pdf = pikepdf.open(pdf_in)

    the_file = pikepdf.Stream(main_pdf, notebook["contents"])
    the_file[N("/Type")] = N("/EmbeddedFile")

    file_wrapper = pikepdf.Dictionary(F=the_file)

    fname = notebook["file_name"]
    embedded_file = pikepdf.Dictionary(Type=N("/Filespec"),
                                       UF=fname,
                                       F=fname,
                                       EF=file_wrapper)

    name_tree = pikepdf.Array([pikepdf.String(fname), embedded_file])

    embedded_files = pikepdf.Dictionary(Names=name_tree)

    names = pikepdf.Dictionary(EmbeddedFiles=embedded_files)

    main_pdf.Root[N("/Names")] = names

    main_pdf.save(pdf_out)
Example #3
0
def test_jbig2_global_palette(resources):
    xobj, _pdf = first_image_in(resources / 'jbig2global.pdf')
    xobj.ColorSpace = pikepdf.Array(
        [Name.Indexed, Name.DeviceRGB, 1, b'\x00\x00\x00\xff\xff\xff'])
    pim = PdfImage(xobj)
    im = pim.as_pil_image()
    assert im.size == (4000, 2864)
Example #4
0
def test_ccitt_icc(resources):
    xobj, pdf = first_image_in(resources / 'sandwich.pdf')

    pim = PdfImage(xobj)
    assert pim.icc is None
    bio = BytesIO()
    output_type = pim.extract_to(stream=bio)
    assert output_type == '.tif'
    bio.seek(0)
    assert b'GRAYXYZ' not in bio.read(1000)
    bio.seek(0)
    assert Image.open(bio)

    icc_data = (resources / 'Gray.icc').read_bytes()
    icc_stream = pdf.make_stream(icc_data)
    icc_stream.N = 1
    xobj.ColorSpace = pikepdf.Array([Name.ICCBased, icc_stream])

    pim = PdfImage(xobj)
    assert pim.icc.profile.xcolor_space == 'GRAY'
    bio = BytesIO()
    output_type = pim.extract_to(stream=bio)
    assert output_type == '.tif'
    bio.seek(0)
    assert b'GRAYXYZ' in bio.read(1000)
    bio.seek(0)
    assert Image.open(bio)
Example #5
0
def test_list_apis():
    a = pikepdf.Array([1, 2, 3])
    a[1] = None
    assert a[1] is None
    assert len(a) == 3
    del a[1]
    assert len(a) == 2
    a[-1] = Name('/Foo')
Example #6
0
def test_jbig2_global_palette(first_image_in):
    xobj, _pdf = first_image_in('jbig2global.pdf')
    xobj.ColorSpace = pikepdf.Array(
        [Name.Indexed, Name.DeviceRGB, 1, b'\x00\x00\x00\xff\xff\xff']
    )
    pim = PdfImage(xobj)
    im = pim.as_pil_image()
    assert im.size == (4000, 2864)
    assert im.getpixel((0, 0)) == 255  # Ensure loaded
Example #7
0
def test_list_apis():
    a = pikepdf.Array([1, 2, 3])
    a[1] = None
    assert a[1] is None
    assert len(a) == 3
    del a[1]
    assert len(a) == 2
    a[-1] = Name('/Foo')
    with pytest.raises(IndexError):
        a[-5555] = Name.Foo
    assert a == pikepdf.Array([1, Name.Foo])
    a.append(4)
    assert a == pikepdf.Array([1, Name.Foo, 4])
    a.extend([42, 666])
    assert a == pikepdf.Array([1, Name.Foo, 4, 42, 666])
    with pytest.raises(ValueError, match='object is not a dictionary'):
        del a.ImaginaryKey
    with pytest.raises(TypeError, match=r"items\(\) not available"):
        a.items()
Example #8
0
def test_list_apis():
    a = pikepdf.Array([1, 2, 3])
    a[1] = None
    assert a[1] is None
    assert len(a) == 3
    del a[1]
    assert len(a) == 2
    a[-1] = Name('/Foo')
    with pytest.raises(IndexError):
        a[-5555] = Name.Foo
Example #9
0
    def test_array_contains(self):
        a = pikepdf.Array([Name.One, Name.Two])
        assert Name.One in a
        assert Name.Two in a
        assert Name.N not in a

        a = pikepdf.Array([1, 2, 3])
        assert 1 in a
        assert 3 in a
        assert 42 not in a

        with pytest.raises(TypeError):
            assert 'forty two' not in a
        with pytest.raises(TypeError):
            assert b'forty two' not in a
        assert pikepdf.String('forty two') not in a

        a = pikepdf.Array(['1234', b'\x80\x81\x82'])
        assert pikepdf.String('1234') in a
        assert pikepdf.String(b'\x80\x81\x82') in a
Example #10
0
def test_not_convertible():
    class PurePythonObj:
        def __repr__(self):
            return 'PurePythonObj()'

    c = PurePythonObj()
    with pytest.raises(RuntimeError):
        encode(c)
    with pytest.raises(RuntimeError):
        pikepdf.Array([1, 2, c])

    d = pikepdf.Dictionary()
    with pytest.raises(RuntimeError):
        d.SomeKey = c
Example #11
0
def test_filespec_types(pal, resources):
    some_bytes = b'just some bytes'
    some_path = resources / 'rle.pdf'
    assert isinstance(some_path, Path)

    fs_bytes = AttachedFileSpec(pal, some_bytes)
    assert fs_bytes.get_file().read_bytes() == some_bytes
    assert fs_bytes.filename == ''

    fs_path = AttachedFileSpec.from_filepath(pal, some_path)
    assert fs_path.get_file().read_bytes() == some_path.read_bytes()

    with pytest.raises(TypeError):
        fs_path.get_file(pikepdf.Array([1]))
Example #12
0
def test_invalid_toc(resources, outdir, caplog):
    pdf = pikepdf.open(resources / 'toc.pdf')

    # Corrupt a TOC entry
    pdf.Root.Outlines.Last.Dest = pikepdf.Array([None, 0.0, 0.1, 0.2])
    pdf.save(outdir / 'test.pdf')

    pdf = pikepdf.open(outdir / 'test.pdf')
    remap = {}
    remap[pdf.pages[0].objgen] = pdf.pages[0].objgen  # Dummy remap

    # Confirm we complain about the TOC and don't throw an exception
    log = logging.getLogger()
    _fix_toc(pdf, remap, log)
    assert 'invalid table of contents entries' in caplog.text
Example #13
0
def test_page_boxes(graph_page):
    page = graph_page
    assert page.mediabox == page.cropbox == page.trimbox
    page.cropbox = [0, 0, page.mediabox[2] - 100, page.mediabox[3] - 100]
    page.mediabox = [
        page.mediabox[0] - 50,
        page.mediabox[1] - 50,
        page.mediabox[2] + 50,
        page.mediabox[3] + 50,
    ]
    page.trimbox = [50, 50, page.mediabox[2] - 50, page.mediabox[3] - 50]

    assert page.mediabox != page.cropbox
    assert page.cropbox != page.mediabox

    page.cropbox = pikepdf.Array([0, 0, 50, 50])
Example #14
0
    def content_stream(self):
        new_content_stream = []
        for operands, operator in self.original_content_stream:
            new_operands = operands[:]

            if operator == pikepdf.Operator('Tj'):
                new_operands = self._get_objects(operands)
            elif operator == pikepdf.Operator('TJ'):
                new_operands = []
                for tja in operands:
                    if _is_array_op(tja):
                        objects = self._get_objects(tja)
                        new_operands.append(pikepdf.Array(objects))
                    else:
                        new_operands.append(tja)

            new_content_stream.append((new_operands, operator))
        return new_content_stream
    def filter_ocg_order(self, input):
        if input._type_name == 'array':
            # create a copy of the array and filter the items
            output = pikepdf.Array([])
            for item in input:
                f_item = self.filter_ocg_order(item)
                if f_item is not None:
                    output.append(f_item)

            return output

        elif input._type_name == 'dictionary':
            if '/Type' in input.keys():
                if input.Type == pikepdf.Name.OCG:
                    # found the OCG entry, delete it if it's not in our keep array
                    if any([input.Name == oc for oc in self.keep_ocs]):
                        return input
                    else:
                        return None
        else:
            return input
def update_dest(zoom_factor,current):
    dest = pikepdf.Array()
    dest.append(current[0])
    dest.append(pikepdf.Name("/XYZ"))
    dest.append(0)
    dest.append(0)
    dest.append(zoom_factor)
    dest_type = current[1]
    
    if dest_type == "/XYZ":
        dest[2] = current[2]
        dest[3] = current[3]
    elif dest_type in ("/FitH","/FitBH"):            
        dest[3] = current[2]          
    elif dest_type in ("/FitV","/FitBV"):            
        dest[2] = current[2]          
    elif dest_type == "/FitR":
        dest[2] = current[2]
        dest[3] = current[4]
    #("/Fit","/FitB"):
    
    return dest    
Example #17
0
 def test_wrong_contains_type(self):
     d = pikepdf.Dictionary()
     with pytest.raises(TypeError, match="can only contain Names"):
         pikepdf.Array([3]) in d
Example #18
0
def test_nested_list2(array):
    assume(isinstance(array, list))
    a = pikepdf.Array(array)
    assert a == array
Example #19
0
def test_nested_list(array):
    a = pikepdf.Array(array)
    assert a == array
Example #20
0
def test_issue_100(trivial):
    with trivial.open_metadata() as m:
        m.load_from_docinfo({'/AAPL:Example': pikepdf.Array([42])})
Example #21
0
def test_list(array):
    a = pikepdf.Array(array)
    assert decode_encode(a) == a
Example #22
0
def test_nested_list2(array):
    assume(isinstance(array, list))
    a = pikepdf.Array(array)
    assert decode_encode(a) == a
Example #23
0
 def test_list(self, array):
     a = pikepdf.Array(array)
     assert a == array
Example #24
0
def test_issue_100(trivial):
    with trivial.open_metadata() as m, pytest.warns(UserWarning,
                                                    match="no XMP equivalent"):
        m.load_from_docinfo({'/AAPL:Example': pikepdf.Array([42])})