How do I use dynamic variable inside PHP tags

Probably used wrong terminology here, sorry :slight_smile:

The following is an include file used with HTML2PDF. It works well as it is, but I want to make things dynamic.

For instance I want to change the include template file from
/admin/pdf_template_AD_Report.php >> /admin/{{dynamic_template_filename}}.php
and also the output filename
neiltest.pdf >> {{dynamic_filename}}.pdf

but I don’t know how to format the Wappler generated value inside the PHP tags.

    /**
        * Html2Pdf Library - example
        *
        * HTML => PDF converter
        * distributed under the OSL-3.0 License
        *
        * @package   Html2pdf
        * @author    Laurent MINGUET <webmaster@html2pdf.fr>
        * @copyright 2017 Laurent MINGUET
    */

    require_once dirname(__FILE__).'/vendor/autoload.php';

    use Spipu\Html2Pdf\Html2Pdf;
    use Spipu\Html2Pdf\Exception\Html2PdfException;
    use Spipu\Html2Pdf\Exception\ExceptionFormatter;

    try {
        ob_start();
        include dirname(__FILE__).'/admin/pdf_template_AD_Report.php';
        $content = ob_get_clean();

        $html2pdf = new Html2Pdf('P', 'A4', 'fr');
        $html2pdf->writeHTML($content);
        $html2pdf->output('neiltest.pdf');
    } catch (Html2PdfException $e) {
        $html2pdf->clean();

        $formatter = new ExceptionFormatter($e);
        echo $formatter->getHtmlMessage();
    exit("End");
    }
?>```

You must know that PHP is executed on your webserver and App Connect runs in the browser. You should call the PHP script as an API, you can pass data to it using the querystring or as post data. In PHP you then use $_GET or $_POST.

1 Like

Sorry @patrick, having a Friday afternoon nightmare. Yes, thanks for the reminder that App Connect works client-side. An important part that I had forgotten.

So if I follow @Teodor’s Using API Data Source tutorial then I should get somewhere should I?

Can I treat a PHP file in the same server space as an API? If so then I have never attempted this before, can you give some hints or guidance please based on the info above?

Is this the php script you are using?

Thank you!

Hi Nikolaos, yes it is.
Is this something you can help with @t11 ?

Well I’m getting nowhere fast again. I can’t work out what to do here at all. Can someone help please.

To extend what I think Patrick is suggesting…

You will have the php file you shared above with some modifications to receive GET parameters:

// set some variables for use inside the provided script
$dirname = $_GET["dirname"];
$output = $_GET["output"];

/**
        * Html2Pdf Library - example
        *
        * HTML => PDF converter
        * distributed under the OSL-3.0 License
        *
        * @package   Html2pdf
        * @author    Laurent MINGUET <webmaster@html2pdf.fr>
        * @copyright 2017 Laurent MINGUET
    */

    require_once dirname(__FILE__).'/vendor/autoload.php';

    use Spipu\Html2Pdf\Html2Pdf;
    use Spipu\Html2Pdf\Exception\Html2PdfException;
    use Spipu\Html2Pdf\Exception\ExceptionFormatter;

    try {
        ob_start();
        include dirname(__FILE__).$dirname;
        $content = ob_get_clean();

        $html2pdf = new Html2Pdf('P', 'A4', 'fr');
        $html2pdf->writeHTML($content);
        $html2pdf->output($output);
    } catch (Html2PdfException $e) {
        $html2pdf->clean();

        $formatter = new ExceptionFormatter($e);
        echo $formatter->getHtmlMessage();
    exit("End");
    }
?>

Now using an api action you can call this script:
Method = GET
URL = path to the script file
Setup a Query parameter on the action for dirname and output (and others you might need,) and setup the values you desire.

I haven’t set this up myself, so nothing here has been tested…just trying to move you forward a step!

2 Likes

I will try to implement it but was away.

1 Like

Make sure you validate the user input, don’t just use it directly like include dirname(__FILE__).$dirname;.

A user could set it to something like /../../../etc/passwd to get access to the password file of your server or other sensitive information.

1 Like

Hi,

-I add API Action to client side.
-Method: GET
-URL: path to php script file
-Data Type: Auto
-Added 2 query parameters
-Setup 2 query paramters top of my php script.
-Assigned 2 of the to my variables.

But objects are empty. Does not returns any value.

Do you have another working solution?