Adding content for generating PDF
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 30 Mar 2016 20:20:44 +0000 (16:20 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 30 Mar 2016 20:20:44 +0000 (16:20 -0400)
Temporarily using the library that I have with ionCube which is there
eval version.

models/admin/ajax/pdfOutput.php
models/front/events/list.php

index 360398b..a9b7a34 100644 (file)
@@ -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.
 // 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']."<br>";
-        die();
-    }
-
-
 }
index d5fe325..1928951 100644 (file)
@@ -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 '<pre>$_REQUEST: ' . print_r($_REQUEST, true) . '</pre>';
-            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 '<pre>$events: ' . print_r($events, true) . '</pre>';
+            //echo '<pre>$events: ' . print_r($events, true) . '</pre>';
+            //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 '<pre>dates $lineCount: ' . print_r($lineCount, true) . '</pre>';
+                    $dates->draw($page->getCanvas(), 20, $pdfY);
+
+                    $pdfY -= self::LINE_HEIGHT * $lineCount;
+                    //echo '<pre>dates $pdfY: ' . print_r($pdfY, true) . '</pre>';
+
+                    // title
+                    $title = new SetaPDF_Core_Text_Block( $boldFont, 8 );
+                    $title->setText($event['name']);
+                    $lineCount = $title->getLineCount();
+                    //echo '<pre>title $lineCount: ' . print_r($lineCount, true) . '</pre>';
+                    $title->draw($page->getCanvas(), 20, $pdfY);
+
+                    $pdfY -= self::LINE_HEIGHT * $lineCount  + 10;
+                    //echo '<pre>title $pdfY: ' . print_r($pdfY, true) . '</pre>';
+
+                    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 '<pre>intro $lineCount: ' . print_r($lineCount, true) . '</pre>';
+                        $intro->draw($page->getCanvas(), 20, $pdfY);
+
+                        $pdfY -= self::LINE_HEIGHT * $lineCount;
+                        if ( $lineCount == 1 ) {
+                             $pdfY -= self::LINE_HEIGHT;
+                        }
+                    //echo '<pre>intro $pdfY: ' . print_r($pdfY, true) . '</pre>';
+                    }
+                    ++$index;
+
+                    //$pdfY -= self::LINE_HEIGHT;
+                    //echo '<pre>$pdfY: ' . print_r($pdfY, true) . '</pre>';
+
+                    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 '<p>SetaPDF_Exception: ' . $e->getMessage() . '</p>';
+            } catch(Exception $e) {
+                echo '<p>Exception: ' . $e->getMessage() . '</p>';
+                echo '<pre>' . print_r( $e, true ) . '</pre>';
+            }
             exit;
             break;
         default:
@@ -170,4 +256,13 @@ class GlmMembersFront_events_list extends GlmMembersFront_events_baseAction
             'settings'         => $settings
         );
     }
+
+    public function cleanText($text)
+    {
+        $text = str_replace('&nbsp;', ' ', $text);
+        $text = str_replace('&ndash;', '-', $text);
+        $text = str_replace('&#39;', "'", $text);
+        $text = str_replace("\n", '', $text);
+        return $text;
+    }
 }