PHPで日本語のPDF(ついでに回転も)

最終的には縦書きをしたいので、その前準備。

初めに結果

  • はろーわーるど的なもの
<?php

require('yoyapdf.php');

$pdf=new YoyaPDF();
$pdf->AddSJISFont();
$pdf->AddPage();
$pdf->SetFont('SJIS','',18);
$pdf->RotatedImage('circle.png',85,60,50,16,45);
$pdf->RotatedText(100,60,'こんにちわ!',45);
$pdf->Output();

fpdf.php

$ apt-cache search fpdf
php-fpdf - PHP class to generate PDF files
$ sudo apt-get install php-fpdf
  • 普通にインストール

japanese.php

  • そのままだとプログラムを SJIS で書かなければならぬので、UTF-8 で書けるように mb_convert_encoding をかます事にする。
    function Text($x,$y,$txt) {
        $txt = mb_convert_encoding($txt, "CP932", "UTF-8"); // ☆
        parent::Text($x,$y,$txt);
    }
}

rotation.php

  • extends PDF を extends PDF_Japanese にして日本語対応
<?php
require('japanese.php');

class PDF_Rotate extends PDF_Japanese // ☆
{

YoyaPDF

好き勝手な改造を入れられるように、もう一段ライブラリを噛ませる。

<?php
require('rotation_japanese.php');

class YoyaPDF extends PDF_Rotate
{
    function RotatedText($x,$y,$txt,$angle)
    {
        //Text rotated around its origin
        $this->Rotate($angle,$x,$y);
        $this->Text($x,$y,$txt);
        $this->Rotate(0);
    }

    function RotatedImage($file,$x,$y,$w,$h,$angle)
    {
        //Image rotated around its upper-left corner
        $this->Rotate($angle,$x,$y);
        $this->Image($file,$x,$y,$w,$h);
        $this->Rotate(0);
    }
    // more implementation.
}

仕方ないので、文字ごとに回転させる縦書きメソッドをここに入れる予定。
句読点が悩ましいが。。(文字をただ回転させても位置が変になる)

でも実は。。。

  • japanese は出来が悪いみたいで、mbfpdf の方が良いらしい。まぁ、限界を感じたらそっち使う。