At a pinch - yes you can although butter is preferable as it gives the gingerbread cake a much richer flavour and moist cake. Also, margarine may give the cake a different texture.