Search code examples
pdflib

PDFlib - change origin of text to top-left from default of bottom-left


In pdflib:

$font = $p->load_font("Helvetica", "unicode", "");
$p->setfont($font, 12);
$p->set_text_pos(100, 100);
$p->show('My text');

First arguments of set_text_pos($x, $y) tells library where to position bottom-left corner of text element.

Question. How can I change the origin of text to be top-left corner, so that "My text" string appears below 100x100 coordinate?


Solution

  • I would recommend to use the more comfortable fit_textline() call. In this case, you can replace your four lines with a single line:

    $p->fit_textline('My text', 100, 100, 'fontname=Helvetica encoding=unicode fontsize=12 position {top left} ');