We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 39929
    • 113 Posts
    Greetings!
    I used fpdf to generate a file..my problem is that how to include snippet in require function.
    require('[[fpdf]]');
    


    fpdf is a php code I make as snippet. Is that possible? thanks in advance.
      • 1778
      • 659 Posts
      Assuming you're using Revo (not sure with Evo) Why don't you do
      require('path/to/your/php/code/mysnippet.php')
      

      If your snippet code is directly in modx you can copy/paste it in a static file, and make the snippet static...
      Hope this helps
      Cheers
        • 36404
        • 307 Posts
        hi,

        agree with anso, i do this quite often in evo when i need a script than can be used as a snippet and in a module for example
        the same for classes (i often use functions from the class a module rely on in a snippet) and it seems the easiest way to deal with this

        have swing
          réfléchir avant d'agir
          • 39929
          • 113 Posts
          are you saying in my file system?
            • 36404
            • 307 Posts
            hi,

            yes, in your case, it would probably be in the assets/snippets/ directory but, actually, you can put the file wherever it sounds logical to you and then call it in your require statement with a simple
            require_once MODX_BASE_PATH."assets/snippets/your_snippet_directory/your_file.php";

            for example

            by the way, this is very useful in many occasions when you need functions to be declared in your snippet
            a snippet has great chances to be called many times in a page/ressouce and this would cause a function re-declaration error, using require_once and a distant file will avoid this

            have swing
              réfléchir avant d'agir
              • 39929
              • 113 Posts
              thank you sir. but I've got this error

              Warning: require_once(MODX_BASE_PATHassets

              Fatal error: require_once() [function.require]: Failed opening required 'MODX_BASE_PATH

              Did I put my snippet in a wrong place?

              By the way I use link path and declare it this way

              <li><a href="assets/link/to/myfile/filename.php" target="_blank">Text here</a></li>
              </ul>
              


              and my filename.php is
              <?php
              require_once MODX_BASE_PATH."assets/directory/pdf.php";
              
              [ed. note: claro_santiago last edited this post 14 years, 3 months ago.]
                • 3749
                • 24,544 Posts
                I'm not 100% clear on what you're doing, but I think what you want to do is put your code in a MODX snippet (call it MakePdf). Have your code create the .pdf file and return the URL to it.

                Then on your page:

                Revolution:
                <li><a href="[[!MakePdf]]" target="_blank">Text here</a></li>


                Evolution
                <li><a href="[!MakePdf!]" target="_blank">Text here</a></li>



                If you'll be creating multiple .pdf files with the snippet, you can send information to the snippet by using properties in the snippet tag.


                ------------------------------------------------------------------------------------------
                PLEASE, PLEASE specify the version of MODX you are using.
                MODX info for everyone: http://bobsguides.com/modx.html
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 39929
                  • 113 Posts
                  Greetings to you Sir Bob, I'm so sorry for that..I'm using Modx revo and just upgraded to 2.2.4..

                  <li><a href="assets/pdf_folder/my_pdf1.php" target="_blank">text here</a></li>
                  


                  and in my my_pdf1.php, I have

                  require('assets/pdf_folder/pdf.php');
                  
                  and it has 
                  
                  $pdf->PrintChapter(1,'','math.txt');
                  
                  it its latter part.
                  


                  my problem is where to put these stuffs and connect them all:

                  math.txt
                  my_pdf.php
                  pdf.php
                  


                  thank you . [ed. note: claro_santiago last edited this post 14 years, 3 months ago.]
                    • 3749
                    • 24,544 Posts
                    If you create a MODX snippet that contains the code of the my_pdf1.php file and change the 'include' statement to give the path to the other file, it should work as I suggested above. The second file can go anywhere as long as you get the path right.

                    I'm not sure about the math.txt file, but there's good chance that it will work if it's in the same directory as the other file.



                    ------------------------------------------------------------------------------------------
                    PLEASE, PLEASE specify the version of MODX you are using.
                    MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 3 months ago.]
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 39929
                      • 113 Posts
                      I tried this code and it went well:
                      <li><a href="[[!MakePdf]]" target="_blank">Text here</a></li>
                      


                      the problem is modx can't find math.txt even if there are in the same directory.

                      Warning: file_get_contents(math.txt) [function.file-get-contents]: failed to open stream: No such file or directory in ....