Download invoice as PDF with Linode API?

Linode Staff

Is it possible to download an invoice as a PDF file using the Linode API?

1 Reply

While I could not find a way to do this directly in the API, you can use a script, such as the one below, to download an invoice as a PDF. This script is untested, but should work on a Linux machine with wkhtmltopdf installed on it (the script will install it if it isn't already present). From what I can tell, this requires a GUI, and wouldn't directly run on any of my Linodes, so you may need to run it on a Linux desktop. Alternatively, you can navigate to the URL in the script (replace ${invoice_id} with your invoice ID) to download an invoice as a PDF file.

DISCLAIMER: I wasn't able to fully test this, as it appears to require a GUI, and I do not have a compatible GUI handy for testing - you may need to use a tool other than wkhtmltopdf:

#!/bin/bash

# Download Linode invoice and convert it to a PDF file
# The system_detect_distro() code was copied from another project of mine
# You can remove any unreferenced values

function system_detect_distro {
    # Decide if the system is Debian or RedHat based

    # ${1} - required - Which value to echo back to the calling function
    if [ ! -n "${1}" ]; then
        printf "system_detect_distro() requires which value to be"
        printf " returned as it's only argument\n"
        return 1
    fi
    local DISTRO="$(grep "^ID=" /etc/os-release | cut -d= -f2)"
    DISTRO="$(sed -e 's/^"//' -e 's/"$//' <<<"${DISTRO}")"
    local VERSION="$(grep "^VERSION_ID=" /etc/os-release | cut -d= -f2)"
    VERSION="$(sed -e 's/^"//' -e 's/"$//' <<<"${VERSION}")"
    [ -f /etc/debian_version ] && local -r FAMILY='debian'
    [ -f /etc/redhat-release ] && local -r FAMILY='redhat'


    # Determine what the calling function wants
    if [ "${1}" == 'distro' ]; then 
        echo "${DISTRO}"
    elif [ "${1}" == 'family' ]; then
        echo "${FAMILY}"
    elif [ "${1}" == 'version' ]; then
        echo "${VERSION}"
    else
        printf "This does not appear to be a supported distribution\n"
        return 1
    fi
}

# Store detected distribution information
declare DIST="$(system_detect_distro 'distro')"
declare FAM="$(system_detect_distro 'family')"
declare -A DETECTED_DISTRO="(
    [distro]="${DIST,,}" \
    [family]="${FAM,,}" \
    [version]="$(system_detect_distro 'version')"
)"

if [ ! -x /usr/bin/wkhtmltopdf ]; then
    if [[ "${DETECTED_DISTRO['family']}" == 'redhat' ]]; then 
        yum -y install wkhtmltopdf
    elif [[ "${DETECTED_DISTRO['family']}" == 'debian' ]]; then
        apt-get -y install wkhtmltopdf
    fi
}

declare TOKEN="YOUR_LINODE_API_TOKEN_GOES_HERE"
declare invoice_id=$(curl -H "Authorization: Bearer ${TOKEN}" \
    https://api.linode.com/v4/account/invoices)

wget -O- https://manager.linode.com/account/invoicepdf/"${invoice_id}" > \
    "${invoice_id}"
wkhtmltopdf "${invoice_id}".html "${invoice_id}".pdf

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct