SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    TCPDF and mysql - varying the data extracted

    Hi
    I have a table (applicant) and I am looking at producing a pdf displaying various items of data from this table.

    I have succeeded in getting the look I want but in my query I use a fixed applicantid (in this case an id of 4). What I want to be able to do is to have the id cary according to the url. In other words, when someone comes from one web page to this one and the url is of the format .../pdf_production.php?applicantid=42.

    I'm not sure I've explained that very well but I hope someone can help - I think this is the last thing I need to sort out.

    Many thanks in advance.

    The code I have so far is:

    PHP Code:
    <?php require_once('../Connections/process.php'); ?>
    <?php 
    require_once('../tcpdf/config/lang/eng.php');
    require_once(
    '../tcpdf/tcpdf.php');
         
    // get data from users table 

    mysql_select_db($database_process$process);
    $result mysql_query("SELECT * FROM applicant WHERE applicantid = '4'"); 

    while(
    $row mysql_fetch_array($result)) 
      { 
        
    $appid $row['applicantid']; 
        
    $idcode $row['idcode']; 
        
    $type $row['type']; 
        
    $company $row['company']; 
        
    $email $row['email']; 
      } 
       
    // create new PDF document 
    $pdf = new TCPDF(PDF_PAGE_ORIENTATIONPDF_UNITPDF_PAGE_FORMATtrue'UTF-8'false);  

    $pdf->SetPrintHeader(false); $pdf->SetPrintFooter(false); 

    // set default monospaced font 
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); 

    //set margins 
    $pdf->SetMargins(PDF_MARGIN_LEFTPDF_MARGIN_TOPPDF_MARGIN_RIGHT); 

    //set auto page breaks 
    $pdf->SetAutoPageBreak(TRUEPDF_MARGIN_BOTTOM); 

    //set image scale factor 
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);  

    //set some language-dependent strings 
    $pdf->setLanguageArray($l);  

    // --------------------------------------------------------- 

    // set font 
    $pdf->SetFont('dejavusans'''10); 

    // add a page 
    $pdf->AddPage(); 
    // create some HTML content 
    $txt = <<<EOD
    Below are the details I require

    Company type: 
    $type
    Company Name: 
    $company
    Company email: 
    $email

    EOD;


    // output the HTML content 
    // $pdf->writeHTML($htmlcontent, true, 0, true, 0); 
    $pdf->Write(0$txt''0'L'true0falsefalse0);

    // $pdf->writeHTML($inlinecss, true, 0, true, 0); 

    // reset pointer to the last page 
    // $pdf->lastPage(); 

    //Close and output PDF document 
    $pdf->Output('example_006.pdf''I'); 

    //============================================================+ 
    // END OF FILE                                                  
    //============================================================+ 
    ?>
    Last edited by SpacePhoenix; Feb 24, 2011 at 08:09. Reason: put php tags round php code to improve readability

  2. #2
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You need to read the URL extension called applicantid and fit that into your code

    PHP Code:
    <?php 
    require_once('../tcpdf/config/lang/eng.php');
    require_once(
    '../tcpdf/tcpdf.php');
     
    if(isset(
    $_GET['applicantid'])){
      
    $appidpassed $_GET['applicantid'];
    }else{
      
    $appidpassed 4// set this to your default value if no URL value is present
    }
     
     
    // get data from users table 
     
    mysql_select_db($database_process$process);
    $result mysql_query("SELECT * 
                        FROM applicant 
                        WHERE applicantid = '
    $appidpassed'
                        LIMIT 1
                        "
    ); 
     
    // rest of your code
    As you are only ever picking one record from the database you should really LIMIT your results to a single output on your query.

    This will also remove the need to have a while statement in the next piece of code, with only one dataset returned you can omit the while { }
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks. I'll try that later.

    Also, do you happen to know how I can amend the filename the pdf saves with to include (say) the applicatid?

  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes just play with the code at the bottom of your script

    PHP Code:
    //Close and output PDF document 
    $docname 'pdf_document_' $appidpassed '.pdf';
    $pdf->Output($docname'I'); 
    will call the doucment

    pdf_document_xx.pdf where xx is the number passed in the URL or the default value is no number was passed.

    You may need to expand this code to prevent two documents being saved with the same name, perhaps add a time stamp or other unique identifier to the name.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2011
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's brilliant - it all works fine.

    Sorry to be a pain but if I want to save the pdf to a location rather than see it in the browser (for example to a folder called documents) how to amend the Output line?

    Once again, very many thanks.

    Graham

  6. #6
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Im not familiar with the PD classs your using but in the first instance id try and just add a directory to the file name.

    You must make sure the the directory exists on the server before trying it out.

    PHP Code:
     //Close and output PDF document 
    $directory 'pdf_store\';
    $docname = '
    pdf_document_' . $appidpassed . '.pdf';
    $path = $directory . $docname;
    $pdf->Output($path, '
    I' ); 
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •