[SOLVED] mpdf/dompdf library to generate PDF + query issue + Example

Status
Not open for further replies.

marcq

Member
Hi,

I struggled days with dompf to generate a correct invoice, without success. So I have uploaded the mpdf library to my server and I would like to use it, since it seems to have more design possibilities than dompdf.

Can I use mpdf in the Fabrik environment

If yes, I have created a template (components/com_fabrik/views/list/tmpl/invoice/invoice.php) that I would like to generate by clicking a button in list view.

How can I do this ?

Thanks in advance for your suggestions.

Cheers,

Marc

ps : template is attached
 

Attachments

  • CODE.txt
    19.9 KB · Views: 309
Not something we can help with on a subscription basis, I'm afraid.

If you'd like to hire us to implement an mpdf view, we could take a look at it.

-- hugh
 
Hi Hugh,

Thanks to Th?o I was able to create a PDF output with dompdf:

http://fabrikar.com/forums/index.php?threads/just-a-pdf-template-example.42965/

I'm not finished with it, because I'm struggling with "injecting" 2 queries in my code and with placeholder raw values which are not applied.

I tried several thing but I'm just fed up.

I'm paying to do the job, so please if you have time, just PM me a quote.

Cheers,

Marc

Queries are :
Training
Code:
<?php
$candidateid = JRequest::getVar('gprh_fabrik_user_enrollment___id');
$db = JFactory::getDbo();
   $query = $db->getQuery(true);
   $query
      ->select (array('gprh_fabrik_user_enrollment.id', 'gprh_fabrik_user_training_124_repeat.nom_formation', 'gprh_fabrik_user_training_124_repeat.intitule_session', 'gprh_fabrik_user_training_124_repeat.session_du', 'gprh_fabrik_user_training_124_repeat.session_au'))
      ->from('gprh_fabrik_user_enrollment')
      ->leftJoin('gprh_fabrik_user_enrollment_repeat_choix_formation ON gprh_fabrik_user_enrollment.id = gprh_fabrik_user_enrollment_repeat_choix_formation.parent_id')
      ->leftJoin('gprh_fabrik_user_training_124_repeat ON gprh_fabrik_user_enrollment_repeat_choix_formation.choix_formation = gprh_fabrik_user_training_124_repeat.id')
      ->leftJoin('gprh_fabrik_user_training ON gprh_fabrik_user_training_124_repeat.parent_id = gprh_fabrik_user_training.id')
      ->having('gprh_fabrik_user_enrollment.id = ' . $db->quote($candidateid))
     ->group('gprh_fabrik_user_enrollment.id, gprh_fabrik_user_training_124_repeat.nom_formation, gprh_fabrik_user_training_124_repeat.intitule_session, gprh_fabrik_user_training_124_repeat.session_du, gprh_fabrik_user_training_124_repeat.session_au');
   $db->setQuery($query);
   $row = $db->loadObjectList();
   echo "<div style='font-family: arial, helvetica, sans-serif; font-size: 11pt;'><UL>";
   foreach ($row as $item)
   {
           $du_date = new DateTime($item->session_du, new DateTimeZone('Europe/Zurich'));
           $du_date_formatted = $du_date->format('d/m/Y');
           $au_date = new DateTime($item->session_au, new DateTimeZone('Europe/Zurich'));
           $au_date_formatted = $au_date->format('d/m/Y');

      echo "<LI> $item->nom_formation | ";
      echo "$item->intitule_session | ";
      echo "du : $du_date_formatted  | ";
      echo "au : $au_date_formatted <br /><br />";
      echo "</LI>";
   }
   echo "</UL></div>";
?>

and

Documents
Code:
<?php
$candidateid = JRequest::getVar('gprh_fabrik_user_enrollment___id');
$db = JFactory::getDbo();
   $query = $db->getQuery(true);
   $query
      ->select('gprh_fabrik_user_enrollment.id, gprh_fabrik_user_training.documents_a_fournir')
      ->from('gprh_fabrik_user_enrollment')
      ->leftJoin('gprh_fabrik_user_enrollment_repeat_choix_formation ON gprh_fabrik_user_enrollment.id = gprh_fabrik_user_enrollment_repeat_choix_formation.parent_id')
      ->leftJoin('gprh_fabrik_user_training_124_repeat ON gprh_fabrik_user_enrollment_repeat_choix_formation.choix_formation = gprh_fabrik_user_training_124_repeat.id')
      ->leftJoin('gprh_fabrik_user_training ON gprh_fabrik_user_training_124_repeat.parent_id = gprh_fabrik_user_training.id')
      ->having('gprh_fabrik_user_enrollment.id = ' . $db->quote($candidateid));
   $db->setQuery($query);
   // var_dump((string)$query);
   $docscandidat = $db->loadResult();
?>
 

Attachments

  • Inscription formation - Acceptée-188-9.pdf
    1.1 MB · Views: 302
Last edited:
If you can give me some details, like which list / form it's on, I'll take a look.

