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.
So what do we know? We know that in javascript we can open a new window. The snag is I don’t want to have my users click another button or link or screen when my server PHP process completes. I want the process to complete, then automatically open the document. guess what? PHP can send javascript commands! okay… now it’s easy. In the PHP script that is driving your PDF generation, assuming it got started in a browser, and once your PHP server side is complete, you just include the following PHP / Javascript and that PDF will open!
$mypdf = "../myserverfolderpath/" . $mypdf_filename ; echo "<script>window.open('$mypdf', '_blank');</script>";
Put simply, we are embedding javascript in the PHP page. Now that you see that working… this should open up a whole bunch of other things you can do in certain parts of your PHP code. Take care with your syntax, especially with those quotes. PHP treats doublequotes differently from singlequotes (but you knew that already!).
In my case, after the ‘add page’ completed, I didn’t want to stay on the Add, and I didn’t want to go to the List page, I needed to go back two pages in the browser to a different page. Javascript in PHP to the rescue again? of course!
echo "<script>history.go(-2);</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.