We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34120
    • 236 Posts
    Is this possible?

    I’m redeveloping an existing site using modx. I have a link that passes the ID in the url to a php script generate-pdf.php.
    <a href="assets/pdfgen/generate-pdf.php?id=[*id*]" target="_blank">Download PDF datasheet</a>

    The php script has been created outside of modx. So when I try to access variables in this script using getTemplateVar() I get a fatal error "Call to a member function on a non-object"

    The alternative would be to move the contents of generate-pdf.php to a snippet. The problem with this is I’m not sure how to then run the snippet and pass the id from a link.

    Evolution 1.0.0

    Any ideas much appreciated
    Thanks
      • 4041
      • 788 Posts
      Something like this might work:

      Create a new page to hold the get_pdf snippet call, this is the page you will direct the user to from the link you create. ex: [!get_pdf!]

      Make your get_pdf snippet something like this:
      <?php
      /* your get_pdf code
      */
      $output ='';
      $modx_id = isset($_GET['mid']) ? $_GET['mid'] : '';
      
      
      if(!empty($modx_id)){
      
      // if you made it this far, you should have a valid document id 
      // to use in further processing: $modx_id
      
      $output .='<p>The page you came from is '.$modx_id.'</p>';
      
      return $output;
      }
      ?>


      Then your link would look something like this (replacing the XX with the document id which you created to hold the snippet call):
      w/o friendly urls:
      <a href="index.php?id=XX&mid=[*id*]" target="_blank">

      with friendly urls:
      <a href="[~XX~]?mid=[*id*]" target="_blank">

      Hope this helps smiley
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 34120
        • 236 Posts
        Thanks so much for taking the time to do that Breezer, it works great. Unfortunately I now have another related problem.

        Using your code I can now pass the id to the snippet and trigger the PDF code. The problem is instead of producing a PDF it outputs the contents of the PDF to the browser. This is because the PDF script is being called from within an HTML page I think, see here, http://www.fpdf.org/en/FAQ.php#q2

        Using the previous method (php script outside of modx) the PDF is created fine, but I have the problem of passing/accessing TVs from this script.

        So my options are:

        1. Back to original plan, external PHP file and try to get modx API working in that file so I can access TV’s

        2. Write my own PHP file to connect to the database directly to get values I need. I’m currently trying this but have just descended into SQL hell.

        3. Go to the pub.

        Any bright ideas welcome.
        thanks!



          • 4041
          • 788 Posts
          Go and edit the document properties on pdf landing page you have the snippet call on, make the template (blank) and set the content type to application/pdf and see if that helps.
            xforum
            http://frsbuilders.net (under construction) forum for evolution
            • 34120
            • 236 Posts
            Breezer!! massive thanks. That has got it working.

            Now I’ve got it working I’ve just discovered a snippet that does something very similar. I guess I should check it out.

            Many thanks for all the help.