-- hugh

Hi Hugh, thanks for you reply :

PDF is working well now (see attached file), I'm just struggling with the sql query :

template file is : /projects/gprh.ch/components/com_fabrik/views/details/tmpl/bootstrap_printinvoice/default.php
Joomla form is 32 "Inscription formation - Accept?e".

Let's start with the first query, I guess if you show me how to do it (I have no problem inserting a query into a email template, but I'm unable to do it is this one):

I would like to have this query running at line 71 of the default.php file :

Code:
$candidateid = "<?php echo '{rowid}'; ?>";
$db = JFactory::getDbo()
$query = "SELECT gprh_fabrik_user_enrollment.id, gprh_fabrik_user_training_124_repeat.nom_formation, gprh_fabrik_user_training_124_repeat.intitule_session, gprh_fabrik_user_training_124_repeat.intitule_prix, gprh_fabrik_user_training_124_repeat.session_du, gprh_fabrik_user_training_124_repeat.session_au, gprh_fabrik_user_training_124_repeat.prix, gprh_fabrik_user_training_124_repeat.frais, [prix]+[frais] AS total
FROM ((gprh_fabrik_user_enrollment LEFT JOIN gprh_fabrik_user_enrollment_repeat_choix_formation ON gprh_fabrik_user_enrollment.id = gprh_fabrik_user_enrollment_repeat_choix_formation.parent_id) LEFT JOIN gprh_fabrik_user_training_124_repeat ON gprh_fabrik_user_enrollment_repeat_choix_formation.choix_formation = gprh_fabrik_user_training_124_repeat.id) LEFT JOIN gprh_fabrik_user_training ON gprh_fabrik_user_training_124_repeat.parent_id = gprh_fabrik_user_training.id
WHERE (((gprh_fabrik_user_enrollment.id)=$candidateid"));
$result = mysql_query($query);
$row=mysql_fetch_row($result);
echo $row;

Thank you in advance,

Marc

Query which is running and working into my email template is

Code:
$candidateid = JRequest::getVar('gprh_fabrik_user_enrollment___id');
$db = JFactory::getDbo();
   $query = $db->getQuery(true);
   $query
      ->select (array('gprh_fabrik_user_enrollment.id', 'gprh_fabrik_user_training_124_repeat.nom_formation', 'gprh_fabrik_user_training_124_repeat.intitule_session', 'gprh_fabrik_user_training_124_repeat.session_du', 'gprh_fabrik_user_training_124_repeat.session_au'))
      ->from('gprh_fabrik_user_enrollment')
      ->leftJoin('gprh_fabrik_user_enrollment_repeat_choix_formation ON gprh_fabrik_user_enrollment.id = gprh_fabrik_user_enrollment_repeat_choix_formation.parent_id')
      ->leftJoin('gprh_fabrik_user_training_124_repeat ON gprh_fabrik_user_enrollment_repeat_choix_formation.choix_formation = gprh_fabrik_user_training_124_repeat.id')
      ->leftJoin('gprh_fabrik_user_training ON gprh_fabrik_user_training_124_repeat.parent_id = gprh_fabrik_user_training.id')
      ->having('gprh_fabrik_user_enrollment.id = ' . $db->quote($candidateid))
     ->group('gprh_fabrik_user_enrollment.id, gprh_fabrik_user_training_124_repeat.nom_formation, gprh_fabrik_user_training_124_repeat.intitule_session, gprh_fabrik_user_training_124_repeat.session_du, gprh_fabrik_user_training_124_repeat.session_au');
   $db->setQuery($query);
   $row = $db->loadObjectList();
   echo "<div style='font-family: arial, helvetica, sans-serif; font-size: 11pt;'><UL>";
   foreach ($row as $item)
   {
           $du_date = new DateTime($item->session_du, new DateTimeZone('Europe/Zurich'));
           $du_date_formatted = $du_date->format('d/m/Y');
           $au_date = new DateTime($item->session_au, new DateTimeZone('Europe/Zurich'));
           $au_date_formatted = $au_date->format('d/m/Y');

      echo "<LI> $item->nom_formation | ";
      echo "$item->intitule_session | ";
      echo "du : $du_date_formatted  | ";
      echo "au : $au_date_formatted <br /><br />";
      echo "</LI>";
   }
   echo "</UL></div>";
 

Attachments

  • INVOICE.pdf
    1.8 MB · Views: 445
Try just using $form->id.

Code:
$candidateid = $form->id

Placeholders don't work in regular PHP files. They only work in "inline" PHP that you enter directly in the Fabrik backend, or in things like PHP tmeplates you select for the email plugin, which we run placeholder replacement on before handing to PHP to be eval'ed.

The default.php file is just run as a normal PHP file.

-- hugh
 
Thanks Hug but it is not working.

Process :

From the list view, I'm choosing a record (in my case corresponding to list row id 188) :
"index.php/gestion-des-formations-sa/inscriptions/formation-acceptee/form/32/188"

and I'm editing the Form in order to see the Details of the data corresponding to row id 188.

By checking with PDF debugging if the data returned belong to this row id, the query is returning me the data corresponding to row id 32.

Is it possible to get the rowid and to use it in my query ?

Code:
$candidateid = $form->id;
$db = JFactory::getDbo();
   $query = $db->getQuery(true);
   $query
      ->select (array('gprh_fabrik_user_enrollment.id', 'gprh_fabrik_user_training_124_repeat.nom_formation', 'gprh_fabrik_user_training_124_repeat.intitule_session', 'gprh_fabrik_user_training_124_repeat.session_du', 'gprh_fabrik_user_training_124_repeat.session_au')) 
      ->from('gprh_fabrik_user_enrollment')
      ->leftJoin('gprh_fabrik_user_enrollment_repeat_choix_formation ON gprh_fabrik_user_enrollment.id = gprh_fabrik_user_enrollment_repeat_choix_formation.parent_id')
      ->leftJoin('gprh_fabrik_user_training_124_repeat ON gprh_fabrik_user_enrollment_repeat_choix_formation.choix_formation = gprh_fabrik_user_training_124_repeat.id')
      ->leftJoin('gprh_fabrik_user_training ON gprh_fabrik_user_training_124_repeat.parent_id = gprh_fabrik_user_training.id')
      ->having('gprh_fabrik_user_enrollment.id = ' . $db->quote($candidateid))
     ->group('gprh_fabrik_user_enrollment.id, gprh_fabrik_user_training_124_repeat.nom_formation, gprh_fabrik_user_training_124_repeat.intitule_session, gprh_fabrik_user_training_124_repeat.session_du, gprh_fabrik_user_training_124_repeat.session_au');
   $db->setQuery($query);
   $row = $db->loadObjectList();
   echo "<div style='font-family: arial, helvetica, sans-serif; font-size: 11pt;'><UL>";
   foreach ($row as $item)
   {
           $du_date = new DateTime($item->session_du, new DateTimeZone('Europe/Zurich'));
           $du_date_formatted = $du_date->format('d/m/Y');
           $au_date = new DateTime($item->session_au, new DateTimeZone('Europe/Zurich'));
           $au_date_formatted = $au_date->format('d/m/Y');

      echo "<LI> $item->nom_formation | ";
      echo "$item->intitule_session | ";
      echo "du : $du_date_formatted  | ";
      echo "au : $au_date_formatted <br /><br />";
      echo "</LI>";
   }
   echo "</UL></div>";
 
Last edited:
Try that. $this->rowid.

I've turned error reporting up to max so you can see the other issues in the code.

-- hugh
 
Thanks a lot Hugh, I can now add 2 more queries by myself.
Thanks also to have turned the error reporting to max, issues are fixed now.
Cheers,
Marc
 
Hi Hugh,
I finalized my PDF template, I have set the PDF debug to no and how interesting I have a error message which is displayed instead of the download or save popup :

Fatal error: Call to undefined method DOMText::getAttribute() in /homepages/38/d566226193/htdocs/projects/gprh.ch/libraries/dompdf/include/cellmap.cls.php on line 437

By the way there were no error in debug mode, the html output was good.

I guess I've something in my code that causes this error, I will check and try to solve it by myself.
 
Last edited:
You might want to run the test mode through https://validator.w3.org/

You can either give it the URL to load, or copy and paste the page source.

Those kinds of errors from DOMPDF are usually to do with errors in the HTML structure. That code seems to be to do with spanned table cells, so make sure any tables you are building don't have structure errors (like missing closing tags, incorrect span counts, etc).

-- hugh
 
Hi Hugh,

Thanks for sharing the "Validator", it was indeed a ">" which I forgot to delete...

Now everything is working fine. Output is not bad at all, so I'm quite satisfied with it, I just need to improve the size (A4) of each page in order to have the footer positioned more "precisely" at the bottom of each pages. As soon as it is done, I will share the template since it can be of interest for people who don't know how to inject properly queries as it was my case

Thanks for your advise and also to Th?o for sharing is template which was a great basis to start.

Cheers,

Marc
 

Attachments

  • Inscription formation - Acceptée-188-44.pdf
    1.1 MB · Views: 290
Yup, DOMPDF is a lot less forgiving about HTML syntax errors than a browser is. So that's pretty much the first place to start if you get weird errors in DOMPDF when something displays OK in the browser in PDF test mode.

-- hugh
 
To whom it might interest the template + footer + custom_css files :

Form detail view PDF
Template location : /components/com_fabrik/views/details/tmpl/[name of your template]

Attached the default, footer_1 (page 1), footer_2 (page 2) and custom_css files: rename it with .php

ps : position of the 2 footers needs to be improved.
 

Attachments

  • custom_css.txt
    3.7 KB · Views: 309
  • default.txt
    13 KB · Views: 285
  • footer_1.txt
    168 bytes · Views: 228
  • footer_2.txt
    168 bytes · Views: 214
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top