We report on a first-principles investigation of photocurrent generation by graphene $PN$ junctions. The junctions are formed by either chemically doping with nitrogen and boron atoms, or by controlling gate voltages. The nonequilibrium Green's function formalism combined with density functional theory is applied to calculate the photoresponse function. The graphene $PN$ junctions show a broadband photoresponse including the terahertz range. The dependence of the response on the angle between the light polarization vector and the $PN$ interface is determined. Its variation against photon energy ${E}_{\text{ph}}$ is calculated in the visible range. The essential properties of chemically doped and gate-controlled $PN$ junctions are similar, but the former shows fingerprints of dopant distribution.