Skip to content
Advertisement

PDFlib place table on bottom of defined fitbox

I have created a table in PDFlib PHP and defined the coordinates of the fitbox as the following:

$llx = 350;
$lly = 500;
$urx = 600;
$ury = 800;

Now I want the table to start at the bottom of these coordinates and I want the table to grow to the top if I add more cells. How can I achieve that?

The function I wrote so far (here the table starts at the top):

function createTable(pdflib $p, int $fontMedium, int $fontRegular, array $arrInput) {
    $tbl=0;
    $rowmax = 8;

    /* ---------- table header */
    $row = 1; $col = 1;

    $optlist = "margin=4 fittextline={position=center font=" . $fontMedium . " fontsize=10} " .
        "matchbox={fillcolor={#ffed00}} colspan=1 colwidth=200";

    $tbl = $p->add_table_cell($tbl, $col, $row, $arrInput['table']['tableHeading'], $optlist);
    if ($tbl == 0) {
        echo("Error: " . $p->get_errmsg());
        exit(1);
    }

    /* ---------- content rows */
    foreach($arrInput['table']['tableListings'] as $listings) {
        /* ----- Multi-line text with Textflow */
        // $row++;
        if ($row <= $rowmax) {
            $row++;
        } else {
            echo("Error: Too many Items for table");
            exit(1);
        }

        $font = $p->load_font("W-Regular", "unicode", "");
        if ($font == 0) {
            echo("Error: " . $p->get_errmsg());
            exit(1);
        }

        $optlist = "charref fontname=W-Regular encoding=unicode fontsize=10";

        $tf = $p->add_textflow(0, $listings, $optlist);
        if ($tf == 0) {
            echo("Error: " . $p->get_errmsg());
            exit(1);
        }

        $optlist = "margin=4 matchbox={fillcolor={rgb 0.9 0.5 0}} textflow=" . $tf;

        $tbl = $p->add_table_cell($tbl, $col, $row, "", $optlist);
        if ($tbl == 0) {
            echo("Error: " . $p->get_errmsg());
            exit(1);
        }
    }

    do {
        $optlist = "header=1 rowheightdefault=20 " .
        "stroke={{line=other}} ";

        $result = $p->fit_table($tbl, $llx, $lly, $urx, $ury, $optlist);
        if ($result ==  "_error") {
            echo("Couldn't place table: " . $p->get_errmsg());
            exit(1);
        }
    } while ($result == "_boxfull");

    $p->delete_table($tbl, "");
}

Advertisement

Answer

I have found the answer. In the $optlist for the fit_table you can define the position of the table which (if you want it at the bottom of the fitbox) looks like this:

$optlist = "header=1 rowheightdefault=20 "
        . "stroke={{line=horother linewidth=0.3}} "
        . "position={bottom} "; // statement to define position of the table in the fitbox
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement