The task: in a PHPRunner event, launch a PDF in a new tab/window. Really?? Something that I had expected to be super-trivial ended up taking me hours of googling stack overflow just to figure out how to approach this. Let’s start with why it was a problem and then get to the very quick one-liner solution. This is more a PHP tip, than a PHPRunner tip, but I needed it recently in a PHPRunner application so maybe you do to?
First of all, even though we use PHP for web page interactions, it’s very easy to forget that PHP is a server-side scripting language. Especially as we have PHP script events surrounding all our data interactions in PHPRunner – it “feels” like PHP code is executing on the client. But it’s not! All of which means that PHP cannot make the browser do anything directly.
If you were building a web page in HTML, there are plenty of ways to set a hyperlink and tie an event/listener to it so that your user can click the href and you can control how the link opens a new target. But what I (and many others apparently) were chasing was how to have a PHPRunner Add page accept data, and in an after_record_added event, have a PHP process run and generate a PDF dynamically based on data just added, then once the server processing is complete, and the PDF is available on the server, have it automatically open the PDF for the user. In a new window or tab. We should note here that the latter part isn’t something you get to decide – the user’s browser decides whether it will default to open the ‘window’ either as a new tab or as a new window. All you can do is open the window. Which is quite acceptable.
$mypdf = "../myserverfolderpath/" . $mypdf_filename ; echo "<script>window.open('$mypdf', '_blank');</script>";
Now my process flows beautifully!! I start on a List page for transactions, my createPDF button click goes to the Add page for my PDF process, the user inputs data, I do the Add, my after add event gets the user data, fires the server PHP script to generate the PDF, when that completes, the PDF opens in a new window, and my user is put back to the page he first clicked the “create PDF” button on.