Tuesday, August 9, 2011

PDFlib & PHP - (1) HELLO WERLD

I need to take baby-steps; myself described in the PDFlib manual as a PHP Web Programmer.
NOTE: The API listing/real documentation for PDFlib lives here under Reference

In the example here I sometimes say, "see: ...php.net/some-documentation," and there is a distinct and notable difference between the php.net and usage below. That is, in all "see:" comments it works like this:


  • PHP.NET says:
    float PDF_stringwidth ( resource $p , string $text , int $font , float $fontsize )

  • Where usage below is always going to replace the PDF_ with the PDFlib object $p.

  • ...and the first parameter in the PHP.NET documentaion is never resource, but the next, i.e. for the above mentioned PDF_stringwidth we have:
    $p->stringwidth(string $text, int $font, float $fontsize)

This will be:


  1. Make a PDF delivered to browser that was crafted using PDFlib usage of PHP.

  2. Throw in a (centered-to-doc) line of text, in the PDF.



try{
# number reflect an A4 sized document.
$doc_width = 595;
$doc_height = 842;
# create a new instance of PDFlib.
$p = new PDFlib();
# extras here. in cookbook examples these are not mentioned.
# but, for error logging add logging and set errorpolicy to exception.
# (thanks PDFLib support for these setting info)
$p->set_parameter("logging", "filename {PDFlib.log}");
$p->set_parameter("errorpolicy", "exception");
# set textformat, or try without and watch log:
# [Warning message 2592: "Glyph for Unicode value U+6548 not
# found in font 'Helvetica' (Unicode will be replaced in text)"]
$p->set_parameter("textformat", "utf8");
# begin document, see:
# http://www.php.net/manual/en/function.pdf-begin-document.php
if ($p->begin_document("", "") == 0)
throw new Exception("Error@begin_document: " . $p->get_errmsg());
# loading font (that most will have)
$font = $p->load_font("Helvetica", "unicode", "");
# make/begin page, see:
# http://www.php.net/manual/en/function.pdf-begin-page-ext.php
$p->begin_page_ext($doc_width, $doc_height, "");
# setfont before adding text, see:
# http://www.php.net/manual/en/function.pdf-setfont.php
if ($p->setfont($font, 24) == 0)
throw new Exception("Error@setfont: " . $p->get_errmsg());
# add line of text, to center of page, approximately :)
# doing a few things here to center the width and then place text.
$message = "Hello Werld!";
$message_width = $p->stringwidth($message, $font, 24);
$p->show_xy($message, ($doc_width*.5)-($message_width*.5), $doc_height*.5);
# end page...
$p->end_page_ext("");
#
$p->end_document("");
# get and send buffer
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=blog_1.pdf");
print $buf;
} catch (PDFlibException $e) {
die("PDFlib exception occurred:\n".
"[" . $e->get_errnum() . "] " . $e->get_apiname() .
": " . $e->get_errmsg() . "\n");
} catch (Exception $e) {
die($e->getMessage());
}

Props to this person for the pretty-print on blogger, blog:
Pretty Print Blog by Luka Marinko

No comments:

Post a Comment