Generally speaking, if you go to a doctor and get a sick note (Arbeitsunfähigkeitsbescheinigung) then you are unfit for work. Matter of fact the name in German already says you are unfit to work. It does not pass any judgement as to why.
It's never illegal to go to a doctor and get a sick note.
In rare cases, if it can be proven that you did it on purpose or with gross negligence, the people who paid for your sick leave (in this case probably your Krankenversicherung, in case of an accident the gesetzliche Unfallversicherung) can refuse to pay or demand their already paid money back. Please note that those are serious cases. Like hitting a singsign-post head-on with your car while speeding under the influence of alcohol on your way to work. That can be ruled as self-inflicted or at least gross negligence and the insurance can refuse to pay. It's still not illegal to take sick leave because the truth is that the person is not fit for work after colliding with the sign post (the DUI is another matter). But it's basically unpaid and that's costly.
Sick leave is also a protected state, you cannot be fired for being sick, not even in the above case of the DUI (although you might be fired for having a DUI on record if your job is being a driver at the company). A company might find a few phony other reasons to fire people that they think had too much sick leave though or if the person is a contract worker just not renew the contract when it runs out.
So yes, you can eat that pizza again. It's not even proven that that pizza made you sick. Now from the perspective of your boss, after the third or fourth time you have to go home sick after eating there, I would seriously question your personal judgement. But making bad decisions is not illegal.