def encode_stream(stream, stream_def):
    """Apply all specified filters in order to encode stream."""
    if '/Filter' not in stream_def:
        return stream
    filters = stream_def['/Filter']
    if type(filters) == str:
        filters = [filters]
    res = stream
    for _, f in enumerate(filters):
        if f == '/FlateDecode':
            try:
                res = zlib.compress(res)
            except:
                return b'#PDFSyntaxException: cannot encode Flate'
        elif f == '/ASCIIHexDecode':
            try:
                res = asciihex(res)
            except:
                return b'#PDFSyntaxException: cannot encode ASCIIHex'
        elif f == '/ASCII85Decode':
            try:
                res = base64.a85encode(res, adobe=True)
            except:
                return b'#PDFSyntaxException: cannot encode ASCII85'
        else:
            return b'#PDFSyntaxException: unsupported filter'
    return res
Apply all specified filters in order to encode stream.
pdfsyntax/filters.py