Vernon Philander claimed his ninth five-wicket haul in his 15th test for South Africa, but a spirited ninth-wicket partnership boosted Pakistan to 338 on day two of the second test at Newlands.
The visiting side was bowled out on the stroke of lunch, with Philander finishing with figures of 5-59 from 26 overs.
Philander struck in the second, fourth and sixth overs of the day to reduce Pakistan to 268-8, but Tanvir Ahmed and Saeed Ajmal put on 64 for the ninth wicket to keep the innings rolling.
Spinner Robin Peterson eventually ended the stand when he dismissed Ahmed for 44, before bringing the innings to a close by bowling last man Mohammad Irfan.