A general method is presented to calculate absolute binding energies of core levels in metals and insulators, based on a penalty functional and an exact Coulomb cutoff method in the framework of density functional theory. The spurious interaction of core holes between supercells is avoided by the exact Coulomb cutoff method, while the variational penalty functional enables us to treat multiple splittings due to chemical shift, spin-orbit coupling, and exchange interaction on equal footing, both of which are not accessible by previous methods. It is demonstrated that the absolute binding energies of core levels for both metals and insulators are calculated by the proposed method in a mean absolute (relative) error of 0.4 eV (0.16%) for eight cases compared to experimental values measured with x-ray photoemission spectroscopy within a generalized gradient approximation to the exchange-correlation functional.