http://bakery.cakephp.org/articles/view/225」を参考に日本語対応のFPDF helperを作ってみた。


FPDFは /vendors/fpdf/ へ FPDIは /vendors/fpdi/ へ置き、requre(...) は適宜 vendor(...)で置き換える。FPDFサイト内の「What languages can I use?」の japanese.zip もダウンロードし、 /vendors/fpdf/ へ置いておく。

/vendors/fpdf/japanese.php の頭の部分は以下のように置き換えた。

<?php
//require('fpdf.php');
vendor('fpdf/fpdf');
vendor('fpdi/fpdi');

//class PDF_Japanese extends FPDF
class PDF_Japanese extends FPDI
{
    var $SJIS_widths = array(
        ' '=>278,'!'=>299,'"'=>353,'#'=>614,'$'=>614,'%'=>721,'&'=>735,'\''=>216,
    	'('=>323,')'=>323,'*'=>449,'+'=>529,','=>219,'-'=>306,'.'=>219,'/'=>453,'0'=>614,'1'=>614,
    	'2'=>614,'3'=>614,'4'=>614,'5'=>614,'6'=>614,'7'=>614,'8'=>614,'9'=>614,':'=>219,';'=>219,
    	'<'=>529,'='=>529,'>'=>529,'?'=>486,'@'=>744,'A'=>646,'B'=>604,'C'=>617,'D'=>681,'E'=>567,
    	'F'=>537,'G'=>647,'H'=>738,'I'=>320,'J'=>433,'K'=>637,'L'=>566,'M'=>904,'N'=>710,'O'=>716,
    	'P'=>605,'Q'=>716,'R'=>623,'S'=>517,'T'=>601,'U'=>690,'V'=>668,'W'=>990,'X'=>681,'Y'=>634,
    	'Z'=>578,'['=>316,'\\'=>614,']'=>316,'^'=>529,'_'=>500,'`'=>387,'a'=>509,'b'=>566,'c'=>478,
    	'd'=>565,'e'=>503,'f'=>337,'g'=>549,'h'=>580,'i'=>275,'j'=>266,'k'=>544,'l'=>276,'m'=>854,
    	'n'=>579,'o'=>550,'p'=>578,'q'=>566,'r'=>410,'s'=>444,'t'=>340,'u'=>575,'v'=>512,'w'=>760,
    	'x'=>503,'y'=>529,'z'=>453,'{'=>326,'|'=>380,'}'=>326,'~'=>387);

// (中略)

    function AddSJISFont($family='SJIS')
    {
    	//Add SJIS font with proportional Latin
    	$name='KozMinPro-Regular-Acro';
    	//$cw=$GLOBALS['SJIS_widths'];
    	$CMap='90msp-RKSJ-H';
    	$registry=array('ordering'=>'Japan1','supplement'=>2);
    	$this->AddCIDFonts($family,$name,$this->SJIS_widths,$CMap,$registry);
    }

//(続く)

で、ヘルパー /app/views/helpers/fpdf.php

<?php 
vendor('fpdf/japanese');

if (!defined('PARAGRAPH_STRING')) define('PARAGRAPH_STRING', '~~~');

define('FPDF_FONTPATH', VENDORS . 'fpdf/font/');
define('FPDFHELPER_INTERNAL_ENCODING', 'UTF-8');

class fpdfHelper extends PDF_Japanese {
    
    public $helpers = array();
    
    function setup($orientation='P',$unit='mm',$format='A4') {
        $this->PDF_Japanese($orientation, $unit, $format);
    }
    
    function fpdfOutput ($name = 'page.pdf', $destination = 's') {
        return $this->Output($name, $destination);
    }

    function Cell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='') {
        parent::Cell($w,$h,$this->conv_sjis($txt),$border,$ln,$align,$fill,$link);
    }
    
    function Text($x,$y,$txt) {
        parent::Text($x,$y,$this->conv_sjis($txt));
    }

    function conv_sjis($txt) {
        return mb_convert_encoding($txt, "SJIS-win", FPDFHELPER_INTERNAL_ENCODING);
    }
}
?> 

japanese.phpSJISのみの対応とのことなので、ヘルパー内で文字コード変換を適宜行う。これはjapanese.phpを書き換えたほうがスマートなので、そのうち作業する。


/app/views/layouts/pdf.thtml

<?php
header("Content-type: application/pdf");
//header("Content-type: text/html");
echo $content_for_layout;
?>

コントローラサンプル /app/controllers/tests_controller.php

<?php 
    class TestsController extends AppController 
    {
        public $name    = 'Tests';
        public $helpers = array('Fpdf'); // this will use the pdf.php class
        public $uses    = array();
        
        function index()
        {
            $this->layout = 'pdf'; //this will use the pdf.thtml layout
            $this->set('data','ハローワールド hello world!');
            $this->render();
        }
    }
?> 

ビューサンプル /app/views/tests/index.thtml

<?php
    $fpdf->AddSJISFont();
    $fpdf->Open();
    $fpdf->AddPage();
    $fpdf->SetFont('SJIS','',16);
    $fpdf->Cell(40,10,$data);
    $fpdf->Text(0, 10, "テスト");
    echo $fpdf->fpdfOutput();
?>

動作確認環境は次のとおり。