Try using stream = True in the below section to download the file directly instead of it going into memory first.
req = urllib.request.Request(url, headers=headers) with urllib.request.urlopen(req) as response: pdf_data = response.read()