1

I have been struggling with a couple of strange issues in my free time project I'm workig on. It's my first "big" PHP / JS project, and to be honest I'm using ajax for the first time so I might be just missing something.

Anyways, here how it is. I'm programming a very simple invoicing system using PHP and jQuery technologies with mPDF library to generate an PDF file from HTML/CSS. I'm using mainly Session variables inside the template that gets sent to mPDF to generate an PDF invoice.

Issue I'm experiencing is on Chrome for Android, tested on latest version on OnePlus One. The Session variables are not showing in the PDF itself. I think it worked like once or twice totally at random. My friend with Android device and Google Chrome also confirms same issue.

test.php:

<?php
session_start();
error_reporting(E_ALL);

if (!isset($_SESSION['GLO_IS_LOGGED_IN'])) {
    header("Location: index.php");
    exit;
}

include('libs/mPDF/mpdf.php');

ob_start();
include('protected/templates/template.php');
$data = ob_get_clean();

$mpdf = new mPDF();
$mpdf->WriteHTML($data);
$mpdf->Output('protected/invoices/Faktura ' . date('j-m-Y-H-i-s') . '.pdf');
$mpdf->Output('Faktura ' . date('j-m-Y-H-i-s') . '.pdf', 'D');

unset($_SESSION['VAR_DESCRIPTION_ARRAY']);
unset($_SESSION['VAR_AMOUNT_ARRAY']);
unset($_SESSION['VAR_PRICE_ARRAY']);
unset($_SESSION['VAR_TO_ADDRESS']);
unset($_SESSION['VAR_INVOICE_NUMBER']);

Here is generateInvoice.php file that you might have noticed in the invoice-script.js:

<?php
session_start();
error_reporting(E_ALL);

if (!isset($_SESSION['GLO_IS_LOGGED_IN'])) {
    header("Location: index.php");
    exit;
}

if (!empty($_POST['invoice-number'])) {
    $_SESSION['VAR_INVOICE_NUMBER'] = trim($_POST['invoice-number']);
} else {
    echo('Please add invoice number');
    exit;
}

if (!empty($_POST['to-address'])) {
    $_SESSION['VAR_TO_ADDRESS'] = ($_POST['to-address']);
} else {
    echo('Internal Error');
    exit;
}

$_SESSION['VAR_DESCRIPTION_ARRAY'] = $_POST['invoice-description'];
$_SESSION['VAR_AMOUNT_ARRAY'] = $_POST['invoice-amount'];
$_SESSION['VAR_PRICE_ARRAY'] = $_POST['invoice-price'];

I don't want to make this post very-long so I'll stop posting any code snippets here. Believe me that I have done everything I could to find out myself what's going on and it feels really bad that I cant figure it out myself and that I need to ask others for help. Anyways thanks for any feedback and help. Cheers!

2 Answers2

1

'invoice-form' doesn't contain any fields - the input tags should be within the form

IanMcL
  • 366
  • 2
  • 10
  • http://stackoverflow.com/a/21622209/1796469 This is what I have done and theoretically it should work just fine. Reason for this was that the form can not be a parent of a TABLE. Cheers – user1796469 Mar 28 '16 at 19:56
  • Short addon to my previous comment. I have just noticed that people said this will not work on IE, and anything below HTML5. I'll give it a shot and provide with an update. Thanks! – user1796469 Mar 28 '16 at 20:01
  • 1
    stackoverflow.com/a/21622209/1796469 says that a Form cannot be a child of a Table, rather than a parent – IanMcL Mar 28 '16 at 20:04
  • Good point! My bad! I'll try to come back to you with an update shortly. – user1796469 Mar 28 '16 at 20:05
  • This have fixed my problem with Microsoft Edge. However, Chrome for Android still generates empty PDF's (empty in the matter that the session variables i put into the invoice fields do not show up). Works fine with Opera and Firefox on Android. Cheers! – user1796469 Mar 28 '16 at 20:48
  • check that the Chrome on Andriod browser can accept session cookies http://stackoverflow.com/questions/6663859/check-if-cookies-are-enabled – IanMcL Mar 28 '16 at 22:00
  • According to my settings, it can. I can also log in and keep the session so it seems like there shouldn't be any problems with that. Asked my friend with Android to confirm this and he also gets empty invoice. – user1796469 Mar 28 '16 at 22:26
0

Ok people, I think I have found a solution for this.

Commenting away all of the unset methods at the end in test.php solved the Chrome for Android issue.

I don't understand why this was happening in the first place. Why were the session variables unset BEFORE the invoice was generated? They shouldn't be, right? Or I am really missing something? I know I shouldn't ask for clarification in my own answer but I think at this point I really need to.

Cheers and thanks to IanMcL for solving my Edge issue!