You request it free of charge at the AEAT electronic office using a digital certificate or Cl@ve. There are two versions: a general certificate and one issued under a specific tax treaty, which is the version foreign tax authorities usually ask for.
Rather have this calculated for you?
kontora generates your tax forms box by box, tells you how much to set aside and reminds you before every deadline.