Search code examples
apache-sparkdatabricksazure-cognitive-services

Extract data from PDFs at scale with form recognizer: HttpResponseError: (FailedToDownloadImage) Failed to download image from input URL on Databricks


I am trying to extract data from pdfs at scale with Azure Form Recognizer. I am using the code example at github

I have entered the code as follows:

import pandas as pd

field_list = ["InvoiceId", "VendorName", "VendorAddress", "CustomerName", "CustomerAddress", "CustomerAddressRecipient", "InvoiceDate", "InvoiceTotal", "DueDate"]
df = pd.DataFrame(columns=field_list)

for blob in container.list_blobs():
  blob_url = container_url + "/" + blob.name
  poller = form_recognizer_client.begin_recognize_invoices_from_url(invoice_url=blob_url)
  invoices = poller.result()
  print("Scanning " + blob.name + "...")
  
  for idx, invoice in enumerate(invoices):
      single_df = pd.DataFrame(columns=field_list)

      for field in field_list:
        entry = invoice.fields.get(field)
        
        if entry:
          single_df[field] = [entry.value]
          
      single_df['FileName'] = blob.name
      df = df.append(single_df)

df = df.reset_index(drop=True)
df

However, I keep on getting the following error:

HttpResponseError: (FailedToDownloadImage) Failed to download image from input URL.

My URL looks like the following:

https://blobpretbiukblbdev.blob.core.windows.net/demo?sp=racwdl&st=2022-05-21T19:39:07Z&se=2022-05-22T03:39:07Z&sv=2020-08-04&sr=c&sig=XYhdecG2jKF8aNPPpkcP%2FCGVVRKYTFPrOQYdNDsASCA%3D/pdf1.pdf

NB: The key has been regenerated, I have just left the key in as it will appear in my code for illustration.

Where might I be going wrong?


Solution

  • As mentioned in REST API supportive documentation, there is a need to specify the Content-Type. There is a need to set the public access to source via JSON file. Set the Content-Type to application/pdf. To make this work, there is a need to install filetype package using link

    pip install filetype
    

    Check this link for better implementation of REST API to user Form Recognizer SDK.