General approach to perfect and broadband absorption in thin films