在FPDF中将页面方向从右向左更改


changing page direction to right-to-left in FPDF

我使用php和reportico开发了一个基于web的报告应用程序。我想用波斯语(波斯语)做报告,所以我需要改变PDF文件的方向(从左到右,从rght到左)。在FPDF中有任何支持吗,或者在reportico中有任何解决方案吗?

看到这个URL,我觉得它非常有用。

http://www.fpdf.de/downloads/addons/31/

或者试试。

说明:此扩展允许打印旋转和剪切(即像斜体一样扭曲)的文本。

TextWithDirection(浮点x,浮点y,字符串txt[,字符串方向])

x: 横坐标

y: 纵坐标

txt:文本字符串

方向:以下值之一(默认为R):

  • R(右):从左到右
  • U(向上):从下到上
  • D(向下):从上到下
  • L(左):从右到左

TextWithRotation(float x,float y,string txt,float txt_angle[,float font_angle])

x: 横坐标

y: 纵坐标

txt:文本字符串

txt_angle:文本的角度

font_angle:剪切角(默认为0)

来源

 <?php
require('fpdf.php');
class RPDF extends FPDF {
function TextWithDirection($x, $y, $txt, $direction='R')
{
    $txt=str_replace(')', ''')', str_replace('(', '''(', str_replace('''', '''''', $txt)));
    if ($direction=='R')
        $s=sprintf('BT %.2f %.2f %.2f %.2f %.2f %.2f Tm (%s) Tj ET', 1, 0, 0, 1, $x*$this->k, ($this->h-$y)*$this->k, $txt);
    elseif ($direction=='L')
        $s=sprintf('BT %.2f %.2f %.2f %.2f %.2f %.2f Tm (%s) Tj ET', -1, 0, 0, -1, $x*$this->k, ($this->h-$y)*$this->k, $txt);
    elseif ($direction=='U')
        $s=sprintf('BT %.2f %.2f %.2f %.2f %.2f %.2f Tm (%s) Tj ET', 0, 1, -1, 0, $x*$this->k, ($this->h-$y)*$this->k, $txt);
    elseif ($direction=='D')
        $s=sprintf('BT %.2f %.2f %.2f %.2f %.2f %.2f Tm (%s) Tj ET', 0, -1, 1, 0, $x*$this->k, ($this->h-$y)*$this->k, $txt);
    else
        $s=sprintf('BT %.2f %.2f Td (%s) Tj ET', $x*$this->k, ($this->h-$y)*$this->k, $txt);
    if ($this->ColorFlag)
        $s='q '.$this->TextColor.' '.$s.' Q';
    $this->_out($s);
}
function TextWithRotation($x, $y, $txt, $txt_angle, $font_angle=0)
{
    $txt=str_replace(')', ''')', str_replace('(', '''(', str_replace('''', '''''', $txt)));
    $font_angle+=90+$txt_angle;
    $txt_angle*=M_PI/180;
    $font_angle*=M_PI/180;
    $txt_dx=cos($txt_angle);
    $txt_dy=sin($txt_angle);
    $font_dx=cos($font_angle);
    $font_dy=sin($font_angle);
    $s=sprintf('BT %.2f %.2f %.2f %.2f %.2f %.2f Tm (%s) Tj ET',
             $txt_dx, $txt_dy, $font_dx, $font_dy,
             $x*$this->k, ($this->h-$y)*$this->k, $txt);
    if ($this->ColorFlag)
        $s='q '.$this->TextColor.' '.$s.' Q';
    $this->_out($s);
}
}
?> 

示例

 <?php
define('FPDF_FONTPATH', 'font/');
require('rpdf.php');
$pdf=new RPDF();
$pdf->Open();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 40);
$pdf->TextWithRotation(50, 65, 'Hello', 45, -45);
$pdf->SetFontSize(30);
$pdf->TextWithDirection(110, 50, 'world!', 'L');
$pdf->TextWithDirection(110, 50, 'world!', 'U');
$pdf->TextWithDirection(110, 50, 'world!', 'R');
$pdf->TextWithDirection(110, 50, 'world!', 'D');
$pdf->Output();
?> 

输出:-http://www.fpdf.org/en/script/ex31.pdf

这可能不是最好的解决方案,但你可以随时更改方法本身,因为它使用的是Cell方法(支持对齐),你只需要在方法上执行$align参数,然后更改打印文本的所有行。

(有六个地方必须更换,最后部分有点棘手)

此外,请考虑我刚刚做了更改,您可能需要修复一些错误,但以下是Write方法最终的样子,

function Write($h, $txt, $link='', $align='L')
{
    // Output text in flowing mode
    $cw = &$this->CurrentFont['cw'];
    $w = $this->w-$this->rMargin-$this->x;
    $wmax = ($w-2*$this->cMargin);
    $s = str_replace("'r",'',$txt);
    if ($this->unifontSubset) {
        $nb = mb_strlen($s, 'UTF-8');
        if($nb==1 && $s==" ") {
            $this->x += $this->GetStringWidth($s);
            return;
        }
    }
    else {
        $nb = strlen($s);
    }
    $sep = -1;
    $i = 0;
    $j = 0;
    $l = 0;
    $nl = 1;
    while($i<$nb)
    {
        // Get next character
        if ($this->unifontSubset) {
            $c = mb_substr($s,$i,1,'UTF-8');
        }
        else {
            $c = $s[$i];
        }
        if($c=="'n")
        {
            // Explicit line break
            if ($this->unifontSubset) {
                $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,$align,0,$link);
            }
            else {
                $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,$align,0,$link);
            }
            $i++;
            $sep = -1;
            $j = $i;
            $l = 0;
            if($nl==1)
            {
                $this->x = $this->lMargin;
                $w = $this->w-$this->rMargin-$this->x;
                $wmax = ($w-2*$this->cMargin);
            }
            $nl++;
            continue;
        }
        if($c==' ')
            $sep = $i;
        if ($this->unifontSubset) { $l += $this->GetStringWidth($c); }
        else { $l += $cw[$c]*$this->FontSize/1000; }
        if($l>$wmax)
        {
            // Automatic line break
            if($sep==-1)
            {
                if($this->x>$this->lMargin)
                {
                    // Move to next line
                    $this->x = $this->lMargin;
                    $this->y += $h;
                    $w = $this->w-$this->rMargin-$this->x;
                    $wmax = ($w-2*$this->cMargin);
                    $i++;
                    $nl++;
                    continue;
                }
                if($i==$j)
                    $i++;
                if ($this->unifontSubset) {
                    $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,$align,0,$link);
                }
                else {
                    $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,$align,0,$link);
                }
            }
            else
            {
                if ($this->unifontSubset) {
                    $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'UTF-8'),0,2,$align,0,$link);
                }
                else {
                    $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,$align,0,$link);
                }
                $i = $sep+1;
            }
            $sep = -1;
            $j = $i;
            $l = 0;
            if($nl==1)
            {
                $this->x = $this->lMargin;
                $w = $this->w-$this->rMargin-$this->x;
                $wmax = ($w-2*$this->cMargin);
            }
            $nl++;
        }
        else
            $i++;
    }
    // Last chunk
    if($i!=$j) {
        if ($this->unifontSubset) {
            $this->Cell($w, $h,mb_substr($s,$j,$i-$j,'UTF-8'),0,0,$align,0,$link);
            // $this->Cell($l, $h,mb_substr($s,$j,$i-$j,'UTF-8'),0,0,'',0,$link);
        }
        else {
            $this->Cell($w, $h,substr($s,$j),0,0,$align,0,$link);
            // $this->Cell($l, $h,substr($s,$j),0,0,'',0,$link);
        }
    }
}