snappdf
snappdf copied to clipboard
setUrl with variables
Love the code! I have searched for WEEKS to find code to add to my web project, which is a scheduling and billing app, that will export php coded invoices to PDF and this is the 1st I've found that actually works!!! :) Kuddos!!!
I do have one issue and it's my lack of knowledge that I need help with.
For ->setUrl to pull from, I am trying to insert a local php file with variables. The code fails on the ampersand (&) in the URL. I changed the ampersand to %26 and it allowed the code to complete and the PDF was created. But the PDF now shows an error that the variable after the ampersand was not passed so the page errors. An example below. Is there a way to fix this?
->setUrl('http://localhost/projectname/invoice.php?id=202311101%26cid=101')
The PDF that is created shows undefined array key "cid"
Can anyone help? Much appreciated!!!
Hey there!
I've tried replicating this, without luck. For me & works just fine, here's the code I used.
// invoice.php
<?php
echo "Invoice: {$_GET['id']}, CID: {$_GET['cid']}";
// Code in different file used to call snappdf
$snappdf = new Snappdf();
$snappdf
->setUrl('http://localhost:8081/invoice.php?id=202311101&cid=101')
->save('invoice.pdf');
Generated PDF seems fine:
Are you able to provide repo to replicate an issue?
good afternoon! i will try to provide as much info as possible on the issue i am having. thank you so much for taking your time to assist me. i really appreciate it!
i have a file invoice.php and the 1st 2 lines are:
$id = $_GET['id']; $cid = $_GET['cid'];
i pass the variables using:
http://localhost/sitename/invoice.php?id=202311101&cid=101
this is the code i'm using for snappdf:
require 'vendor/autoload.php';
$snappdf = new \Beganovich\Snappdf\Snappdf();
$pdf = $snappdf ->setChromiumPath('C:\Progra~2\Microsoft\Edge\Application\msedge.exe') ->setUrl('http://localhost/lawnco/snap_invoice.php?id=202311101&cid=101') ->save('C:\inetpub\wwwroot\sitename\invoices\test.pdf');
If I leave the ampersand in the setUrl, i receive the following error:
PHP Fatal error: Uncaught Beganovich\Snappdf\Exception\ProcessFailedException: 'cid' is not recognized as an internal or external command, operable program or batch file. in C:\inetpub\wwwroot\lawnco\vendor\beganovich\snappdf\src\Snappdf.php:290 Stack trace: #0 C:\inetpub\wwwroot\lawnco\vendor\beganovich\snappdf\src\Snappdf.php(253): Beganovich\Snappdf\Snappdf->executeOnWindows() #1 C:\inetpub\wwwroot\lawnco\vendor\beganovich\snappdf\src\Snappdf.php(273): Beganovich\Snappdf\Snappdf->generate() #2 C:\inetpub\wwwroot\lawnco\snappdf.php(11): Beganovich\Snappdf\Snappdf->save() #3 {main} thrown in C:\inetpub\wwwroot\lawnco\vendor\beganovich\snappdf\src\Snappdf.php on line 290
If i replace the ampersand with %26 the code runs and the pdf is created, but the pdf shows as such instead of the complete pdf:
PHP Warning: Undefined array key "cid" in C:\inetpub\wwwroot\lawnco\snap_invoice.php on line 3PHP Warning: Trying to access array offset on null in C:\inetpub\wwwroot\sitename\invoice.php on line 71
If i hard code the variables into the id and cid at the top of invoice.php and setUrl to invoice.php with no variables, the pdf is generated correctly but i would like to do these in bulk providing the variables as invoices are generated..thereby saving a pdf of the invoice after they are created in the system.
when i passed the id to an echo on the pdf the id appears as 202211101&cid=101 and the cid is blank
Thanks so much for your response and sending me down the right path. I was able to make it work by escaping the & with a ^ in the setUrl. Awesome program. Hopefully this helps someone running into the same issue. :) Cheers!!!
Thanks for letting us know. I'll still try to replicate this on Windows as soon as I find some time and then close this issue.
Feel free to also send PR if you know the solution!