We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 15076
    • 43 Posts
    Hi all,

    I want to create a file that creates a pdf from a page. I know there are a two snippets available but I only need to insert the content in a pdf file, so I prefer writing one myself.

    I parse the id to be printed to a php file that should get the content of the id and put it in the pdf file. I can succesfully put the id in the pdf file, but I am struggling with getting the content from the database. I tried my query in the phpmyadmin interface and that is working, so it’s definitely in the way to acces the database. From the modx api, i use: $modx->db->query($query);, but I get the following error:

    Fatal error: Call to a member function query() on a non-object in C:\wamp\www\evo\assets\printpdf\printpdf2.php on line 9.

    I searched the forum and found that I should put global $modx; in the first lines. However this is not working...

    This is my code:

    [tt]
    <?php
    global $modx;
    require_once(’fpdf.php’);
    require_once(’../../modxapi.php’);

    echo $modx;
    $id=$_GET[’id’];
    $query= "SELECT content FROM `modx_site_content` WHERE id=".$id.";";
    $res = $modx->db->query($query);

    $pdf=new FPDF();
    $pdf->AddPage();
    $pdf->SetFont(’Arial’,’B’,16);
    $pdf->Cell(40,10,$res);
    $pdf->Output();
    ?>[/tt]

    Can someone help me to fix this error?

    Thanks in advance,
      • 22303 MODX Staff
      • 10,725 Posts
      Why are you not doing this inside a MODx Resource/Snippet?
        • 15076
        • 43 Posts
        Because when I put in in a snippet and call that from a page, fpdf doesn’t understand it.. it complains that there are 2 output abilities, one for pdf and one for the website
          • 15076
          • 43 Posts
          hmm by removing the echo statement in the snippet fpdf does work in a snippet:)