2011/03/27(日)insideなページ構築中 (pdfの作成)
注意
最終的にroot権限がないとこにインスコする必要があるため、PREFIX付けてます
ライブラリの更新だけはrootでやってますが(汗
PDF::API2のインスコ
準備
ExtUtils::MakeMakerの6.31以上が必要らしいので
rootでcpan使ってinstall ExtUtils::MakeMakerしておく
あと、Font::TTFも必要っぽいので落として入れておく
$ tar xvzf Font-TTF-0.48.tar.gz $ cd Font-TTF-0.48 $ perl Makefile PREFIX=~/lib $ make; make test; $ make install
PDF::API2
$ tar xvzf PDF-API2-2.019.tar.gz $ perl Makefile.PL PREFIX=~/public_html/inside/lib $ make; make test $ make install
make testでFont::TTFが無いと怒られるけど、標準パスに置いてないので当然
気にしないでinstall
使う
use PDF::API2;を忘れずに
基本
# 1. PDFを作る or 開く
$pdf = PDF::API2->new();
#$pdf = PDF::API2->open('my.pdf');
# 2. ページを開く or 作る
#$page = $pdf->openpage(1); # 1ページ目
#$page = $pdf->openpage(-1); # 最後のページ
$page = $pdf->page(); # 新期ページ
# 3. いろいろする
# サンプル参考に
# 4. 保存する or 名前を付けて保存する or 更新する or textではき出す
#$pdf->save();
$pdf->saveas('new.pdf');
#$pdf->update();
#$pdf->stringify();
# 5. pdfを閉じる
$pdf->end();
サンプル
文字を書く
use PDF::API2;
my $pdf;
my $page;
my $font;
my $text;
$pdf = PDF::API2->new();
$page = $pdf->page();
$page->mediabox('A4');
$font = $pdf->corefont('Times-Roman');
$text = $page->text();
$text->font($font, 16);
$text->translate(10,300); # 左下が基準っぽい
$text->text("sample pdf");
$font = $pdf->cjkfont('KozMin');
$text = $page->text();
$text->font($font, 16);
$text->translate(10,275);
$text->text("サンプル②");
# ttf使うと、fontが埋め込まれます
# その分ファイルサイズが大きくなるので注意
$font = $pdf->ttfont('./font/ipaexg.ttf', -encode=>'euc-jp');
$text = $page->text();
$text->font($font, 16);
$text->translate(10,250);
$text->text("サンプルⅢ");
$pdf->safeas('test.pdf');
$pdf->end();
exit();
他
座標 1
ポイント単位なので
sub mm2pt(){
return $_[0] / 25.4 * 72;
}
とか用意しておくと、幸せになれるかもしれない
座標 2
用紙のサイズ(ポイント)は$page->get_mediaboxで取得できるので、
(undef, undef, $x, $y) = $page->get_mediabox;
でとれるので上からの10mmの座標は
($page->get_mediabox)[4] - &mm2pt(10)
になる
参考サイト
- 元
- http://search.cpan.org/~ssimms/PDF-API2/
- 基本的な使い方
- http://pdfapi2.sourceforge.net/pdfapi2_for_fun_and_profit_APW2005.pdf
- http://search.cpan.org/~ssimms/PDF-API2/lib/PDF/API2.pm
- http://d.hatena.ne.jp/end0tknr/20090531/1243757247
- $textとかコンテンツ回り
- http://search.cpan.org/~ssimms/PDF-API2/lib/PDF/API2/Content.pm
- corefont
- http://search.cpan.org/~ssimms/PDF-API2/lib/PDF/API2/Resource/Font/CoreFont.pm