Roots Discourse

Cannot declare class for library, because the name is already in use fatal error

sage9
#1

Hi All,

I’m trying to use a library called PDFCrowd for generating a PDF of a page on my site. I’ve used it loads in the past without a problem, but I’m struggling including it in Sage.

The library I’m using is here - https://pdfcrowd.com/doc/api/html-to-pdf/php/

I’ve written a static function in my App controller:

public static function do_pdf()
    {
        $path = get_theme_root() . '/my_theme/';
        include_once($path . 'pdfcrowd.php');
    }

But I’m constantly getting the error: Fatal error: Cannot declare class PdfCrowd, because the name is already in use in /Users/mynname/Sites/my_theme/public_html_sg/wp-content/themes/jewson_kitchens/pdfcrowd.php on line 49

I’m not loading this requiring or including this library anywhere else so I’m confused why I’m getting this error.

Thanks
Kevin

0 Likes

#2

Edit: my answer below is probably not valid since you are using include_once which I missed when I read the question.

I am guessing that this is unrelated to Sage but anyways: are you (or soberwp/controller) maybe calling do_pdf() multiple times? If so, the error occurs on the second call to the function.

There are probably multiple solutions to this but the quickest and most dirty solution is probably to wrap the include call in a conditional check in which you use https://php.net/manual/en/function.class-exists.php or use

0 Likes

#3

My bad!!! Just realised I’ve got a use at the top of my app controller which is loading pdfcrowd from a plugin - one of my previous attempts to solve the problem.

0 Likes

#4

It looks like PDFCrowd can be installed via Composer: https://pdfcrowd.com/doc/api/client-library/php/

composer require pdfcrowd/pdfcrowd

Should be all you need to do to make the API available to your theme.

0 Likes