We demonstrate broadband, frequency-tunable, phase-locked terahertz (THz) generation and detection based on difference frequency mixing of temporally and spectrally structured near-infared (NIR) pulses. The pulses are prepared in a gas-filled hollow-core photonic crystal fiber (HC-PCF), whose linear and nonlinear optical properties can be adjusted by tuning the gas pressure. This permits optimization of both the spectral broadening of the pulses due to self-phase modulation (SPM) and the generated THz spectrum. The properties of the prepared pulses, measured at several different argon gas pressures, agree well with the results of numerical modeling. Using these pulses, we perform difference frequency generation in a standard time-resolved THz scheme. As the argon pressure is gradually increased from 0 to 10 bar, the NIR pulses spectrally broaden from 3.5 to 8.7 THz, while the measured THz bandwidth increases correspondingly from 2.3 to 4.5 THz. At 10 bar, the THz spectrum extends to 6 THz, limited only by the spectral bandwidth of our time-resolved detection scheme. Interestingly, SPM in the HC-PCF produces asymmetric spectral broadening that may be used to enhance the generation of selected THz frequencies. This scheme, based on a HC-PCF pulse shaper, holds great promise for broadband time-domain spectroscopy in the THz, enabling the use of compact and stable ultrafast laser sources with relatively narrow linewidths (< 4 THz)