" . "Phone 7079 4301\n" . "Fax 7079 4302" . "info@kraxi.com\n" . "www.kraxi.com\n"; $salesrepfilename = "sales_rep4.jpg"; $salesrepname = "Lucy Irwin"; $salesrepcaption = "Local sales rep:"; /* * ----------------------------------- * Print company logo and sender address * ----------------------------------- */ if ($run_sample_with_pdi) { $stationeryfilename = "kraxi_logo2.pdf"; $stationery = $p->open_pdi_document($stationeryfilename, ""); $page = $p->open_pdi_page($stationery, 1, ""); /* Check whether the page can be placed in the current situation */ if ((int) $p->info_pdi_page($page, "fittingpossible", "") == 0) { throw new Exception("Cannot place Tagged PDF page: " . $p->get_errmsg()); } $p->fit_pdi_page($page, 0, $y_company_logo, "boxsize={595 85} position={65 center}"); $p->close_pdi_page($page); $p->close_pdi_document($stationery); } $optlist = $basefontoptions . " fontsize=" . $fontsizesmall . " fontname=" . $stationeryfontname . " charref=true tag={tagname=Artifact}"; $p->fit_textline($sender, $x_table, $y_address + $fontsize, $optlist); /* * ----------------------------------- * Print full company contact details * ----------------------------------- */ $optlist = $basefontoptions . " fontname=" . $stationeryfontname . " fillcolor={gray 0.35} leading=125%"; $tf = $p->create_textflow($senderfull, $optlist); $p->fit_textflow($tf, $x_salesrep, $y_address, $x_salesrep + $imagesize, $y_address + 150, $basetagoptions . " verticalalign=bottom"); $p->delete_textflow($tf); /* * ----------------------------------- * Place name and image of local sales rep. * Figure is followed by Caption per Best Practice Guide * ----------------------------------- */ $salesrepimage = $p->load_image("auto", $salesrepfilename, ""); $optlist = "boxsize={" . $imagesize . " " . $imagesize . "} fitmethod=meet tag={tagname=Figure Alt={Photo of sales rep.} Placement=Block}"; $p->fit_image($salesrepimage, $x_salesrep, $y_address - 3 * $fontsizesmall - $imagesize, $optlist); $p->close_image($salesrepimage); /* Open two structure elements with a single call */ $id_Caption = $p->begin_item("Caption", "tag={tagname=P}"); $optlist = $basefontoptions . " fontsize=" . $fontsizesmall; $p->fit_textline($salesrepcaption, $x_salesrep, $y_address - $fontsizesmall, $optlist); $p->fit_textline($salesrepname, $x_salesrep, $y_address - 2 * $fontsizesmall, $optlist); $p->end_item($id_Caption); } /* * Print receiver address */ function create_address($p) { global $x_table, $tablewidth, $y_address, $basefontoptions, $basetagoptions; $address = "John Q. Doe\n" . "255 Customer Lane\n" . "Suite B\n" . "12345 User Town\n" . "Everland"; $optlist = $basefontoptions . " leading=120%"; $tf = $p->create_textflow($address, $optlist); $optlist = $basetagoptions; $p->fit_textflow($tf, $x_table, $y_address, $x_table + 200, $y_address - 100, $basetagoptions); $p->delete_textflow($tf); } /* * ----------------------------------- * Print the header and date * ----------------------------------- */ function create_table_header($p, $id_Part) { global $x_table, $tablewidth, $y_invoice, $basefontoptions, $basetagoptions; $invoiceheader = "INVOICE " . date("Y") . "-10"; /* Add a structured bookmark which references the Part element */ $optlist = "item=" . $id_Part; $p->create_bookmark($invoiceheader, $optlist); $optlist = $basefontoptions . " tag={tagname=H1 Title={" . $invoiceheader . "}}"; $p->fit_textline($invoiceheader, $x_table, $y_invoice, $optlist); $date = date("F j, Y"); $optlist = "position {100 0} " . $basefontoptions . " tag={tagname=P Title=date}"; ; $p->fit_textline($date, $x_table + $tablewidth, $y_invoice, $optlist); } /* This is where the data files are. Adjust as necessary. */ $searchpath = dirname(__FILE__,3)."/input"; /* By default annotations are also imported. In some cases this * requires the Noto fonts for creating annotation appearance streams. * We therefore set the searchpath to also point to the font directory. */ $fontpath = dirname(__FILE__,3)."/resource/font"; $closingtext = "Terms of payment: 30 days net. " . "90 days warranty starting at the day of sale. " . "This warranty covers defects in workmanship only. " . "Kraxi Systems, Inc. will, at its option, repair or replace the " . "product under the warranty. This warranty is not transferable. " . "No returns or exchanges will be accepted for wet products."; class articledata { function __construct($name, $price, $quantity) { $this->name = $name; $this->price = $price; $this->quantity = $quantity; } } $data = array( new articledata("Super Kite", 20, 2), new articledata("Turbo Flyer", 40, 5), new articledata("Giga Trash", 180, 1), new articledata("Bare Bone Kit", 50, 3), new articledata("Nitty Gritty", 20, 10), new articledata("Pretty Dark Flyer", 75, 1), new articledata("Free Gift", 0, 1) ); $headers = array( "ITEM", "DESCRIPTION", "QUANTITY", "PRICE (\u{20AC})", "AMOUNT (\u{20AC})" ); $alignments = array( "right", "left", "right", "right", "right" ); $pagecount = 0; $id_sect = 0; $id_div = 0; try { $p = new pdflib(); $p->set_option("SearchPath={" . $searchpath . "}"); $p->set_option("SearchPath={" . $fontpath . "}"); /* * This mean we don't have to check error return values, but will * get an exception in case of runtime problems. */ $p->set_option("errorpolicy=exception"); /* all strings are expected as utf8*/ /* "autospace" improves Reflow */ $p->set_option("autospace=true"); $p->begin_document("", "pdfua=PDF/UA-1 pdfa=PDF/A-2a lang=en " . "tag={tagname=Document " . "Title={Invoice for John Q. Doe - Kraxi Systems, Inc.}}"); $p->set_info("Creator", "tagged_invoice"); $p->set_info("Author", "Kraxi Systems, Inc."); $p->set_info("Title", "Invoice for John Q. Doe - Kraxi Systems, Inc."); /* Use sRGB output intent so that we can use RGB color */ $p->load_iccprofile("sRGB", "usage=outputintent"); /* Each Div element could contain a separate invoice */ $id_Part = $p->begin_item("Part", "Title={Invoice " . date("Y") . "-10}"); /* * ----------------------------------- * Create and place table with article list * ----------------------------------- */ /* ---------- Header row */ $row = 1; $tbl = 0; for ($col = 1; $col <= count($headers); $col++) { $optlist = "fittextline={position={" . $alignments[$col - 1] . " center} " . $basefontoptions . " " . "} margin=2"; $tbl = $p->add_table_cell($tbl, $col, $row, $headers[$col - 1], $optlist); } $row++; /* ---------- Data rows: one for each article */ $total = 0; for ($i = 0; $i < count($data); $i++) { $sum = $data[$i]->price * $data[$i]->quantity; $col = 1; /* column 1: ITEM */ $buf = sprintf("%d", $i + 1); $optlist = "fittextline={position={" . $alignments[$col - 1] . " center} " . $basefontoptions . " " . "} margin=2"; $tbl = $p->add_table_cell($tbl, $col++, $row, $buf, $optlist); /* column 2: DESCRIPTION */ $optlist = "fittextline={position={" . $alignments[$col - 1] . " center} " . $basefontoptions . " " . "} colwidth=50% margin=2"; $tbl = $p->add_table_cell($tbl, $col++, $row, $data[$i]->name, $optlist); /* column 3: QUANTITY */ $buf = sprintf("%d", $data[$i]->quantity); $optlist = "fittextline={position={" . $alignments[$col - 1] . " center} " . $basefontoptions . " " . "} margin=2"; $tbl = $p->add_table_cell($tbl, $col++, $row, $buf, $optlist); /* column 4: PRICE */ $buf = sprintf("%.2f", $data[$i]->price); $optlist = "fittextline={position={" . $alignments[$col - 1] . " center} " . $basefontoptions . " " . "} margin=2"; $tbl = $p->add_table_cell($tbl, $col++, $row, $buf, $optlist); /* column 5: AMOUNT */ $buf = sprintf("%.2f", $sum); $optlist = "fittextline={position={" . $alignments[$col - 1] . " center} " . $basefontoptions . " " . "} margin=2"; $tbl = $p->add_table_cell($tbl, $col++, $row, $buf, $optlist); $total += $sum; $row++; } /* ---------- Print total in the rightmost column */ $buf = sprintf("%.2f", $total); $optlist = "fittextline={position={" . $alignments[count($headers) - 1] . " center} " . $basefontoptions . "} margin=2"; $tbl = $p->add_table_cell($tbl, count($headers), $row++, $buf, $optlist); /* ---------- Create invoice header on first page */ $p->begin_page_ext(0, 0, $pagedimensions); $id_Sect = $p->begin_item("Sect", "Title={Sender stationery}"); create_stationery($p); $p->end_item($id_Sect); $id_Sect = $p->begin_item("Sect", "Title={Recipient address}"); create_address($p); $p->end_item($id_Sect); $id_Sect = $p->begin_item("Sect", "Title={Invoice body}"); create_table_header($p, $id_Part); /* * ---------- Place the table instance(s), creating pages as required */ $top = $y_invoice - 3 * $fontsize; do { /* Place the table on the page; Shade every other row. */ $optlist = "header=1 fill={{area=rowodd fillcolor={gray 0.9}}} " . "tag={tagname=Table}"; $result = $p->fit_table($tbl, $x_table, $bottom, $x_table + $tablewidth, $top, $optlist); if ($result == "_error") { throw new Exception("Couldn't place table: " . $p->get_errmsg()); } else if ($result == "_boxfull") { $p->end_page_ext(""); $p->begin_page_ext(0, 0, $pagedimensions); $top = $y_invoice_p2; } } while ($result == "_boxfull"); /* Determine vertical start point for footer from top of last * placed table fragment. */ $table_height = $p->info_table($tbl, "height"); $top -= $table_height + 2 * $fontsize; $p->delete_table($tbl, ""); /* ---------- Footer with terms of payment */ $optlist = $basefontoptions . " alignment=justify leading=120%"; $tf = $p->create_textflow($closingtext, $optlist); /* Loop until all of the text is placed; create new pages * as long as more text needs to be placed. */ do { $result = $p->fit_textflow($tf, $x_table, $bottom, $x_table + $tablewidth, $top, "tag={tagname=P}"); /* Start new page if more text is to be placed */ if ($result == "_boxfull" || $result == "_nextpage") { $p->end_page_ext(""); $p->begin_page_ext(0, 0, $pagedimensions); $top = $y_invoice_p2; } } while ($result == "_boxfull" || $result == "_nextpage"); $p->end_item($id_Sect); $p->end_item($id_Part); $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=invoice_pdfa2a.pdf"); print $buf; } catch (PDFlibException $e) { echo("PDFlib exception occurred in invoice_pdfa2a sample:\n" . "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . $e->get_errmsg() . "\n"); exit(1); } catch (Throwable $e) { echo($e); exit(1); } $p = 0; ?>