0

I need to use a library I found on Github in an existing component, but my near non-existent knowledge of NAMESPACE is making it impossible.

To merge PDF files, I'm using the library at https://github.com/gabrielchavezme/pdf-table-of-content. The standalone code that works is below. However, when used within my Joomla controller or CLI scripts, everything crashes when it interferes with Joomla.

How can I use this library from with Joomla?

UPDATE: For POC, I did try it outside the controller class, and of course it was a total failure, which is when I tried it at the top of a CLI script, and had the same kind of problems. In short, I don't know where I should be trying to put it into Joomla.

namespace GabrielChavez\PdfTableOfContent;
use GabrielChavez\PdfTableOfContent\Exceptions\FileNotFoundException;
use GabrielChavez\PdfTableOfContent\Exceptions\NoFilesDefinedException;
use GabrielChavez\PdfTableOfContent\PdfTableOfContent;
use TCPDI;

require_once ('libraries/pdfTOC/vendor/autoload.php');
$pdfMerge = new PdfTableOfContent();
$pdfMerge->add([
    'file' => '2018_01_08_RM_Exhibit_A.pdf',
    'title' => 'File 1',
    'id' => 1
]);
$pdfMerge->add([
    'file' => '2018_01_08_RM_Exhibit_B.pdf',
    'title' => 'File 2',
    'id' => 2
]);
$pdfMerge->merge('MyMergedPDF.pdf');
5
  • 1
    "Everything crashes" isn't helpful. Post the exact errors or other issues you're getting.
    – Sharky
    Commented Oct 14, 2021 at 17:48
  • It would also be helpful to see how this code is placed in your controller. Surely, you don't just place it outside the controller class?
    – Sharky
    Commented Oct 14, 2021 at 18:00
  • @Sharky Per my update, I did try that initially to "get started", but quickly saw that wasn't going to work, so tried it from a CLI script.
    – GDP
    Commented Oct 14, 2021 at 21:54
  • I think you might need to require the autoload.php before your use statements.
    – Lodder
    Commented Oct 15, 2021 at 4:33
  • Your update has not provided any valuable information. "Had the same kind of problems" isn't useful since you haven't mentioned what problems you were having in the first place.
    – Sharky
    Commented Oct 15, 2021 at 5:35

1 Answer 1

1

Without knowing what errors you're getting or what your Joomla code looks like I can only think of two potential issues.

First is the unnecessary and wrong use of namespace declaration. If you add it to your controller class file, your controller class will no longer be found by Joomla. Another issue could be related to relative paths. As good practice you should use absolute paths when including files. In Joomla use JPATH_* constants where possible and __DIR__ PHP constants elsewhere.

Below is a working example which assumes that PDF files are present in Joomla root directory and that the library is installed as a Joomla library extension in libraries directory.

defined('_JEXEC') or die;

use GabrielChavez\PdfTableOfContent\PdfTableOfContent;

require_once JPATH_LIBRARIES . '/pdftoc/vendor/autoload.php';

class MyComponentControllerPdf extends JControllerLegacy
{
    public function mergePdf()
    {
        $pdfMerge = new PdfTableOfContent();
        $pdfMerge->add([
            'file' => JPATH_ROOT . '/2018_01_08_RM_Exhibit_A.pdf.pdf',
            'title' => 'File 1',
            'id' => 1
        ]);
        $pdfMerge->add([
            'file' => JPATH_ROOT . '/2018_01_08_RM_Exhibit_B.pdf',
            'title' => 'File 2',
            'id' => 2
        ]);
        $pdfMerge->merge(JPATH_ROOT . '/MyMergedPDF.pdf');
    }
}
1
  • Thanks! I'll accept this as the correct answer because I can get it to work in a new installation of Joomla. However, using my custom component is still producing a plethora of problems.
    – GDP
    Commented Oct 15, 2021 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.