A developer with ties to Mayor de Blasio won the right to transform a Brooklyn library into a residential skyscraper despite being outbid by at least two other builders, multiple sources told The Post.

Hudson Companies bid $52 million for development rights to the Brooklyn Heights Library, $6 million and $1 million less than two other bidders.

The bids of 11 other developers who lost out on the lucrative project have never been publicly revealed.

Hudson CEO David Kramer, a mayoral friend and fund-raiser, proposed a 30-story residential tower with space for a new library on the ground floor and $40 million to fix up other branches. (It has since grown to 36 stories.)

Not only was his bid lower than others, Kramer was the only competitor to propose placing the required affordable housing units off-site — at a location two miles away in Clinton Hill. This despite de Blasio long decrying projects that separate subsidized apartments from market-rate ones.

Nearly all the other bidders put the affordable units in the same building on Clinton Street, according to sources familiar with the bid process. Kramer proposed 114 units; another bidder planned 117.

The price tag for Kramer’s plan came out to be about $100 per square foot less than the going rate of $500 per square foot for Brooklyn Heights properties.

Critics said Hudson got a de-Blasio-sized discount.

“This is a sweetheart deal to a politically connected supporter, directly contrary to de Blasio’s stated goals for development projects,” said one source familiar with the bids. “He was not going to be the winning guy pre-de-Blasio.”

Kramer has known de Blasio since the mayor was a Park Slope councilman.

He and his wife gave more than $9,000 to de Blasio’s public-advocate and mayoral campaigns since 2007, although de Blasio had to return all but $400 of Kramer’s cash because his company did business with the city.

‘This is a sweetheart deal to a politically connected supporter, directly contrary to de Blasio’s stated goals for development projects’

- a source

Hudson employees gave an additional $2,450 during that time.

Kramer acknowledged his friendship with the mayor but said he never spoke with Hizzoner about the project. Any insinuation the bid process was rigged is “insulting” to the library and the city, he said.

“There was absolutely nothing inappropriate going on,” Kramer said. “The market has spoken. There was a bid, it was competitive, and I was selected.”

Kramer acknowledged that he did speak to Deputy Mayor Alicia Glen, who used to run Goldman Sachs’ Urban Investment Group, to “look at the project” shortly after she arrived at City Hall. That same Goldman Sachs division provided Kramer with a $10 million loan to help finance the library project.

“What happens [is] a new administration comes in and City Hall wants to opine on it,” Kramer said. “They were doing a million different things.”

Library board member Peter Ashkenasy said, “At the end of the day, it was the issues that concerned the library the most — what the new library would look like and the economic benefits to the library, and Hudson had identified a site for the interim library. No one else had done that.”

But real-estate insiders were flabbergasted.

“This is the only project I ever did that didn’t go to the highest bidder,” one source said. “I’ve never seen it before.”

City Hall spokesman Wiley Norvell said Hudson’s project guaranteed a higher amount of housing than Toll Brothers’ 73 units, along with an interim library space during construction, making it clearly “the best package.”.