How can I print an external .png file offered by URL only

Greetings,

I’m working on integrating EasyPost shipping with ERPNext. There are a few ways EasyPost offers shipping labels. I think the most flexible (for printing purposes) is a .png file offered as a URL. When I use the Tools > print shipping label, it opens a .png file URL (which is hosted on Amazon S3 bucket from EasyPost’s Amazon account) in a new tab. Unfortunately, the new tab displays the .png file but without any controls/buttons to download or print the file. The user would need to print the web page via the browser’s file menu, which is not ideal.

I’m looking for solutions on how to print this file using the provided URL.
Should I use something like CURL or WGET to download the file to the server, then try to print? This doesn’t seem ideal as there will be maintenance needed to discard old files.

Is there a way to use the Frappe print preview with a file located on another server like Amazon s3?

It seems .png files are generally problematic. I even tried adding the .png file as an upload to the files app. The files app will show a preview of the file, but if I choose to download it simply opens the file in a new tab of the browser with no option to print or download. Is this a limitation of .png?
Of course if I try to print via the files app it will print the doctype, not the file contents.

Thanks in advance for taking the time.

I was able to work this out (with the help of ChatGPT) by creating server script and client script to save the file to /tmp, import cups, and then run a custom print command. I was not able to use print_by_server because it expects a doctype as an argument.

def print_label_from_url(label_url: str, printer_setting: str):
    """
    Downloads a file from a URL and sends it to the configured network printer.
    """
    import cups

    # Fetch printer settings
    print_settings = frappe.get_doc("Network Printer Settings", printer_setting)

    try:
        # Download the label from the URL
        response = requests.get(label_url)
        response.raise_for_status()

        # Save the label locally
        file_name = os.path.basename(label_url)
        local_path = os.path.join("/tmp", file_name)
        with open(local_path, "wb") as f:
            f.write(response.content)

        # Set up CUPS connection
        cups.setServer(print_settings.server_ip)
        cups.setPort(print_settings.port)
        conn = cups.Connection()

        # Print the file
        conn.printFile(print_settings.printer_name, local_path, "Shipping Label", {})

        # Clean up the temporary file
        os.remove(local_path)

    except requests.exceptions.RequestException as e:
        frappe.throw(_("Failed to download the label: {0}").format(e))
    except cups.IPPError as e:
        frappe.throw(_("Printing failed due to CUPS error: {0}").format(e))
    except Exception as e:
        frappe.throw(_("An error occurred while printing: {0}").format(e))