Hi,

Does anyone know how to rotate text by 90degrees using FPDF.org?

Please help me!!

    3 months later

    edraynham,

    try this.

    class PDF extends FPDF {
    
    function Rotate($angle,$x=-1,$y=-1) {
    
        if($x==-1)
            $x=$this->x;
        if($y==-1)
            $y=$this->y;
        if($this->angle!=0)
            $this->_out('Q');
        $this->angle=$angle;
        if($angle!=0)
    
        {
            $angle*=M_PI/180;
            $c=cos($angle);
            $s=sin($angle);
            $cx=$x*$this->k;
            $cy=($this->h-$y)*$this->k;
    
            $this->_out(sprintf('q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
        }
    }
    }
    
    $pdf=new PDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','',10);
    $pdf->Write(20,this text is straight);
    $this->Rotate(10);
    $pdf->Write(20,this text is crooked);
    $this->Rotate(0);
    $pdf->Write(20,this text is straight again);
    $pdf->Output();

    I didn't write the function, I found it somewhere and it's working just fine for me. Let me know whether it helps.

      4 years later

      This is a typical "register by one post":
      🆒
      Of course that works!
      If you still alive take a frozen margarita to your salute...
      I.-

        NomikOS;10951930 wrote:

        This is a typical "register by one post":
        🆒
        Of course that works!
        If you still alive take a frozen margarita to your salute...
        I.-

        Salud! Glad to have been of assistance. 🙂

          2 years later

          Guys, wanted to give a heads-up: you MUST call Rotate(0) at the end of every page to output a matching 'Q' command, otherwise you end up with a broken PDF (which is unnoticeable in OS X Preview.app and also unnoticeable for single-page document, but as soon as you have 2+ pages, Adobe Reader starts complaining).

          The example gets this right, but it's not otherwise obvious from the code.

          Basically 'q' and 'Q' must be balanced in PDFs (those are “push state” and “pop state” commands). Just spent 2 hours to find this out.

            2 months later

            Many thanks to ergot, for the great Rotate function. I had to make a few corrections to make it work for me, and I'll detail those here for others who may be having trouble -

            • change $this-> to $pdf-> in the main code (not within the function!)
            • if you've been using: $pdf = new FPDF(), change to $pdf = new PDF();
            • be sure to put quotes around the text such as -
              $pdf->Write(20,'this text is crooked');

            Thanks again!

              Sorry, but I don't see an "Edit" link -

              Also, remember that your X and Y will be reversed when using Rotate(). Rather obvious, but I wasted an hour trying to place text near the right margin and kept having it disappear.

                2 years later

                Didn't work for me, I applied the changes suggested by compusolver, to no avail. At last I got it working. I paste the fixed code:

                class PDF extends FPDF {
                
                public $angle = 0;
                
                function Rotate($angle,$x=-1,$y=-1) {
                
                    if($x==-1)
                        $x=$this->x;
                    if($y==-1)
                        $y=$this->y;
                    if($this->angle!=0)
                        $this->_out('Q');
                    $this->angle=$angle;
                    if($angle!=0)
                
                    {
                        $angle*=M_PI/180;
                        $c=cos($angle);
                        $s=sin($angle);
                        $cx=$x*$this->k;
                        $cy=($this->h-$y)*$this->k;
                
                        $this->_out(sprintf('q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
                    }
                }
                }
                
                $pdf=new PDF();
                $pdf->AddPage();
                $pdf->SetFont('Arial','',10);
                $pdf->Write(20,'this text is straight');
                $pdf->Rotate(10);
                $pdf->Write(20,'this text is crooked');
                $pdf->Rotate(0);
                $pdf->Write(20,'this text is straight again');
                $pdf->Output();
                  4 years later

                  Hi, I used the exactly code, it work in localhost, but when I upload into server. it appear
                  Fatal error: Call to undefined method FPDF::RotatedText()

                  My server is running in window O/S

                    Write a Reply...