[Python] Firebase Cloud Function Not Returning Response (Even After Successful Data Extraction)

Discussão em 'Python' iniciado por Stack, Abril 28, 2025.

  1. Stack

    Stack Membro Participativo

    Problem: I am building a Firebase Cloud Function in Python to extract text and links from a PDF file hosted at a URL. The function works correctly in extracting the data (I can see it printed in the logs), but when I return the response, the function doesn't return the expected result. The function seems to process the PDF correctly, but the response isn't being returned as expected.

    Here's my code

    from firebase_functions import https_fn
    from firebase_admin import initialize_app

    import requests
    import fitz # PyMuPDF
    from io import BytesIO

    from firebase_functions import https_fn, options
    import flask

    initialize_app()
    app = flask.Flask(__name__)

    @app.post("/extract")
    def extract():
    """
    Extracts text from a PDF file and returns it as a JSON response.
    """
    # Get the request data
    request_data = flask.request.get_json()

    # Check if the request data is valid
    if not request_data or 'url' not in request_data:
    return flask.jsonify({'error': 'Invalid request data'}), 400

    # Get the URL of the PDF file
    pdf_url = request_data['url']

    # Download the PDF file
    response = requests.get(pdf_url)
    pdf_bytes = response.content
    doc = fitz.open(stream=pdf_bytes, filetype="pdf")
    text = ""
    links = []
    for page in doc:
    text = page.get_text()
    print(text)

    links = page.get_links()
    for link in links:
    if link.get('uri'):
    links.append(link['uri'])
    print(link)
    response_data = {
    'text': text,
    'links': links
    }
    print(response_data)
    # Return the extracted text as a JSON response_data
    return flask.jsonify(response_data), 200


    @app.route('/__/health', methods=['GET'])
    def health_check():
    return "OK", 200

    @https_fn.on_request()
    def extractor(req: https_fn.Request) -> https_fn.Response:
    with app.request_context(req.environ):
    return app.full_dispatch_request()


    this is a screenshot [​IMG]

    print the results then postman keep loading until timeout reached

    Continue reading...

Compartilhe esta Página