Gerald Klein, along with family and friends, celebrated with wide smiles and hugs in a courtroom Friday after learning the 81-year-old former pain clinic doctor no longer faces the prospect of going to prison.

“I’m relieved and satisfied with everything,” Klein said minutes after he was sentenced to four years of probation for a felony drug conviction from a trial last fall.

Circuit Judge Karen Miller — the target of unsuccessful efforts by Klein’s lawyers to have her disqualified from the case because of alleged bias — showed him mercy. She denied the prosecutor’s request for a six-month jail sentence as part of a recommended five years of probation.

Instead, Miller imposed a punishment of 13 days in Palm Beach County Jail, with Klein getting credit for already having served that time. He also must perform 240 hours of community service, the judge said in a courtroom packed with more than 100 Klein supporters.

But Klein, who has high blood pressure, heart and prostate problems, may apply later to reduce the length of his probation to three years.

Six months ago, a jury found Klein not guilty of first-degree murder and nine drug charges filed by prosecutors in 2011 as part of a high-profile case against so

The jury convicted Klein of one charge concerning a 2009 prescription he wrote for the anti-anxiety drug Xanax to a patient at the since-closed East Coast Pain Clinic in West Palm Beach.

The patient? Alejandro Pino, then a chef for business tycoon and current GOP presidential front-runner Donald Trump.

Before hearing his sentence, Klein apologized for his mistake. He asked the judge to consider his work as a respected pediatric surgeon in New Jersey, military service as a U.S. Army captain who operated on injured Vietnam War soldiers, and deep devotion to his family.

“Your honor, I’m truly sorry,” the grandfather of four told Miller. “In retrospect, I made a poor decision in working at that clinic. As much as I’d like to rewind the tape, I cannot. But I express my feelings of remorse and regret.”

Under state law, a conviction for the charge of sale of alprazolam is punishable by up to five years in prison.

But attorney Sam Rabin pleaded for “straight probation” and no time behind bars because of his client’s advanced age, health problems, distinguished career in medicine, and need to care for his wife, a cancer survivor.

Rabin also pointed to statistics from the Florida Department of Corrections to boost his argument. The stats show that in the past two years, probation sentences were given to 85 to 88 percent of defendants from Palm Beach County ranked similarly to Klein under the state’s sentencing guidelines.

“This is a good man who may have made a mistake in judgment,” Rabin said, adding that no minimum jail time was required under the law. “He’s done a lot of good in his life.”

Wendy Klein testified her father worked tirelessly, first in the Army and later in a medical practice devoted to children.

“He used his surgical talents to save many lives,” she said. “He’s not just my hero. He’s a hero to many others as well.”

Assistant State Attorney John Parnofiello agreed Klein is a “good person” but noted that “good people can make bad decisions.”

“This doctor issued prescriptions in bad faith,” the prosecutor said.

Klein worked part-time at East Coast Pain Clinic from the time it opened in 2008 until federal law enforcement officials shut it down two years later.

Klein was indicted in 2011, blamed for the Feb. 28, 2009, overdose death of Joseph Bartolucci, 24, of West Palm Beach, along with charges of trafficking in the painkillers oxycodone and hydromorphone. The case was one of the rare times a doctor in Florida has been tried for murder in the death of a pill-taking patient.

Prosecutors told the jury Klein killed Bartolucci, explaining that the aspiring paramedic died one day after taking a dangerous mix of the narcotic hydromorphone, under the name Dilaudid, and Xanax, prescribed by Klein.

But Rabin argued the doctor was guilty of nothing more than “bad handwriting” for keeping some messy patient records.

Klein wasn’t involved in an illegal conspiracy with clinic owner Jeff George and business manager Theodore Obermeyer, and the evidence showed Klein tried to wean patients from highly addictive painkillers, Rabin told the jury.

After five hours of deliberations, the jurors agreed Klein was not guilty of all of the charges except for the least serious count in the indictment. Friday’s sentencing ended Klein’s house arrest and criminal ordeal after 41/2 years.