SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Shropshire
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fpdf - various help needed

    I have FPDF working fine using information from a database and have finally got more than one image to display per page (one per product).

    The problem I initially had with the images was lining them up with the relvant product information and not overlapping. I have resolved this by calculating Y and placing the images accordingly. However, as I don't know how much text there is going to be per product, the image placement is now causing other problems that I cannot work out how to resolve:

    1. If an image's height is more than the height of the content text then it overlaps with the next product heading.

    2. It appears that my calculation of GetY() and SetY() is now causing a product's text to overlap the product above if it is more than the Y height I have set.

    3. I also need to find out how to calculate the total height of all a product's information (hopefully related to the image problem above) so that if it is going to overlap 2 pages I can force a page break before the product heading.

    The pdf class I am using is:
    PHP Code:
    <?php

    class PDF extends FPDF
    {

        var 
    $_toc=array();
        var 
    $_numbering=false;
        var 
    $_numberingFooter=false;
        var 
    $_numPageNum=1;

        function 
    AddPage($orientation='') {
            
    parent::AddPage($orientation);
            if(
    $this->_numbering)
                
    $this->_numPageNum++;
        }

        function 
    startPageNums() {
            
    $this->_numbering=true;
            
    $this->_numberingFooter=true;
        }

        function 
    stopPageNums() {
            
    $this->_numbering=false;
        }

        function 
    numPageNo() {
            return 
    $this->_numPageNum;
        }

        function 
    TOC_Entry($txt,$level=0$dotted=0) {
            
    $this->_toc[]=array('t'=>$txt,'l'=>$level'1'=>$dotted'p'=>$this->numPageNo());
        }
        
        function 
    TOC_Entry2($txt,$level=0$dotted=0) {
            
    $this->_toc[]=array('t'=>$txt,'l'=>$level'1'=>$dotted);
        }
        function 
    insertTOC$location=1,
                            
    $labelSize=20,
                            
    $entrySize=10,
                            
    $tocfont='Times',
                            
    $label='Table of Contents'
                            
    ) {
            
    //make toc at end
            
    $this->stopPageNums();
            
    $this->AddPage();
            
    $tocstart=$this->page;

            
    $this->SetFont($tocfont,'B',$labelSize);
            
    $this->Cell(0,5,$label,0,1,'C');
            
    $this->Ln(10);
            
            
            foreach(
    $this->_toc as $t) {

                
    //Offset
                
    $level=$t['l'];
                
    $dotted=$t['1'];
                if(
    $level>0)
                    
    $this->Cell($level*8);
                
    $weight='';
                if(
    $level==0)
                    
    $weight='B';
                
    $str=$t['t'];
                
    $this->SetFont($tocfont,$weight,$entrySize);
                
    $strsize=$this->GetStringWidth($str);
                
    $this->Cell($strsize+2,$this->FontSize+2,$str);

                
    //Filling dots
                
    $this->SetFont($tocfont,'',$entrySize);
                
    $PageCellSize=$this->GetStringWidth($t['p'])+2;
                
    $w=$this->w-$this->lMargin-$this->rMargin-$PageCellSize-($level*8)-($strsize+2);
                if(
    $dotted>0)
                {
                
    $nb=$w/$this->GetStringWidth('.');
                
    $dots=str_repeat('.',$nb);
                }
                else 
                {
                
    $nb=$w/$this->GetStringWidth(' ');
                
    $dots=str_repeat(' ',$nb);
                }
                
    $this->Cell($w,$this->FontSize+2,$dots,0,0,'R');

                
    //Page number
                
    $this->Cell($PageCellSize,$this->FontSize+2,$t['p'],0,1,'R');
            }

            
    //grab it and move to selected location
            
    $n=$this->page;
            
    $n_toc $n $tocstart 1;
            
    $last = array();

            
    //store toc pages
            
    for($i $tocstart;$i <= $n;$i++)
                
    $last[]=$this->pages[$i];

            
    //move pages
            
    for($i=$tocstart 1;$i>=$location-1;$i--)
                
    $this->pages[$i+$n_toc]=$this->pages[$i];

            
    //Put toc pages at insert point
            
    for($i 0;$i $n_toc;$i++)
                
    $this->pages[$location $i]=$last[$i];
        }


    //Page header
    function Header()
    {
        
    $this->Image('../images/logo-header-pdf.jpg',159,0);
        
    $this->Ln(20);
    }

    //Page footer
    function Footer()
    {
        if(
    $this->_numberingFooter==false)
                return;
        
    $this->SetTextColor(255,255,255);
        
    $this->SetFont('Arial','',10);
        
    //Position at 1.5 cm from bottom
        
    $this->SetY(-17);
        
    $this->Image('../images/fp-pdf-footer.jpg',10,280);
        
    //Page number
        
    $this->Cell(0,10,$this->numPageNo(),0,0,'C');
     if(
    $this->_numbering==false)
                
    $this->_numberingFooter=false;
    }

    function 
    SubCat($subcat)
    {
        
    $this->SetFont('Arial','',14);
        
    $this->SetTextColor(255,255,255);
        
    $this->SetFillColor(0,108,176);
        
    $this->Cell(0,7,"$subcat",0,0,'L',1);
    }

    function 
    Cat($cat)
    {
        
    $this->SetFont('Arial','',14);
        
    $this->SetTextColor(255,204,51);
        
    //Background color
        
    $this->SetFillColor(0,108,176);
        
    $this->Cell(0,7,"$cat",0,1,'R',1);
        
    $this->Ln(4);
    }

    function 
    ProdName($title)
    {
        
    $this->SetFont('Arial','',12);
        
    $this->SetTextColor(0,108,176);
        
    $this->SetFillColor(200,220,255);
        
    $this->Cell(0,6,"$title",0,1,'L',1);
        
    $this->Ln(4);
    }

    function 
    ProdPartNo($part)
    {
        
    $this->SetFont('Arial','',11);
        
    $this->SetTextColor(0,0,0);
        
    $this->Cell(136,6,"$part",0,1,'L');
        
    $this->Ln(4);
    }

    function 
    ProdDesc($file)
    {
        
    $this->SetFont('Arial','',10);
        
    $this->SetTextColor(0,0,0);
        
    $this->MultiCell(136,5,$file);
        
    $this->Ln();
    }

    function 
    ProdFeat($features)
    {
        
    $this->SetFont('Arial','',10);
        
    $this->SetTextColor(0,0,0);
        
    $this->Cell(14);
        
    $this->MultiCell(122,5,$features);
        
    $this->Ln();
    }

    function 
    LineBreak($line)
    {
        
    $this->Cell(0,6,"$line",0,0,'');
    }


    function 
    PrintPage($subcat,$cat)
    {
        
    $this->SetMargins(10,10,10);
        
    $this->AddPage();
        
    $this->SubCat($subcat);
        
    $this->Cat($cat);
    }

    function 
    ProductName($title)
    {
        
    $this->ProdName($title);

    }

    function 
    ProductPart($part)
    {
        
    $this->ProdPartNo($part);
    }

    function 
    ProductDesc($file)
    {
        
    $this->ProdDesc($file);
    }

    function 
    ProductFeat($feat)
    {
        
    $this->ProdFeat($feat);
    }

    function 
    Line($lbr)
    {
        
    $this->LineBreak($lbr);
    }



    function 
    PrintCoverPage($file)
    {
        
    $this->SetMargins(0,0,0);
        
    $this->AddPage();
    }

    }


    ?>
    And the page pdf is created with:
    PHP Code:
    <?php
    require_once("../includes/functions.php");
    require_once(
    "../includes/image_class.php");
    require_once(
    "../includes/fpdf.php");
    require_once(
    "../includes/pdf.class.php");


    $pdf=new PDF();
    $pdf->SetFont('Arial','',12);

    $results 0;

            
    $objcategory = new category;
            
    $search_results=$objcategory->PDFsearch();
            
            
    $current '';
            while (
    $catRow=$objcategory->get_row())
            { 
                
    $pdf->startPageNums();
                if (
    $catRow['subcatname']=='')
                {
                
    $pdf->PrintPage('',stripslashes($catRow['catname']));
                
    $pdf->TOC_Entry(stripslashes($catRow['catname']), 01);
                
    $catid=$catRow['catid'];
                }
                else
                {    
                    
                    
    $pdf->PrintPage(stripslashes($catRow['subcatname']),stripslashes($catRow['catname']));
                    
    // $row['foo'] would be whatever value holds the subcat name
                        // that you use to print the subcat name to the pdf
                        // looks like its probably $catRow['subcatname']
                        
    if ($catRow['catname'] != $current) {
                            
    // this conditional will only be true when the subcat is not the same
                            // as it was on the previous iteration of the loop
                    
                    
                            // print the value to the pdf
                            
    $pdf->TOC_Entry2(stripslashes($catRow['catname']), 00);
                    
                            
    // we are updating the var since the subcat just changed
                            
    $current $catRow['catname'];
                        } else {
                            
    // we do nothing here. the subcat was the same value
                            // on the last iteration so we dont print it
                        
    }
                    
    $pdf->TOC_Entry(stripslashes($catRow['subcatname']), 11);
                    
    $catid=$catRow['subcatid'];
                }
                    
    $objproduct = new product;
                    
    $objproduct->catid=$catid;
                    
    $objproduct->search();
                    
                    while (
    $prodRow $objproduct->get_row())
                    {
                        
    $pdf->ProductName($prodRow['prodname']);
                        
    $y $pdf->GetY();
                        if (
    $prodRow['prodpart']!='')
                        {
                        
    $pdf->ProductPart('Part Number: '.$prodRow['prodpart']);
                        }
                        if (
    $prodRow['proddesc']!='')
                        {
                        
    $pdf->ProductDesc($prodRow['proddesc']);
                        }
                        if (
    $prodRow['prodfeat']!='')
                        {
                            
    $pdf->ProductFeat($prodRow['prodfeat']);
                        }
                            
    $imagenull;
                            
    $imgname=mysql_query("SELECT imgname FROM prodimgs WHERE prodid='".$prodRow['prodid']."' LIMIT 1");
                            while (
    $imgrow mysql_fetch_array ($imgname))
                            {
                            
    $image=$imgrow['imgname']; 
                            
    $pdf->SetY($y+60);
                            
    $pdf->Image('../images/products_thumb/tb_'.$image,160,$y);
                            }
                        }
            }


    $pdf->stopPageNums();
    //Generate and insert TOC at page 2
    $pdf->insertTOC(1);
    $pdf->Output();

    ?>
    Any help would be greatly appreciated as I have been trying to finish this project for over a month or so now and I can't seem to find the information / help I need.

  2. #2
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. I'll try to explain: before writing any description of the product, you store Y position into a variable ($y_init). Now you start writing text. At the end of writing you store the current Y into another variable ($y_text) and you reset Y to initial position ($y_init) and show the image. After image is displayed you get Y again ($y_image) and you compare Y position after text was written and after image had been displayed. If Y from text is greater then set new Y to that Y. The code will explain better.
    PHP Code:
    $y_init $pdf->GetY(); //before writing text
    /*
    You write text.
    */
    $y_text $pdf->GetY();

    $pdf->SetY($y_init);
    /*
    You display image here.
    */
    $y_image $pdf->GetY();
    if (
    $y_text>$y_image$pdf->SetY($y_text);
    /*
    Continue to generate PDF.
    */ 
    2. I hope 1. will resolve also 2.

    3. Check this: http://www.fpdf.org/en/script/script3.php. You could use function NbLines() to calculate total height of product description.

  3. #3
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Shropshire
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Julian

    Thank you for your help. I have got the product information to stop overlapping now, but I can't get the Y value of the bottom of the image to puch down the next product in the page if the current products details are not as tall as the image. Therefore the images are getting cut off if this is the case and the next product is still overlapping the image.

    I have tried your code in various ways and what is below is the best at the moment. I have tried many ways to calculate if the $x_image is greater than the $x_text as well but this doesn't seem to work either.

    PHP Code:
    while ($prodRow $objproduct->get_row())
                    {
                        
    $y_init $pdf->GetY();
                        
    $pdf->ProductName($prodRow['prodname']);
                        
    $y_text $pdf->GetY();
                            
    $imagenull;
                            
    $imgname=mysql_query("SELECT imgname FROM prodimgs WHERE prodid='".$prodRow['prodid']."' LIMIT 1");
                            while (
    $imgrow mysql_fetch_array ($imgname))
                            {
                            
    $image=$imgrow['imgname']; 
                            
    $pdf->SetY($y_init);
                            
    $pdf->Image('../images/products_thumb/tb_'.$image,160,$y_text);
                            
    $y_image $pdf->GetY();
                            if (
    $y_text>$y_image$pdf->SetY($y_text);
                            }
                            
                        if (
    $prodRow['prodpart']!='')
                        {
                        
    $pdf->ProductPart('Part Number: '.$prodRow['prodpart']);
                        }
                        if (
    $prodRow['proddesc']!='')
                        {
                        
    $pdf->ProductDesc($prodRow['proddesc']);
                        }
                        if (
    $prodRow['prodfeat']!='')
                        {
                            
    $pdf->ProductFeat($prodRow['prodfeat']);
                        }
                        
                        
                        } 
    Do you have any more suggestions please?

    I have also tried calcualting the Y value for all of the text buand puting the image after, but this just pushes the image down below the relevant product information and the images overlap with each other. It seems to me that if I could just get the calculation for the Y co-ordinate at the bottom of the image to work as needed, all would be resolved.

  4. #4
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    chilli11, I attach an image that shows you how I imagined it will look when I posted that code. From the code you posted I see that you can have more than one image for every article. If you can post a sketch of how the pdf will look it would be more usefull.
    Attached Images Attached Images

  5. #5
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Shropshire
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Julian

    Sorry for the delay in responding but I was waiting for your attachment to be authorised and then I forgot yesterday.

    Anyway, that is nearly what I am trying for but I can see where the problem is coming from now in that above each section of text with an image is the product name. I have modifed your image quickly to show you how it is laid out with the header and hopefully this will help.

    http://i12.photobucket.com/albums/a2...pdf-layout.jpg

    Thanks

  6. #6
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    I gave you an ideea that supports the layout I posted. Now, for you layout you shoul do something like this (I just give you an example, I am really busy now):

    PHP Code:
    /*
    Product name goes here.
    */

    $y_init $pdf->GetY();
    /*
    Print description, details about the product
    */
    $y_text $pdf->GetY(); // this is the Y where text ended

    $pdf->SetY($y_init); // now you go back to the Y where you started printing text (description, details about product)
    /*
    You must set the corresponding x;
    You display image here.
    */
    $y_image $pdf->GetY(); // this is the Y where image ended
    if ($y_text>$y_image$pdf->SetY($y_text); // if the Y where text ended printing > Y of image where ended printing then set current Y to text Y
    /*
    Continue to generate PDF.
    */ 
    I hope I was clear enough and this helps you. If something goes wrong post a link to a pdf.

  7. #7
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Shropshire
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I am not too sure about setting the corresponding X value but this is obviously key (as I have already tried the set Y and get Y in those positions in the code) and I will keep trying to see if I can work it out, thank you.

  8. #8
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chilli11 View Post
    Thanks, I am not too sure about setting the corresponding X value but this is obviously key (as I have already tried the set Y and get Y in those positions in the code) and I will keep trying to see if I can work it out, thank you.
    I hope it works!

  9. #9
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fpdf problem

    Hi
    is there anyone who can help me ?
    My problem is that i am not able to print a page written in fpdf+asp
    in LANDSCAPE.
    I know the addpage() command but it does not work.
    I am sure is my fall.
    Is there anyone who can make me see an example ?
    Thank you
    Mauro
    here is what i did !
    It works but the page is always Portrait

    Dim i,pdf
    Set pdf=CreateJsObject("FPDF")
    pdf.CreatePDF()
    pdf.SetPath("fpdf/")
    pdf.SetFont "Arial","",8
    pdf.Open()
    pdf.LoadModels("clienti_model") ' il nome del modello č specificato senza estensione.
    pdf.AddPage(L)
    pdf.SetFont "Arial","",8


    pdf.ln(4)
    pdf.SetFont "Times","B",8
    w_az=session("w_az")
    sql_cli = "SELECT * FROM anacli where cl_az='" & w_az & "'" & " order by cl_nome"
    Set rs_cli = Server.CreateObject("ADODB.Recordset")
    rs_cli.Open sql_cli,CON_RH,1,1
    rs_cli.cachesize=50
    max_rg=20
    conta_riga=40
    do while not rs_cli.eof
    if conta_riga>max_rg then
    if conta_riga<40 then
    pdf.addpage(L)
    end if
    pdf.Cell 45,4, "Nome" ,1,0
    pdf.Cell 45,4, "Indirizzo" ,1,0
    pdf.Cell 10,4, "Cap" ,1,0
    pdf.Cell 40,4, "Cittā" ,1,0
    pdf.Cell 40,4, "Riferimento" ,1,0
    pdf.ln(4)
    conta_riga=1
    end if
    pdf.Cell 45,4, mid(rs_cli("cl_nome"),1,20) ,"L" ,0
    pdf.Cell 45,4, mid(rs_cli("cl_indir"),1,20) ,"L",0
    pdf.Cell 10,4, rs_cli("cl_cap") ,"L",0
    pdf.Cell 40,4, mid(rs_cli("cl_citta"),1,20) ,"L",0
    pdf.Cell 40,4, rs_cli("cl_riferimento") ,"L,R",0
    pdf.ln(4)
    conta_riga=conta_riga+1
    rs_cli.movenext
    loop

    pdf.Close()
    pdf.Output()
    %>

  10. #10
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should call addpage("L") not addpage(L). I think there is the problem.

  11. #11
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Shropshire
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am hoping I can still get some help on this as no matter where or how I calculate and get the X value, I cannot resolve the problem of the images overlapping with the product below if it's text takes up less height.

    The link to view what happens is http://www.fullwood.echocd.co.uk/adm...-catalogue.php.

    I also cannot owrk out how to calculate if a products details and / or image are going to overlap the bottom of a page and if so force it onto the next page.

    Can anyone help please.

  12. #12
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Post the code.

  13. #13
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Shropshire
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, totally forgot about the code, does help!

    Anyway:
    PHP Code:
    $pdf=new PDF();
    $pdf->SetFont('Arial','',12);

    $results 0;

            
    $objcategory = new category;
            
    $search_results=$objcategory->PDFsearch();
            
            
    $current '';
            while (
    $catRow=$objcategory->get_row())
            { 
                
    $pdf->startPageNums();
                if (
    $catRow['subcatname']=='')
                {
                
    $pdf->PrintPage('',stripslashes($catRow['catname']));
                
    $pdf->TOC_Entry(stripslashes($catRow['catname']), 01);
                
    $catid=$catRow['catid'];
                }
                else
                {    
                    
                    
    $pdf->PrintPage(stripslashes($catRow['subcatname']),stripslashes($catRow['catname']));
                        if (
    $catRow['catname'] != $current) {
                            
    $pdf->TOC_Entry2(stripslashes($catRow['catname']), 00);
                            
    $current $catRow['catname'];
                        } else {
                        }
                    
    $pdf->TOC_Entry(stripslashes($catRow['subcatname']), 11);
                    
    $catid=$catRow['subcatid'];
                }
                    
    $objproduct = new product;
                    
    $objproduct->catid=$catid;
                    
    $objproduct->search();
                    
                    while (
    $prodRow $objproduct->get_row())
                    {
                        
                        
    $pdf->ProductName($prodRow['prodname']);
                            
                        
    $y_init $pdf->GetY();
                        
    $x_init $pdf->GetX();
                        if (
    $prodRow['prodpart']!='')
                        {
                        
    $pdf->ProductPart('Part Number: '.$prodRow['prodpart']);
                        }
                        if (
    $prodRow['proddesc']!='')
                        {
                        
    $pdf->ProductDesc($prodRow['proddesc']);
                        }
                        if (
    $prodRow['prodfeat']!='')
                        {
                            
    $pdf->ProductFeat($prodRow['prodfeat']);
                        }
                        
    $y_text $pdf->GetY();
                        
    $x_text $pdf->GetX();
                        
                        
    $imagenull;
                            
    $imgname=mysql_query("SELECT imgname FROM prodimgs WHERE prodid='".$prodRow['prodid']."' LIMIT 1");
                            while (
    $imgrow mysql_fetch_array ($imgname))
                            {
                            
    $image=$imgrow['imgname']; 
                            
    $pdf->SetY($y_init);
                            
    $pdf->SetX($x_init);
                            
    $pdf->Image('../images/products_thumb/tb_'.$image,160,$y_init);
                            
    $y_image $pdf->GetY();
                            
    $x_image $pdf->GetX();
                            if (
    $y_text>$y_image$pdf->SetY($y_text);
                            elseif (
    $x_text>$x_image$pdf->SetX($x_text);
                            }
                        
                        }
            }


    $pdf->stopPageNums();
    //Generate and insert TOC at page 2
    $pdf->insertTOC(1);
    $pdf->Output(); 

  14. #14
    SitePoint Addict
    Join Date
    Jan 2007
    Location
    Romania
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chilli11 View Post
    I am hoping I can still get some help on this as no matter where or how I calculate and get the X value, I cannot resolve the problem of the images overlapping with the product below if it's text takes up less height.
    I looked over the code and I found that after you insert the image in the pdf is possible that the y position will not change. I didn't try that, but you can. Print both y_text and y_image and see if are different. If are not different than you can sum the y_text and image height and voila... you found y_image and now you check:
    PHP Code:
    if ($y_text $y_image$pdf->SetY($y_image); 
    I don't know what to say about "the X value", I can't see the pdf.
    When trying to access http://www.fullwood.echocd.co.uk/adm...-catalogue.php I get FPDF error: Missing or incorrect image file: ../images/products_thumb/tb_265.jpg.

    Quote Originally Posted by chilli11 View Post
    I also cannot owrk out how to calculate if a products details and / or image are going to overlap the bottom of a page and if so force it onto the next page.

    Can anyone help please.
    Look into the fPDF source code. You will manage to make a function that will calculate how much space (height) is until you reach the bottom of the page. You already know current position and you have the y where bottom starts and you find how much space you have on the page.


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
  •