how to save any online pdf line in local systeeem
save in private and attach link update private/file/filename.pdf etc any format
Try it, please!
If worked then use it.
def save_file_to_custom_location(file_link):
try:
# Check if the URL has a scheme specified
parsed_url = urlparse(file_link)
if not parsed_url.scheme:
# Prepend 'https://' to the URL
file_link = 'https://' + file_link
# Specify the custom location where you want to save the file
file_name = os.path.basename(file_link)
custom_location = os.path.join(frappe.get_site_path(), 'private', 'files', file_name)
print("----------------------------",custom_location)
# Download the file and save it to the custom location
response = requests.get(file_link)
print(response,"------------------------------------------------------")
if response.status_code == 200:
with open(custom_location, 'wb') as f:
f.write(response.content)
# Return success message with the path to the saved file
return {'status': 'success', 'message': 'File saved successfully.', 'file_path': custom_location}
else:
return {'status': 'error', 'message': 'Failed to download file.'}
except Exception as e:
# Log the error for debugging
frappe.logger().error(f"Error saving file: {str(e)}")
# Return error message if something goes wrong
return {'status': 'error', 'message': f'Error saving file: {str(e)}'}