24 V DC to 5 V DC converter for Pi 3B
Having it shut on and off with the printer  may be a bad idea- corrupted file systems and so on will likely haunt you unless you manually do an orderly shutdown every time.
But you could use a DC-DC converter to get 5V from 24V for the Raspberry Pi. There may well not be enough excess capacity in the 3D printer power supply to provide an extra 15W or so.
Depending on the model they draw something like 2A or more at 5V. My two-tentacled Octopi setup has a 2.5A adapter. If you exceed the maximum draw from the 24V supply you may get bad prints, resets or possibly even damage the power supply.
Or you could use a small 24V-coil relay to switch the 5V from your existing adapter (generally safer, provided you do a manual orderly shutdown every time).
As an alternative, you could leave the Rpi powered all the time and remotely control the printer power using a relay module and an appropriate plug-in for Octopi.