Zend PDF

http://framework.zend.com/manual/ru/zend.pdf.html

http://devzone.zend.com/article/2525

http://agoln.net/archives/81

Пример:

<?php

require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();

$pdf = new Zend_Pdf();
$pdf->properties['Title'] = "TITLE";
$pdf->properties['Author'] = "AUTHOR";

$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$pdf->pages[] = $page;
$page_width = $page->getWidth();   // A4 : 595
$page_height = $page->getHeight(); // A4 : 842

$font = Zend_Pdf_Font::fontWithPath('arial.ttf');

$page->setFont($font, 20);

$page->drawText('Hello World!', 10, $page_height - 20, 'UTF-8');

//$color = new Zend_Pdf_Color_Html('#3366FF');
$page->setLineWidth(0.5);
$page->drawLine(10, $page_height - 25, $page_width - 10, $page_height - 25);

$image = Zend_Pdf_Image::imageWithPath('img.jpg');
$imgWidth = $image->getPixelWidth();
$imgHeight = $image->getPixelHeight();
$x1 = 10;
$y1 = $page_height - $image->getPixelWidth() / 2;
$x2 = $x1 + $image->getPixelWidth() / 2;
$y2 = $y1 + $image->getPixelHeight() / 2;
$page->drawImage($image, $x1, $y1, $x2, $y2);

$pdf->save('tmp.pdf');
$pdfData = $pdf->render();

header("Content-Disposition: inline; filename=result.pdf");
header("Content-type: application/x-pdf");
echo $pdfData;

Wrap:

<?php
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();

$pdf = new Zend_Pdf();
$pdf->properties['Title'] = "TITLE";
$pdf->properties['Author'] = "AUTHOR";

$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$pdf->pages[] = $page;
$page_width = $page->getWidth();   // A4 : 595
$page_height = $page->getHeight(); // A4 : 842

$font = Zend_Pdf_Font::fontWithPath('arial.ttf');

$page->setFont($font, 20);
$page->drawText('Hello World!', 10, $page_height - 20, 'UTF-8');

//$color = new Zend_Pdf_Color_Html('#3366FF');
$page->setLineWidth(0.5);
$page->drawLine(10, $page_height - 25, $page_width - 10, $page_height - 25);

$wrap_txt = file_get_contents('wrap.txt');

$text = wordwrap($wrap_txt, 80, "\r\n", false);
$token = strtok($text, "\r\n");

$y = $page_height - 60;
$page->setFont($font, 10);
while ($token != false) {
    if ($y < 60) {
        $pdf->pages[] = ($page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4));
        $page->setFont($font, 10);
        $y = $page_height - 60;
    } else {
        $page->drawText($token, 20, $y, 'UTF-8');
        $y-=20;
    }
    $token = strtok("\r\n");
}

$pdf->save('wrap.pdf');
$pdfData = $pdf->render();

header("Content-Disposition: inline; filename=result.pdf");
header("Content-type: application/x-pdf");
echo $pdfData;

Center:

<?php
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();

$pdf = new Zend_Pdf();

$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$pdf->pages[] = $page;
$page_width = $page->getWidth();   // A4 : 595
$page_height = $page->getHeight(); // A4 : 842

$font = Zend_Pdf_Font::fontWithPath('arial.ttf');

$page->setFont($font, 20);
$w = widthForStringUsingFontSize('Hello World!', $font, 20);

$page->drawText('Hello World!', $page_width / 2 - $w / 2, $page_height - 20, 'UTF-8');

$pdf->save('center.pdf');
$pdfData = $pdf->render();

header("Content-Disposition: inline; filename=result.pdf");
header("Content-type: application/x-pdf");
echo $pdfData;

/**
* Returns the total width in points of the string using the specified
    font and
* size.
*
* This is not the most efficient way to perform this calculation. I'm
* concentrating optimization efforts on the upcoming layout manager
    class.
* Similar calculations exist inside the layout manager class, but
    widths are
* generally calculated only after determining line fragments.
*
* @param string $string
* @param Zend_Pdf_Resource_Font $font
* @param float $fontSize Font size in points
* @return float
*/
function widthForStringUsingFontSize($string, $font, $fontSize) {
    $drawingString = iconv('UTF-8', 'UTF-16BE//IGNORE', $string);
    $characters = array();
    for ($i = 0; $i < strlen($drawingString); $i++) {
        $characters[] = (ord($drawingString[$i++]) << 8 ) | ord($drawingString[$i]);
    }
    $glyphs = $font->glyphNumbersForCharacters($characters);
    $widths = $font->widthsForGlyphs($glyphs);
    $stringWidth = (array_sum($widths) / $font->getUnitsPerEm()) * $fontSize;
    return $stringWidth;
}

dompdf

Так же нашел либу dompdf которая генерить pdf прямо из html’а. Но к сожалению возникает целаю куча проблем с кирилицей, так же полученый pdf выглядит криво.

http://code.google.com/p/dompdf/

http://chernev.ru/dompdf.rar