Formation of the spectrum of ultra-high-energy cosmic rays: Effect of extragalactic magnetic fields and expected contribution of individual sources