An Uber driver drove at cops outside Buckingham Palace then tried to attack them with a samurai sword because he wanted to “be in paradise with Allah,” a court in England heard.

It is alleged that Mohiussunnath Chowdhury, 27, wrote a “martyrdom note” to his sister that said “the Queen and her soldiers will all be in hellfire” before his attack around 8:30 p.m. on Aug. 25, 2017.

He then swerved his car through cones outside the palace, and as two police officers got out of a passing police van he brandished the sword and shouted “Allahu Akbar,” jurors were told.

But the sword was wrestled from him and he was arrested, a jury heard. Chowdhury denies one count of engaging in conduct in preparation of terrorist acts.

British national Chowdhury wrote the martyrdom note three hours before the attack, after researching terrorists including Khalid Masood, the Westminster Bridge attacker whom he described as a “martyr,” it was said.

He had also allegedly changed his WhatsApp picture to an ISIS flag and wrote messages of support for the group.

Prosecutor Timothy Cray said: “He stated in clear terms that his intention was to get to paradise by becoming a martyr, fighting in his words ‘the enemies of Allah.'”

Part of the note reportedly said: “Tell everyone that I love them and that they should struggle against the enemies of Allah with their lives and property.

“The Queen and her soldiers will all be in the hellfire. They go to war with Muslims around the world and kill them without any mercy.

“They are the enemies that Allah tells us to fight.”

Jurors heard just 30 minutes before he penned the note that he bought a “sharpening tool” from a supermarket, which he used on the samurai sword.

Cray added: “It was due to the bravery and quick reaction of these uniformed police officers that something even more serious did not happen.

“As the police officers got to the car the defendant said to them words to the effect, ‘it’s all a bit f—-d up.’

“They then saw him reach for the sword, the officers were struggling with him, getting the passenger door open, reaching into the driver’s door to stop him using the sword.”

The “short but fairly desperate struggle” ended when the officers snatched the sword away, with a police officer sustaining a cut to his hand, the jury heard.

Cray said the officers quickly suspected it was a terror attack, adding: “This was just six months after, for example, the murders at the Houses of Parliament and Westminster Bridge and a further attack at London Bridge.

“Here we were in another prominent London location where a man had driven his vehicle erratically, was armed with a sword and seemed to be attacking uniformed police officers.”

In an interview Chowdhury admitted the events and said he saw police as people who do “the dirty work of the UK government,” the court heard.

Detectives then discovered his internet history and WhatsApp chats, which revealed his support for Islamist terrorism, Cray said.

Chowdury also drew “posters” in prison of an officer being shot by a man shouting “Allahu Akbar” and a picture of a plane hitting the Twin Towers, the court was told.

He then pinned the posters up on his cell wall after he was remanded in custody ahead of his terror trial, it was said.

The trial continues.