Literally, 死前 means before death while 生前 means before life. They seem to bear the opposite meaning. However, in most cases, they actually mean the same thing, i.e., when someone was still alive. What's more, in my experience, 生前 is being used more often than 死前. Any difference between them and when to use which?
Yes, you are quite right that they both mean when someone was still alive. However, there is a clear distinction on the usage of the two words. 生前 is almost always used when someone is discussing about the life of a deceased. Example:
He did a lot of charity work when he was alive.
死前, or more properly 临死之前, is almost always used when someone is discussing about a person prior to his death. Example:
What did he said to you before he die?
They mean differently.
when you use this word, the person you are talking about is dead and you are referring to the time(a long period) or the consistent behavior when he was alive.
Since in general, you can't talk to a dead man.
From this sentence, the listener knows he(他) has died.
This word refers to the time( a short/specified period) just before one's death. In this case, that person could be alive, or could have died, so you could say
Do you have any words to say before your death?
This sentence could happen when a prisoner is going to be executed.
Here is a more complex sentence with both of the words.
He always announced that he would donate all of his wealth to the charity organizations, but he changed his mind one week before the death.
生前 means a time span between birth and death.
死前 means a specific time point at which someone is about to die.