From 5632cbd73ce91fdee1f5900866b7b3fc60f150c8 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 30 Mar 2016 16:20:44 -0400 Subject: [PATCH] Adding content for generating PDF Temporarily using the library that I have with ionCube which is there eval version. --- models/admin/ajax/pdfOutput.php | 49 ++------------- models/front/events/list.php | 103 ++++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 49 deletions(-) diff --git a/models/admin/ajax/pdfOutput.php b/models/admin/ajax/pdfOutput.php index 360398b..a9b7a34 100644 --- a/models/admin/ajax/pdfOutput.php +++ b/models/admin/ajax/pdfOutput.php @@ -13,7 +13,8 @@ * @version 0.1 */ -/* +require_once GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/models/front/events/list.php'; +/** * Steve Note... * * You can get to this using the following URL. @@ -32,12 +33,12 @@ // Load Members data abstract // require_once(GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataImages.php'); -/* +/** * This class performs the work of handling images passed to it via * an AJAX call that goes through the WorPress AJAX Handler. * */ -class GlmMembersAdmin_ajax_pdfOutput // extends GlmDataImages +class GlmMembersAdmin_ajax_pdfOutput extends GlmMembersFront_events_list { /** @@ -55,46 +56,4 @@ class GlmMembersAdmin_ajax_pdfOutput // extends GlmDataImages */ public $config; - /* - * Constructor - * - * This contructor sets up this model. At this time that only includes - * storing away the WordPress data object. - * - * @return object Class object - * - */ - public function __construct ($wpdb, $config) - { - - // Save WordPress Database object - $this->wpdb = $wpdb; - - // Save plugin configuration object - $this->config = $config; - - // Run constructor for members data class -// parent::__construct(false, false); - - } - - /* - * Perform Model Action - * - * This modelAction takes an AJAX image upload and stores the image in the - * media/images directory of the plugin. - * - * This model action does not return, it simply does it's work then calls die(); - * - * @param $actionData - * - * Echos JSON string as response and does not return - */ - public function modelAction ($actionData = false) - { - echo "Parmeter Stuff in my Events pdfOutput model = ".$_REQUEST['mystuff']."
"; - die(); - } - - } diff --git a/models/front/events/list.php b/models/front/events/list.php index d5fe325..1928951 100644 --- a/models/front/events/list.php +++ b/models/front/events/list.php @@ -22,6 +22,9 @@ require_once GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/models/front/events/baseAction.p */ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction { + const PDF_TOP_Y = 750; + const PDF_BOTTOM_Y = 60; + const LINE_HEIGHT = 12; /** * modelAction * @@ -51,9 +54,9 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction if (isset($_REQUEST['glm-event-pdf']) && $pdf = filter_var($_REQUEST['glm-event-pdf'], FILTER_VALIDATE_BOOLEAN)) { $search = true; $action = 'pdf'; - include '/var/www/localhost/htdocs/pdfTest/index.php'; + //include '/var/www/localhost/htdocs/pdfTest/index.php'; //echo '
$_REQUEST: ' . print_r($_REQUEST, true) . '
'; - exit; + //exit; } if ( isset($_REQUEST['glm_event_from']) ) { $fromDate = filter_var($_REQUEST['glm_event_from'], FILTER_SANITIZE_STRING); @@ -113,9 +116,92 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction $view = 'agenda.html'; break; case 'pdf': - include '/var/www/localhost/htdocs/pdfTest/index.php'; + include_once '/var/www/localhost/Setasign/SetaPDF-Core_2.18.0.817_Ioncubed-PHP5.4/library/SetaPDF/Autoload.php'; $events = $this->getModelEventsData($categoryId); - echo '
$events: ' . print_r($events, true) . '
'; + //echo '
$events: ' . print_r($events, true) . '
'; + //exit; + try { + $pdfY = self::PDF_TOP_Y; + $writer = new SetaPDF_Core_Writer_Http('events.pdf', true); + $document = new SetaPDF_Core_Document($writer); + // logo for PDF + $img = SetaPDF_Core_Image::getByPath('../assets/logoPdf.png'); + $xObject = $img->toXObject($document); + + $font = SetaPDF_Core_Font_Standard_Helvetica::create($document); + $boldFont = SetaPDF_Core_Font_Standard_HelveticaBold::create($document); + + $pages = $document->getCatalog()->getPages(); + $page = $pages->create(SetaPDF_Core_PageFormats::A4); + + // add logo to top of the page + $xObject->draw($page->getCanvas(), 20, 770, 500, 60); + + $totalEvents = count($events); + $index = 0; + + foreach ( $events as $eventId => $event ) { + // dates + $dates = new SetaPDF_Core_Text_Block( $boldFont, 8 ); + $dates->setText($event['dates']); + $lineCount = $dates->getLineCount(); + //echo '
dates $lineCount: ' . print_r($lineCount, true) . '
'; + $dates->draw($page->getCanvas(), 20, $pdfY); + + $pdfY -= self::LINE_HEIGHT * $lineCount; + //echo '
dates $pdfY: ' . print_r($pdfY, true) . '
'; + + // title + $title = new SetaPDF_Core_Text_Block( $boldFont, 8 ); + $title->setText($event['name']); + $lineCount = $title->getLineCount(); + //echo '
title $lineCount: ' . print_r($lineCount, true) . '
'; + $title->draw($page->getCanvas(), 20, $pdfY); + + $pdfY -= self::LINE_HEIGHT * $lineCount + 10; + //echo '
title $pdfY: ' . print_r($pdfY, true) . '
'; + + if ( isset($event['intro']) && $event['intro'] ) { + // text + $intro = new SetaPDF_Core_Text_Block( $font, 8 ); + $introText = html_entity_decode( + $this->cleanText( $event['intro'] ), + ENT_HTML5 + ); + $intro->setText($introText); + $intro->setWidth(500); + $lineCount = $intro->getLineCount(); + //echo '
intro $lineCount: ' . print_r($lineCount, true) . '
'; + $intro->draw($page->getCanvas(), 20, $pdfY); + + $pdfY -= self::LINE_HEIGHT * $lineCount; + if ( $lineCount == 1 ) { + $pdfY -= self::LINE_HEIGHT; + } + //echo '
intro $pdfY: ' . print_r($pdfY, true) . '
'; + } + ++$index; + + //$pdfY -= self::LINE_HEIGHT; + //echo '
$pdfY: ' . print_r($pdfY, true) . '
'; + + if ( $pdfY <= self::PDF_BOTTOM_Y && !( $totalEvents == $index ) ) { + $pdfY = self::PDF_TOP_Y; + $page = $pages->create(SetaPDF_Core_PageFormats::A4); + // add logo to top of the page + $xObject->draw($page->getCanvas(), 20, 770, 500, 60); + } + } + + + $document->save()->finish(); + + } catch(SetaPDF_Exception $e) { + echo '

SetaPDF_Exception: ' . $e->getMessage() . '

'; + } catch(Exception $e) { + echo '

Exception: ' . $e->getMessage() . '

'; + echo '
' . print_r( $e, true ) . '
'; + } exit; break; default: @@ -170,4 +256,13 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction 'settings' => $settings ); } + + public function cleanText($text) + { + $text = str_replace(' ', ' ', $text); + $text = str_replace('–', '-', $text); + $text = str_replace(''', "'", $text); + $text = str_replace("\n", '', $text); + return $text; + } } -- 2.17.1