FPDF - PHP로 PDF 만들기 4

[adsense]

목차

이 예제는 이전 장에 대한 변형으로써, 텍스트를 여러 칼럼(다단)에 배치하는 방법을 보여줍니다.

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

class PDF extends FPDF
{
	//현재 칼럼
	var $col=0;
	//칼럼의 세로좌표 시작
	var $y0;

	function Header()
	{
		//페이지 헤더
		global $title;

		$this->SetFont('Arial','B',15);
		$w = $this->GetStringWidth($title)+6;
		$this->SetX((210-$w)/2);
		$this->SetDrawColor(0,80,180);
		$this->SetFillColor(230,230,0);
		$this->SetTextColor(220,50,50);
		$this->SetLineWidth(1);
		$this->Cell($w,9,$title,1,1,'C',1);
		$this->Ln(10);

		//세로좌표 저장
		$this->y0 = $this->GetY();
	}

	function Footer()
	{
		//페이지 푸터
		$this->SetY(-15);
		$this->SetFont('Arial','I',8);
		$this->SetTextColor(128);
		$this->Cell(0,10,'Page '.$this->PageNo(),0,0,'C');
	}

	function SetCol($col)
	{
		//주어진 칼럼의 위치 설정
		$this->col=$col;
		$x=10+$col*65;
		$this->SetLeftMargin($x);
		$this->SetX($x);
	}

	function AcceptPageBreak()
	{
		//자동페이지 나눔을 할 것인지 안할 것인지 결정하는 메소드
		if($this->col < 2)
		{
			//다음 칼럼으로 이동
			$this->SetCol($this->col+1);

			//세로좌표 설정
			$this->SetY($this->y0);

			//페이지 계속
			return false;
		}
		else
		{
			//첫번째 칼럼으로 되돌아감
			$this->SetCol(0);

			//페이지 나눔
			return true;
		}
	}

	function ChapterTitle($num,$label)
	{
		//제목
		$this->SetFont('Arial','',12);
		$this->SetFillColor(200,220,255);
		$this->Cell(0,6,"Chapter  $num : $label",0,1,'L',1);
		$this->Ln(4);
		//세로좌표 저장
		$this->y0=$this->GetY();
	}

	function ChapterBody($fichier)
	{
		//텍스트 파일을 읽는다.
		$f=fopen($fichier,'r');
		$txt=fread($f,filesize($fichier));
		fclose($f);

		//글꼴
		$this->SetFont('Times','',12);

		//6cm 길이의 칼럼에 텍스트 출력
		$this->MultiCell(60,5,$txt);
		$this->Ln();

		//내용
		$this->SetFont('','I');
		$this->Cell(0,5,'(end of excerpt)');

		//첫번째 칼럼으로 돌아간다
		$this->SetCol(0);
	}

	function PrintChapter($num,$title,$file)
	{
		//챕터 추가
		$this->AddPage();
		$this->ChapterTitle($num,$title);
		$this->ChapterBody($file);
	}
}

$pdf=new PDF();
$title='20000 Leagues Under the Seas';
$pdf->SetTitle($title);
$pdf->SetAuthor('Jules Verne');
$pdf->PrintChapter(1,'A RUNAWAY REEF','20k_c1.txt');
$pdf->PrintChapter(2,'THE PROS AND CONS','20k_c2.txt');
$pdf->Output();
?>

[데모보기]

여기서 사용된 중요한 메소드는 AcceptPageBreak() 입니다.

이 메소드는 자동 페이지 나눔을 허용할지 안할지를 결정합니다. 페이지나눔을 하지않고 여백과 현재위치를 조정함으로써 원하는 칼럼 레이아웃을 만드는 것입니다.나머지는 크게 바뀐게 없습니다. 현재 칼럼의 번호와 시작하는 위치를 저장하기 위한 프로퍼티 두개가 추가되었고, MultiCell()을 6cm의 너비로 호출했습니다.

  1. 안녕하세요.
    여긴 중국인데요. fpdf.org 사이트가 계속해서 안뜨네요.(며칠째 -_-;)
    중국에서는 특정사이트가 잘 안뜨는 경우가 있어요....
    바쁘시겠지만 fpdf 클래스 파일좀 보내주시면 안될까요?
    메일로 좀 부탁드립니다.
    그럼,

댓글을 남겨주세요

This site uses Akismet to reduce spam. Learn how your comment data is processed.