For a purchase in Pennsylvania, I think whether the gift will be legal depends on the relationship of the giftee to you. The instructions for the federal 4473 form permit you to answer “yes” to the question about being the actual purchaser if you are “legitimately purchasing the firearm as a gift for a third party.” You must be using your own money to buy the gun with the intent that you will give it to someone else for no compensation.
Under PA law, you can also buy a gun as a gift, but it must be a relative. Under 18 Pa.C.S. § 6111, you are still the “actual purchaser” of a firearm if you are buying it for a:
(4) grandparent; or
This is also the same list of persons one can transfer a handgun to in Pennsylvania without having to go through a PICS check. A transfer to anyone who is not your spouse or someone who is not a lineal ancestor or descendant must go through a background check and fill out the forms.
Long story short: I think you could legally buy and gift a gun to a spouse, parent, child, grandparent, or grandchild. If you want to buy a handgun for someone who doesn’t fall into those categories, then it would be better to get them a gift certificate or do something else that would render that person the “actual purchaser” of the gun. For example, I bet a gun shop would let you pay for the gun and then hold it for your giftee. That person could then go pick it up and fill out the forms himself/herself.