0

We have a PHP script that runs daily ( already working fine) that generates a PDF report for our employees. Now upon this generated PDF we need to add attachments/files that are generated by a JavaScript function. In this JavaScript function we work with data, loaded from our database. This data is different every day and saves a daily attachment file on our server.

In the PHP script mentioned above, we need to include those files/attachments created by JS.

My though was to run a JS function when the PHP script runs, daily. As I have found in this stackoverflow question ( see answer by Vladimir ) it is basically impossible to run js from a PHP script.

How can I achieve this? How can I make sure the files/attachments are generated by JS every day, before the PHP script runs?
(It is allowed for the javascript attachments/files to be generated at 2am, for example. While the PHP script can run at 3am. It doesn't have to be the exact same time).

Any suggestinos would help!

nclsvh
  • 2,628
  • 5
  • 30
  • 52
  • 1
    Where does your JavaScript code normally run? That question you referenced is about calling *client side* JavaScript from PHP, and yes that does not make sense. However, JavaScript can also run at the server (Node.js). – Pointy Apr 25 '18 at 12:55
  • We do not have node.js servers. As of now I have a simple `.html` files that includes the javascript function and runs when the page is ran in a browser.. – nclsvh Apr 25 '18 at 12:57
  • 1
    OK, then the best you could do would be to use something like a headless browser or maybe a testing framework to run the code, but it seems to me that it might be a lot easier to just get it to work directly in Node. – Pointy Apr 25 '18 at 12:58
  • Setting up a node server is not gonna be possible (i'll spare you the reason why). Gonna look into headless browsers, I have no experience with this yet. – nclsvh Apr 25 '18 at 13:01
  • 1
    I wonder if using cURL from PHP to call a specific URL would do it for you. – Dave Apr 25 '18 at 13:03
  • What exactly is the .js doing? – Manuel Otto Apr 25 '18 at 13:07
  • @ManuelOtto it checks the database for the latest data and generates charts who are saved as .png (to be printed in the PDF inside the php-script). Later we might upgrade it to more than just these charts (but thats not for the near future). – nclsvh Apr 25 '18 at 13:20
  • @nclsvh i see. You're probably gonna have a hard time running it on nodejs, since the charts library will require DOM/canvas/svg. So you'd have to go with running a headless browser on the serverside. – Manuel Otto Apr 25 '18 at 13:28
  • *"it checks the database for the latest data and generates charts who are saved as .png (to be printed in the PDF inside the php-script)"* ... how exactly are you doing that in JavaScript? JS runs on the browser it can't, by itself, connect to the database on the server, nor generate files - are you sure you don't mean Java? – CD001 Apr 25 '18 at 13:38
  • @CD001 I haven't written the part where it connects to the database, but I am pretty sure a simple ajax call will get me my data. – nclsvh Apr 25 '18 at 13:49
  • @ManuelOtto node.js servers are not even an option.. i'm researching headless browsers atm. Hadn't heared of it yet, I have little PHP experience. – nclsvh Apr 25 '18 at 13:50
  • *" I am pretty sure a simple ajax call will get me my data"* ... Ye-es but then that's not JavaScript connecting to the database, that's whatever's at the end of the Ajax call doing the connecting... and it'll be that script that's generating the chart images I'm guessing ... so *why* do you need JS for any of this, just re-use whatever that file being called by Ajax is doing. – CD001 Apr 25 '18 at 13:51
  • The ajax call wil only get data (array of values) from the db. The JS-code will generate a chart with Google Charts with that data. Those charts are needed in the PHP-script. Is tha tnot possible? Or are we talking different things? – nclsvh Apr 25 '18 at 13:54
  • @nclsvh How you are converting the google charts to png? – Manuel Otto Apr 25 '18 at 14:00
  • Ah - so what you *actually* want to do is grab a JSON from your server and lob it at Google Charts and use *that* output somewhere else? This might help maybe: https://stackoverflow.com/questions/20554113/how-to-get-webcontent-that-is-loaded-by-javascript-using-curl – CD001 Apr 25 '18 at 14:01
  • @ManuelOtto ATM I have the chart as a base64 code – nclsvh Apr 25 '18 at 14:09
  • @CD001 thanks, will check out the other question! Might help :) – nclsvh Apr 25 '18 at 14:10

1 Answers1

0

I would work toward using curl to get the JS to run by calling the html file with curl from within the PHP script.

A lot of this depends, of course, on what the JS is doing, exactly, but if you have access to both codebases, you should be able to get it to work, by saving the response from curl into a variable and then parsing and handling it appropriately.

I know this answer is a generalization, but this is because we only generally know how the JS and PHP need to interact. I will be happy to be more specific if we get a few more details.

Good luck and Take care,

JB

stubsthewizard
  • 352
  • 2
  • 12
  • 1
    Unless I'm misunderstanding something; cURL is not a web browser - it'll merely ping the URL and (optionally) grab the response, it won't run the JavaScript there, it doesn't have a JS compiler. – CD001 Apr 25 '18 at 13:33
  • Hi there. No. You are not misunderstanding. cUrl will not run the JS. Good catch on that. Without knowing the exact nature of the JS, I just have to spitball a bit here. Two solutions come to mind: 1. Use something like PhantomJS or other third paty application that is headless like cUrl but parses the JS as well. 2. Use server based JS like nodejs or some such to do the whole thing. – stubsthewizard Apr 25 '18 at 17:49
  • One other thing, I would mention, and I mean no offense by this at all, but I don't really think JS is a great solution for this project. Here is why. Javascript cannot save files to the local machine, with the exception of cookies, or browser confirmation in the form of a download or something. AFAIK, javascript can't connect to a database. These restrictions are in place for the security of people browsing the internet, as you know. Were I in your position, I would look for a way to move the entire project into PHP, and forego using JS in this instance. – stubsthewizard Apr 25 '18 at 17:59
  • Oh normally, I'd agree but it took me a while to work out what the OP was even *really* asking; it sounds like they want to throw a JSON at Google Charts, retrieve the resulting image (which will be an SVG I believe) and embed that into a PDF - I'm not familiar enough with Google Charts or its API though to be able to give a comprehensive answer. – CD001 Apr 26 '18 at 08:16