Hello,
Use this to build the current URL:
$url = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"];
I recommend you to take a look at this
tutorial. It contains the "Provide a PDF version of your web pages" section which provides an example code.