I have done it with a couple of PHP libraries (fpdf and mpdf), but they are primitive, compared to desktop PDF generators. I know that you can use Java (never done that), or even...ugh...XSL (also never done that).
Most desktop operating systems offer a print-to-PDF functionality. It's long been an add-on for Microsoft, but that's really a historical accident / deliberate choice of that platform.
PDFs can be trivially created from Markdown or using LaTeX templates if you're looking for a programmatic solution. Pandox and XeLaTex are helpful, the poppler libraries as well. Again, these are generally and widely available at no charge.
I have done it with a couple of PHP libraries (fpdf and mpdf), but they are primitive, compared to desktop PDF generators. I know that you can use Java (never done that), or even...ugh...XSL (also never done that).