set_option("searchpath={" . $searchpath . "}"); /* This means we must check return values of load_font() etc. */ $p->set_option("errorpolicy=return"); if ($p->begin_document($outfile, "") == 0) throw new Exception("Error: " . $p->get_errmsg()); $p->set_info("Creator", "PDFlib Cookbook"); $p->set_info("Title", $title ); /* Start page */ $p->begin_page_ext(0, 0, "width=a4.width height=a4.height"); /* Draw a yellow background rectangle */ $p->setcolor("fill", "rgb", 1, 0.9, 0.58, 0); $p->rect(50, 50, 500, 750); $p->fill(); /* Display some descriptive red text */ $p->setcolor("fill", "rgb", 0.73, 0.12, 0.30, 0); $p->fit_textline("Transparent vector graphics", 100, 100, "fontname=NotoSerif-Regular fontsize=20"); /* Save the current graphics state. The save/restore of the current * state is not necessarily required, but it will help you get back to * a graphics state without any transparency. */ $p->save(); /* Create an extended graphics state with transparency set to 50%, using * create_gstate() with the "opacityfill" option set to 0.5. */ $gstate = $p->create_gstate("opacityfill=.5"); /* Apply the extended graphics state */ $p->set_gstate($gstate); /* Draw a blue rectangle which will be transparent according to the * graphics state set */ $p->setcolor("fill", "rgb", 0, 0.52, 0.64, 0); $p->rect(100, 500, 200, 600); $p->fill(); /* Draw a red circle which will be transparent according to the * graphics state set */ $p->setcolor("fill", "rgb", 0.73, 0.12, 0.30, 0); $p->arc(100, 100, 200, 0, 360); $p->fill(); /* Restore the current graphics state */ $p->restore(); $p->end_page_ext(""); $p->end_document(""); $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=transparent_graphics.pdf"); print $buf; } catch (PDFlibException $e){ echo("PDFlib exception occurred:\n" . "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . $e->get_errmsg() . "\n"); exit(1); } catch (Throwable $e) { echo("PHP exception occurred: " . $e->getMessage() . "\n"); exit(1); } $p = 0; ?>