In August 1972, John Wojtowicz, 27, a married Brooklyn man and Vietnam vet with a stream of gay lovers on the side, decided to rob a bank to pay for his boyfriend’s sex change.

In the aftermath of the crime, a 14-hour hostage ordeal that riveted the nation, a character based on Wojtowicz would be played by Al Pacino in the 1975 film “Dog Day Afternoon,” which earned six Oscar nominations (winning Best Screenplay).

While Wojtowicz’s tale on film became the stuff of legend, the man himself remained little heard from until now, with a posthumous documentary, “The Dog,” hitting theaters on Friday.

The success of Pacino’s portrayal sprang from the hero/villain dichotomy of the character. As in the real-life robbery, which took place on Aug. 22, 1972, at a Chase Manhattan branch in Gravesend, Brooklyn, Wojtowicz got both his hostages and the many onlookers on his side, positioning himself as the little guy fighting against tyranny.

“The Dog,” which shows interviews with Wojtowicz from 2002 until his death four years later, proves his reality was more outlandish than any movie.

The night before the robbery, Wojtowicz and his accomplices — 18-year-old Sal Naturale and 20-year-old Bobby Westenberg — stayed in a New Jersey hotel. Wojtowicz had agreed to pay Westenberg $50,000 for his assistance. For that money, Wojtowicz wanted more than just a partner in crime.

“I grabbed ahold of Bobby Westenberg and I wanted to f - - k him, ’cause he used to dress up as a girl,” Wojtowicz says in the film.

“He goes . . . ‘I don’t want you f - - king me.’ I said, ‘I’m giving you $50,000, and you’re gonna tell me I’m not getting a f - - k out of it?’ . . . So then I f - - ked him.”

The self-described “pervert” met his wife, Carmen, at a bank where they both worked in the mid-1960s. Wojtowicz was drafted soon after and had his first homosexual experience during basic training. After Vietnam, Wojtowicz (still married to Carmen) joined the Gay Activists Alliance (GAA), but was driven more by a desire for sex than politics.

“I was a member of the entertainment committee, so I would meet and greet new gay people coming into the scene,” Wojtowicz said. “I could have sex with them quicker than anybody else, because they were just coming out.”

He was a disgrace. He would fall on a couch and start having sex in a semi-public place. - Randy Wicker

“He was considered a disgrace at GAA [dances]. He would fall on a couch and start having sex with somebody in a semi-public place,” Randy Wicker, a journalist who helped Wojtowicz negotiate the film rights to his story, tells The Post. “His reputation within GAA was, ‘This guy is a looney-tune.’ ”

Wojtowicz eventually left Carmen. In 1971, he met Ernie Aron, a transgender woman who went by the name Liz Eden. The two married in a non-binding ceremony that December.

Eden’s pals were not impressed.

“He was skeevy,” Jeremiah Newton, a longtime friend of Eden’s who appears in the film, tells The Post. “He was obsessed with sex . . . I thought he was pretty stupid.”

Over the following year, Eden talked about a sex change operation, which Wojtowicz was against. But after Eden tried to kill herself, Wojtowicz decided that the surgery was needed to save her life and hatched the plan to rob a bank.

As depicted in “Dog Day Afternoon,” the crime turned into a 14-hour circus that had over 2,000 onlookers on the scene rooting for Wojtowicz, who, at one point, threw money out to the crowd. Westenberg bailed before the crime got under way, Naturale was killed by the FBI and Wojtowicz wound up serving five years in prison.

Once he sold the film rights to his story, the money was used for Aron’s operation. But after the surgery in 1973, Aron — now Liz — told Wojtowicz that she never wanted to see him again. Wojtowicz slit his wrists, but survived.

He found love in prison, “marrying” fellow con George Heath — both got out in 1978 and moved in with Wojtowicz’s mother. Wojtowicz had the nerve to apply for a guard position at Chase Manhattan Bank. Instead, he found a job “cleaning toilet bowls on Park Avenue.” In the years to come, he would spend time in front of the bank signing autographs and wearing a T-shirt that read, “I Robbed This Bank.”

He died of cancer in 2006. While “Dog Day Afternoon” made him a legend, those who knew him say “The Dog” gives a truer picture of who Wojtowicz really was.

“They had no real understanding when they made [‘Dog Day Afternoon’] that John was as crazy as he was,” says Wicker. “He comes out more rational than he really was.”