How can I reduce voltage from 40VDC to 5VDC?
The smallest method (only one power IC) would be using LM2576HV. It accepts maximum 60V as input (only the HV version).

Here is the schematic from datasheet: