$value , 'border' => $border , 'bold' => $bold , 'align' => $align , 'font_size' => $font_size , 'cell_width' => $cell_width , 'cell_height' => $cell_height , 'check_page_end' => $check_page_end , 'ishtml' => $ishtml ); } else { $_PAGE_INFO['new_page_hdr'] = ""; } } if (is_array($value)) { // New page? if ((($_PAGE_INFO['height'] - ($nr_lines * $cell_height)) < PDF_MARGIN_BOTTOM) || (($nr_lines * $cell_height) > $_PAGE_INFO['height'])) { pdf_page_new(); } // Add new page header? if (($_PAGE_INFO['header_first']) && (is_array($_PAGE_INFO['new_page_hdr']))) { print_line($_PAGE_INFO['new_page_hdr']['value'] , $_PAGE_INFO['new_page_hdr']['border'] , $_PAGE_INFO['new_page_hdr']['bold'] , $_PAGE_INFO['new_page_hdr']['align'] , $_PAGE_INFO['new_page_hdr']['font_size'] , $_PAGE_INFO['new_page_hdr']['cell_width'] , $_PAGE_INFO['new_page_hdr']['cell_height'] , $_PAGE_INFO['new_page_hdr']['check_page_end'] , $_PAGE_INFO['new_page_hdr']['ishtml'] , $_PAGE_INFO['new_page_hdr']['styling_options'] ); } // New line print_line($value, $border, $bold, $align, $font_size, $cell_width, $cell_height, $check_page_end, $ishtml, $styling_options); } } /** * Create image */ function pdf_print_image($name, $source, $height, $width = PDF_WIDTH, $left = PDF_MARGIN_LEFT) { global $_PAGE_INFO; // New page? if (($_PAGE_INFO['height'] - $height) < PDF_MARGIN_BOTTOM) { pdf_page_new(); } // Skip image? if ($height < $_PAGE_INFO['height']) { // Load header image $_PAGE_INFO[$name] = PDF_load_image($_PAGE_INFO['pdf'], "auto", $source, ""); if ($_PAGE_INFO[$name] == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); return FALSE; } // Create table $table = PDF_add_table_cell($_PAGE_INFO['pdf'], 0, 1, 1, "", "image=" . $_PAGE_INFO[$name] . ""); if ($table == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); return FALSE; } // Add table $result = PDF_fit_table($_PAGE_INFO['pdf'], $table, $left, ($_PAGE_INFO['height'] - $height), ($left + $width), $_PAGE_INFO['height'], ""); if ($result == "_error") { error_log("Couldn't place table (7): " . PDF_get_errmsg($_PAGE_INFO['pdf'])); return FALSE; } // Decrement height counter $_PAGE_INFO['height'] -= $height; } } /** * New page pdf */ function pdf_page_new() { page_footer(); page_header(); } /** * Print the pdf page header */ function pdf_start_document($page_subject = "", $header_subject = "", $data = "") { global $_PAGE_INFO; // Define page subject $_PAGE_INFO['page_subject'] = $page_subject; // Create new pdf document if( !isset($_PAGE_INFO['pdf']) ) { $_PAGE_INFO['pdf'] = PDF_new(); // Set license PDF_set_parameter($_PAGE_INFO['pdf'], "license", "L700102-010500-736029-N8GN22-6BRR72"); // This means we must check return values PDF_set_parameter($_PAGE_INFO['pdf'], "errorpolicy", "return"); // Set parameters needed for font PDF_set_parameter($_PAGE_INFO['pdf'], "textformat", "utf8"); PDF_set_parameter($_PAGE_INFO['pdf'], "SearchPath", FONT_DIR); PDF_set_parameter($_PAGE_INFO['pdf'], "FontOutline", "Verdana=verdana.ttf"); PDF_set_parameter($_PAGE_INFO['pdf'], "FontOutline", "Verdana-Bold=verdanab.ttf"); PDF_set_parameter($_PAGE_INFO['pdf'], "FontOutline", "Verdana-Italics=verdanai.ttf"); PDF_set_parameter($_PAGE_INFO['pdf'], "FontOutline", "Verdana-BoldItalics=verdanaz.ttf"); if (PDF_begin_document($_PAGE_INFO['pdf'], "", "openmode=bookmarks flush=page") == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } // Set creator/Author/subject PDF_set_info($_PAGE_INFO['pdf'], "Creator", $_PAGE_INFO['skin_name']); PDF_set_info($_PAGE_INFO['pdf'], "Author" , $_PAGE_INFO['skin_name']); PDF_set_info($_PAGE_INFO['pdf'], "Subject", $page_subject); // Load header image $image_src = $_PAGE_INFO['base_path'] . SKIN_DIR . $_PAGE_INFO['skin'] . "/" . $_PAGE_INFO['ini']['report']['header_logo']; $_PAGE_INFO['header'] = PDF_load_image($_PAGE_INFO['pdf'], "auto", $image_src, ""); if ($_PAGE_INFO['header'] == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } // Load Font $_PAGE_INFO['font'] = PDF_load_font($_PAGE_INFO['pdf'], "Verdana", "unicode", ""); if ($_PAGE_INFO['font'] == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } $_PAGE_INFO['font-bold'] = PDF_load_font($_PAGE_INFO['pdf'], "Verdana-Bold", "unicode", ""); if ($_PAGE_INFO['font-bold'] == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } $_PAGE_INFO['font-italics'] = PDF_load_font($_PAGE_INFO['pdf'], "Verdana-Italics", "unicode", ""); if ($_PAGE_INFO['font-italics'] == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } $_PAGE_INFO['font-bold-italics'] = PDF_load_font($_PAGE_INFO['pdf'], "Verdana-BoldItalics", "unicode", ""); if ($_PAGE_INFO['font-bold-italics'] == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } // Initial values $_PAGE_INFO['link'] = 0; $_PAGE_INFO['page'] = 0; } // Create header page_header(); } function pdf_page_header($page_subject = "", $header_subject = "", $data = "") { global $_PAGE_INFO; // Print header if (strlen($header_subject)) { print_line(array($header_subject), array("LRT"), array(1), "", array(16), "", 18, 0, ""); print_line(array(""), array("LR"), "", "", "", "", 4, 0, ""); } // Header data if (is_array($data)) { // Default width $width = 115; if ((isset($_PAGE_INFO['orientation'])) && ($_PAGE_INFO['orientation'] == "landscape")) { $width = 198; } for ($i = 0; $i < sizeof($data); $i++) { if (($i + 1) == sizeof($data)) $border_left = "LB"; else $border_left = "L"; print_line(array($data[$i][0] . ":",shorten_text($data[$i][1],42,SHORTEN_NARROW,FALSE)), array($border_left,"LRBT"), array(1,0), array("R","L"), array(8,8), array($width,65), 16, 0, ""); } } // Empty line print_line(array(""), "", "", "", "", "", 16, 0, ""); print_line(array(""), "", "", "", "", "", 16, 0, ""); } /** * Print the pdf page footer */ function pdf_page_footer($file = "", $dest = "D") { global $_PAGE_INFO; // Initial value $result = ""; // Set title PDF_set_info($_PAGE_INFO['pdf'], "Title", $file); // Create footer page_footer(); // Add page footers for ($i = 1; $i <= $_PAGE_INFO['page']; $i++) { // Go to previous page PDF_resume_page($_PAGE_INFO['pdf'], "pagenumber " . $i); // Bottom line $table = PDF_add_table_cell($_PAGE_INFO['pdf'], 0, 1, 1, "", "matchbox={borderwidth=1 drawbottom=false drawright=false drawleft=false drawtop=true}"); if ($table == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } $result = PDF_fit_table($_PAGE_INFO['pdf'], $table, PDF_MARGIN_LEFT, (PDF_MARGIN_BOTTOM - 10), PDF_MARGIN_LEFT + PDF_WIDTH, (PDF_MARGIN_BOTTOM - 14), ""); if ($result == "_error") { error_log("Couldn't place table (1): " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } // page number $table = PDF_add_table_cell($_PAGE_INFO['pdf'], 0, 1, 1, $i . " / " . $_PAGE_INFO['page'], "fittextline={font=" . $_PAGE_INFO['font'] . " fontsize=8 position={center center}} colwidth=100"); if ($table == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } // Create new page $result = PDF_fit_table($_PAGE_INFO['pdf'], $table, ((PDF_WIDTH + PDF_MARGIN_LEFT + PDF_MARGIN_RIGHT)/2 - 50), (PDF_MARGIN_BOTTOM - 14), ((PDF_WIDTH + PDF_MARGIN_LEFT + PDF_MARGIN_RIGHT)/2 + 50), (PDF_MARGIN_BOTTOM - 30), ""); if ($result == "_error") { error_log("Couldn't place table (2): " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } // page footer text $table = PDF_add_table_cell($_PAGE_INFO['pdf'], 0, 1, 1, html_entity_decode($_PAGE_INFO['ini']['report']['footer_text']), "fittextline={font=" . $_PAGE_INFO['font'] . " fontsize=8}"); if ($table == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } $result = PDF_fit_table($_PAGE_INFO['pdf'], $table, PDF_MARGIN_LEFT + 10, (PDF_MARGIN_BOTTOM - 14), PDF_MARGIN_LEFT + 200, (PDF_MARGIN_BOTTOM - 30), ""); if ($result == "_error") { error_log("Couldn't place table (4): " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } // Delete table PDF_delete_table($_PAGE_INFO['pdf'], $table, ""); // EOP PDF_end_page_ext($_PAGE_INFO['pdf'], ""); } // EOF PDF_end_document($_PAGE_INFO['pdf'], ""); if ($dest == "D") { // Download document download_document(array('mimetype' => "application/pdf", 'document' => PDF_get_buffer($_PAGE_INFO['pdf']), 'filename' => $file)); } else { $result = PDF_get_buffer($_PAGE_INFO['pdf']); } // Delete document PDF_delete($_PAGE_INFO['pdf']); unset($_PAGE_INFO['pdf']); // Remove all attached files if (!empty($_PAGE_INFO['delete'])) { foreach ($_PAGE_INFO['delete'] as $file) { if (file_exists($file)) { unlink($file); } } } return $result; } /******************************************************************************** * Internal functions ********************************************************************************/ /** * Create page header */ function page_header() { global $_PAGE_INFO; // Create new page if ((isset($_PAGE_INFO['orientation'])) && ($_PAGE_INFO['orientation'] == "landscape")) { PDF_begin_page_ext($_PAGE_INFO['pdf'], 0, 0, "width=a4.height height=a4.width"); define("PDF_WIDTH" ,844 - PDF_MARGIN_LEFT - PDF_MARGIN_RIGHT); // Width 100% = 263 define("PDF_HEIGHT" ,595); } else { PDF_begin_page_ext($_PAGE_INFO['pdf'], 0, 0, "width=a4.width height=a4.height"); define("PDF_HEIGHT" ,842); define("PDF_WIDTH" ,595 - PDF_MARGIN_LEFT - PDF_MARGIN_RIGHT); // Width 100% = 180 } $_PAGE_INFO['on_page'] = TRUE; // Reset height counter $_PAGE_INFO['height'] = PDF_HEIGHT; // Add header image $table = PDF_add_table_cell($_PAGE_INFO['pdf'], 0, 1, 1, "", "image=" . $_PAGE_INFO['header']); if ($table == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } $result = PDF_fit_table($_PAGE_INFO['pdf'], $table, PDF_MARGIN_LEFT, ($_PAGE_INFO['height'] - PDF_MARGIN_TOP - PDF_LOGO_HEIGHT), (PDF_MARGIN_LEFT + PDF_LOGO_WIDTH), $_PAGE_INFO['height'] - PDF_MARGIN_TOP, ""); if ($result == "_error") { error_log("Couldn't place table (5): " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } // Add header info print_line(array("") , "" , "" , "", "" , "" , 12, 0, ""); print_line(array("", ucwords($_PAGE_INFO['skin_name'])) , "" , array(0,1), "", array(12,12), array(40,140), 14, 0, ""); print_line(array("", $_PAGE_INFO['page_subject']) , "" , array(0,0), "", array(10,10), array(40,140), 14, 0, ""); print_line(array("", $_PAGE_INFO['ini']['report']['website']), "" , array(0,0), "", array(10,10), array(40,140), 10, 0, array("",$_PAGE_INFO['ini']['report']['website'])); // Top line $table = PDF_add_table_cell($_PAGE_INFO['pdf'], 0, 1, 1, "", "matchbox={borderwidth=1 drawbottom=false drawright=false drawleft=false drawtop=true}"); if ($table == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } $result = PDF_fit_table($_PAGE_INFO['pdf'], $table, PDF_MARGIN_LEFT, (PDF_HEIGHT - 55), PDF_MARGIN_LEFT + PDF_WIDTH, (PDF_HEIGHT - 65), ""); if ($result == "_error") { error_log("Couldn't place table (6): " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } // Delete table PDF_delete_table($_PAGE_INFO['pdf'], $table, ""); // empty lines print_line(array(""), "" , "", "", "", "", 20, 0, ""); // Set header first flag $_PAGE_INFO['header_first'] = 1; // Increment page $_PAGE_INFO['page']++; } /** * Create page footer */ function page_footer() { global $_PAGE_INFO; // EOP => suspend if( $_PAGE_INFO['on_page'] ) { PDF_suspend_page($_PAGE_INFO['pdf'], ""); $_PAGE_INFO['on_page'] = FALSE; } } /** * Create page line */ function print_line($value, $border = "", $bold = "", $align = "", $font_size = "", $cell_width = "", $cell_height = 16, $check_page_end = 1, $ishtml = "", $styling_options = "") { global $_PAGE_INFO; // Reset row width $row_width = PDF_MARGIN_LEFT; if (is_array($value)) { for($i = 0; $i < sizeof($value); $i++) { $text = html_entity_decode($value[$i]); // Link? $optlist = ""; // Font bold/italics/regular? // NB (yech) the 'bold' parameter is in fact italics when set to "I" (or "italics" or anything that starts with an 'i' for that matter) $font = ((is_array($bold)) && ($bold[$i])) ? (substr(strtolower($bold[$i]),0,1) == "i" ? $_PAGE_INFO['font-italics'] : $_PAGE_INFO['font-bold']) : $_PAGE_INFO['font']; // Font alignment? if ((is_array($align)) && (strtolower($align[$i]) == "c")) { $fittextoptions = "position=center "; } else if ((is_array($align)) && (strtolower($align[$i]) == "r")) { $fittextoptions = "position={right center} "; $optlist .= "marginright=2 "; } else { $fittextoptions = "position={left center} "; $optlist .= "marginleft=2 "; } // Create matchbox $optlist .= "matchbox={name=matchbox_" . $_PAGE_INFO['link'] . " "; // Borders? if ((is_array($border)) && (strlen($border[$i]))) { $optlist .= (strpos(strtolower($border[$i]), "l") !== FALSE) ? "drawleft=true " : "drawleft=false "; $optlist .= (strpos(strtolower($border[$i]), "r") !== FALSE) ? "drawright=true " : "drawright=false "; $optlist .= (strpos(strtolower($border[$i]), "t") !== FALSE) ? "drawtop=true " : "drawtop=false "; $optlist .= (strpos(strtolower($border[$i]), "b") !== FALSE) ? "drawbottom=true " : "drawbottom=false "; $optlist .= "borderwidth=0.5 "; } else { $optlist .= "drawleft=false drawright=false drawtop=false drawbottom=false borderwidth=0 "; } // Extra styling options for cell? $optlist_font = ""; if ((is_array($styling_options)) && (isset($styling_options[$i]))) { if (isset($styling_options[$i]['cell'])) { $optlist .= $styling_options[$i]['cell'] . " "; } if (isset($styling_options[$i]['font'])) { $optlist_font = $styling_options[$i]['font']; } } // Add EOF/BOF border if ($check_page_end) { if (($_PAGE_INFO['height'] - (2 * $cell_height)) < PDF_MARGIN_BOTTOM) { if (strpos(strtolower($border[$i]), "b") !== FALSE) { $optlist .= "drawbottom=true borderwidth=0.5 "; } } if ($_PAGE_INFO['header_first']) { if (strpos(strtolower($border[$i]), "b") !== FALSE) { $optlist .= "drawtop=true borderwidth=0.5 "; } } } $optlist .= "} "; if ((is_array($ishtml)) && (((strtolower($ishtml[$i]['type']) == "link") && (strlen($ishtml[$i]['data']))) || (strtolower($ishtml[$i]['type']) == "attachment"))) { $fittextoptions .= "fillcolor={" . $_PAGE_INFO['ini']['report']['link_color'] . "} "; } // Table width $width = (!is_array($cell_width)) ? PDF_WIDTH/sizeof($text) : ($cell_width[$i] * 3); // Create table if( is_array($font_size) ) $cell_fontsize = $font_size[$i]; else if( is_numeric($font_size) ) $cell_fontsize = $font_size; else $cell_fontsize = 12; $table = PDF_add_table_cell($_PAGE_INFO['pdf'], 0, 1, 1, $text, $optlist . "fittextline={" . $fittextoptions . " font=" . $font . " fontsize=" . $cell_fontsize . " " . $optlist_font . "} colwidth=100% rowheight=100%"); if ($table == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } // Add table $result = PDF_fit_table($_PAGE_INFO['pdf'], $table, $row_width, ($_PAGE_INFO['height'] - $cell_height), ($row_width + $width), $_PAGE_INFO['height'], ""); if ($result == "_error") { error_log("Couldn't place table (7): " . PDF_get_errmsg($_PAGE_INFO['pdf']) . ", width=" . $row_width . "+" . $width . ", height=" . $_PAGE_INFO['height'] . "-" . $cell_height . ", font_size=" . $cell_fontsize . ", backtrace: " . calltrace()); } // Add link? if ((is_array($ishtml)) && (strtolower($ishtml[$i]['type']) == "link") && (strlen($ishtml[$i]['data']))) { $action = PDF_create_action($_PAGE_INFO['pdf'], "URI", "url={" . $ishtml[$i]['data'] . "}"); $annotation = PDF_create_annotation($_PAGE_INFO['pdf'],0, 0, 0, 0, "link", "action={activate " . $action . "} usematchbox={matchbox_" . $_PAGE_INFO['link'] . "} linewidth=0"); } // Add file attachment? if ((is_array($ishtml)) && (strtolower($ishtml[$i]['type']) == "attachment")) { // Load attachment image $image_src = $_PAGE_INFO['base_path'] . IMAGE_DIR . "attachment.png"; $_PAGE_INFO['attachment'] = PDF_load_image($_PAGE_INFO['pdf'], "auto", $image_src, ""); if ($_PAGE_INFO['attachment'] == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } $_PAGE_INFO['filename'] = $ishtml[$i]['filename']; $_PAGE_INFO['mimetype'] = $ishtml[$i]['mimetype']; $annotation = PDF_create_annotation($_PAGE_INFO['pdf'],0, 0, 0, 0, "FileAttachment", "filename={" . $ishtml[$i]['filename'] . "} contents={". $ishtml[$i]['data'] . "} mimetype=\"" . $ishtml[$i]['mimetype'] . "\" opacity=0 usematchbox={matchbox_" . $_PAGE_INFO['link'] . "}"); // Add paperclip $table = PDF_add_table_cell($_PAGE_INFO['pdf'], 0, 1, 1, "", "image=" . $_PAGE_INFO['attachment']); $result = PDF_fit_table($_PAGE_INFO['pdf'], $table, PDF_MARGIN_LEFT + ($i * $row_width) + 2, ($_PAGE_INFO['height'] - $cell_height + 1), PDF_MARGIN_LEFT + ($i * $row_width) + 14, $_PAGE_INFO['height'] - 1, ""); if ($table == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } } // Add image? if ((is_array($ishtml)) && (strtolower($ishtml[$i]['type']) == "image")) { // Load item image $image_src = $_PAGE_INFO['base_path'] . IMAGE_DIR . $ishtml[$i]['filename']; $_PAGE_INFO['image'] = PDF_load_image($_PAGE_INFO['pdf'], "auto", $image_src, ""); if ($_PAGE_INFO['image'] == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } $table = PDF_add_table_cell($_PAGE_INFO['pdf'], 0, 1, 1, "", "image=" . $_PAGE_INFO['image']); $result = PDF_fit_table($_PAGE_INFO['pdf'], $table, PDF_MARGIN_LEFT + ($ishtml[$i]['left'] * 3), ($_PAGE_INFO['height'] - $cell_height + 1), PDF_MARGIN_LEFT + ($ishtml[$i]['left'] * 3) + $ishtml[$i]['width'], $_PAGE_INFO['height'] - 1, ""); if ($table == 0) { error_log("Error: " . PDF_get_errmsg($_PAGE_INFO['pdf'])); } } // Add bookmark? if ((is_array($ishtml)) && (strtolower($ishtml[$i]['type']) == "bookmark")) { // Bold? $font = ((isset($ishtml[$i]['bold'])) && ($ishtml[$i]['bold'])) ? "fontstyle=bold" : ""; // New parent? $parent = ((isset($ishtml[$i]['parent'])) && ($ishtml[$i]['parent'])) ? "parent=" . $ishtml[$i]['parent'] . "" : "parent=0"; // Create bookmark $handle = PDF_create_bookmark($_PAGE_INFO['pdf'], $ishtml[$i]['data'], $font . " " . $parent); // New handle? $_PAGE_INFO['bookmark_handle'] = ((isset($ishtml[$i]['parent'])) && (!$ishtml[$i]['parent'])) ? $handle : $_PAGE_INFO['bookmark_handle']; } // Delete table PDF_delete_table($_PAGE_INFO['pdf'], $table, ""); // Increment counters $row_width += $width; $_PAGE_INFO['link']++; } // Decrement height counter $_PAGE_INFO['height'] -= $cell_height; // Clear header first $_PAGE_INFO['header_first'] = 0; } } /** * Fill with empty vertical space */ function pdf_vfill($keep) { global $_PAGE_INFO; print_line(array(""), "", "", "", "", "", $_PAGE_INFO['height'] - $keep - PDF_MARGIN_BOTTOM, 0); } ?>