All praise is due to Allah, and peace and blessings be upon the Messenger of Allah.
There is no contradiction between these two principles according to Ahl al-Sunnah wa al-Jama'ah. Rather, each applies in its proper context.
Whoever sincerely repents from a major sin before death, Allah forgives him, for the one who repents from sin is like one who has no sin.
As for the one who dies while persisting in a major sin without repentance, he is under the will of Allah. If Allah wills, He forgives him; and if He wills, He punishes him, as Allah says:
"Indeed, Allah does not forgive associating partners with Him, but He forgives what is less than that for whom He wills."
(Qur'an 4:48)
At the same time, the scale on the Day of Judgment will determine the balance between good deeds and bad deeds. Whoever's good deeds outweigh his bad deeds will be among the people of Paradise, while whoever's bad deeds outweigh his good deeds deserves punishment. However, if he dies upon Tawhid, his final destination will be Paradise.
The fact that a person's bad deeds outweigh his good deeds does not necessarily mean that he will certainly enter Hell, because he still remains under the will of Allah. Allah may forgive him before punishing him, and this is part of His perfect mercy.
Furthermore, the weighing of deeds takes place after people's rights have been settled and justice has been established between them. Only then are good deeds and bad deeds weighed.
Whoever dies upon Tawhid without repenting from a major sin remains under the will of Allah. If his good deeds outweigh his bad deeds, he is among the people of Paradise. Allah may forgive him without punishing him, and even if he deserves punishment, he will not remain eternally in Hell; rather, his final destination is Paradise by the mercy of Allah.
And Allah knows best.