We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27705
    • 38 Posts
    I am having trouble integrating FPDF with modx.

    I have a snippet with this code:
    include_once('http://www.smart-avi.net/fpdf/fpdf.php');


    But when I run the snippet, its giving me the error:

    Fatal error: Class ’FPDF’ not found in /home/smart/public_html/core/cache/elements/modsnippet/63.include.cache.php on line 9


    What’s the easiest way to reference a PHP library?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Are you sure that the include is successful? Not all PHP configurations allow includes from URLs. Usually an include using a file path rather than a URL is more likely to be successful.

        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 27705
        • 38 Posts
        I switched it to this statement:
        require_once($modx->config['base_path']."core/components/signup/fpdf/fpdf.php");
        


        Based on what I read in the Creating Snippets documentation and that seemed to work (I moved file to different directory also). The snippet still doesn’t work, but I think thats probably just a problem on my part.

        -----

        Update:

        I’m not so sure the code is executing anymore. I’ve tested the php in the snippet outside of modx and it works perfectly. Is there some reason why this wouldn’t work in a snippet?
        require_once($modx->config['base_path']."core/components/signup/fpdf/fpdf.php");
        
        $pdf=new FPDF();
        $pdf->AddPage();
        $pdf->SetFont('Arial','B',16);
        $pdf->Cell(40,10,'Dealer Application');
        $pdf->Output('partner.pdf', I);


          • 18913
          • 654 Posts
          According to the install docs for fpdf, you need to make sure it has the correct path set for the fonts. Are there any messages in an error log somewhere suggesting that’s the problem?
          Matt
            • 18913
            • 654 Posts
            Change your last line to this
            $pdf->Output('partner.pdf', 'F');

            after correctly pointing to the font directory,
            e.g.
            define('FPDF_FONTPATH',$modx->config['base_path'].'assets/snippets/fpdf16/font/');

            (Using Evo 1.0.5 with fpdf in assets/snippets/)

            Matt

              • 27705
              • 38 Posts
              I put this into the snippet

              require_once($modx->config['base_path']."core/components/signup/fpdf/fpdf.php");
              
              define('FPDF_FONTPATH',$modx->config['base_path']."core/components/signup/fpdf/font/");
              
              $pdf=new FPDF();
              $pdf->AddPage();
              $pdf->SetFont('Arial','B',16);
              $pdf->Cell(40,10,'Dealer Application');
              $pdf->Output("partner.pdf", F);


              It works! But for some reason trying to output it as a download gives me an error when Acrobat opens, and doing it inline displays nothing. The local file it saves is fine though. Guess I’ll just see if I can get it to send in an email.

              Thanks for all your help!
                • 27705
                • 38 Posts
                Got it to work for email.

                Here is the code for sending a pdf generated at runtime in a snippet using FormIt for user input, FPDF for PDF creation, and Swift Mailer to send the email with attachment.

                <?php
                
                //Include required files
                
                require_once($modx->config['base_path']."core/components/signup/fpdf/fpdf.php");
                require_once($modx->config['base_path']."core/components/signup/lib/swift_required.php");
                
                //Define FPDF fontpath
                
                define('FPDF_FONTPATH',$modx->config['base_path']."core/components/signup/fpdf/font/");
                
                //Create PDF
                
                $pdf=new FPDF();
                $pdf->AddPage();
                $pdf->SetFont('Arial','B',16);
                $pdf->Cell(40,10,'Dealer Application');
                $doc = $pdf->Output('', S);
                
                //Attachment details for generated PDF
                
                $attachment = Swift_Attachment::newInstance($doc, 'partner.pdf', 'application/pdf');
                
                //Sending the message
                
                $transport = Swift_SmtpTransport::newInstance('mail.domain.com', 25)
                ->setUsername('your username')
                ->setPassword('your password');
                
                $mailer = Swift_Mailer::newInstance($transport);
                
                //Message details
                
                $message = Swift_Message::newInstance()
                ->setSubject('Dealer Application')
                ->setFrom(array('[email protected]' => 'Administrator'))
                ->setTo($hook->getValue('email'))
                ->setBody('Thank you for filling our application! We will be in contact with you shortly.')
                ->attach($attachment);
                
                //Message results
                
                $result = $mailer->send($message);