Search code examples
phppdflib

Transparent background-color (fillcolor) for textline matchboxes


I'm creating business cards with pdflib and php. I'm using PDFLib 9.0.6p4 for Mac with PHP Version 5.6. There is a background image on which the text is displayed. To get it readable I want to have some background-color for my text. Managed to do this with matchbox and fillcolor. Is there a possibility to have a transparent fillcolor for my matchbox?

Already tried to put a transparent rect() as background of my text instead of the matchbox, but it didnt work because I don't know the height and width before printing my text.

    ...
    $textline = "Max Mustermann";
    $p->save();
    $gstate = $p->create_gstate("opacityfill=.8");
    /* Apply the extended graphics state */
    $p->set_gstate($gstate); 
    # Place the text in a box on the top right
    $optlist = "position={top left} showborder matchbox={ boxheight={fontsize descender} borderwidth=0.3 offsetleft=-2 offsetright=2 offsetbottom=-2 fillcolor=green}";
    $p->fit_textline($textline, $x, $y-=$yoff, $optlist);   # sample text
    $p->fit_textline($optlist, $xt, $y + 3, "fontsize=12"); # description
    $p->restore();
    ...

unfortunately this doesnt bring the expected effect of a transparent background color for text.


Solution

  • you are very close: you should add the gstate handle to the matchbox option list (simplified case):

    $gstate = $p->create_gstate("opacityfill=.8");
    $optlist = "matchbox={ fillcolor=green gstate=$gstate}";
    $p->fit_textline($textline, $x, $y-=$yoff, $optlist);