Overview
  • Namespace
  • Class

Namespaces

  • nk2580
    • wordsmith
      • Actions
      • Endpoints
      • Environment
      • Filters
      • Inputs
        • Fields
      • Menus
      • MetaBoxes
      • PostTypes
      • Routes
      • Scripts
      • SettingsPages
      • Taxonomies
        • Hierarchical
        • NonHierarchical
      • UserCapabilities
      • UserRoles
      • Utillities
        • NavWalkers

Classes

  • nk2580\wordsmith\Actions\Action
  • nk2580\wordsmith\Actions\AJAXAction
  • nk2580\wordsmith\Endpoints\Endpoint
  • nk2580\wordsmith\Endpoints\EndpointHandler
  • nk2580\wordsmith\Environment\Instance
  • nk2580\wordsmith\Environment\Plugin
  • nk2580\wordsmith\Environment\Theme
  • nk2580\wordsmith\Filters\Filter
  • nk2580\wordsmith\Inputs\Fields\AddressField
  • nk2580\wordsmith\Inputs\Fields\CheckBoxField
  • nk2580\wordsmith\Inputs\Fields\EmailField
  • nk2580\wordsmith\Inputs\Fields\FileUploadField
  • nk2580\wordsmith\Inputs\Fields\GalleryField
  • nk2580\wordsmith\Inputs\Fields\HiddenField
  • nk2580\wordsmith\Inputs\Fields\ImageUploadField
  • nk2580\wordsmith\Inputs\Fields\PasswordField
  • nk2580\wordsmith\Inputs\Fields\RadioButtonField
  • nk2580\wordsmith\Inputs\Fields\SelectBoxField
  • nk2580\wordsmith\Inputs\Fields\TextAreaField
  • nk2580\wordsmith\Inputs\Fields\TextField
  • nk2580\wordsmith\Inputs\Fields\UrlField
  • nk2580\wordsmith\Inputs\Input
  • nk2580\wordsmith\Inputs\Repeater
  • nk2580\wordsmith\Menus\Menu
  • nk2580\wordsmith\MetaBoxes\MetaBox
  • nk2580\wordsmith\PostTypes\PostType
  • nk2580\wordsmith\Routes\Route
  • nk2580\wordsmith\Routes\RouteFactory
  • nk2580\wordsmith\Routes\RouteGroup
  • nk2580\wordsmith\Scripts\AdminScript
  • nk2580\wordsmith\Scripts\PublicScript
  • nk2580\wordsmith\SettingsPages\SettingsPage
  • nk2580\wordsmith\Taxonomies\Hierarchical\HierarchicalTaxonomy
  • nk2580\wordsmith\Taxonomies\NonHierarchical\NonHierarchicalTaxonomy
  • nk2580\wordsmith\Taxonomies\Taxonomy
  • nk2580\wordsmith\UserCapabilities\UserCapability
  • nk2580\wordsmith\UserRoles\UserRole
  • nk2580\wordsmith\Utillities\Cypher
  • nk2580\wordsmith\Utillities\JsonResponse
  • nk2580\wordsmith\Utillities\NavWalkers\Bootstrap
  • nk2580\wordsmith\Utillities\PDF
 1 <?php
 2 
 3 /*
 4  * To change this license header, choose License Headers in Project Properties.
 5  * To change this template file, choose Tools | Templates
 6  * and open the template in the editor.
 7  */
 8 
 9 namespace nk2580\wordsmith\Utillities;
10 
11 use \Knp\Snappy\Pdf as Snappy;
12 
13 /**
14  * PDF Exporter Wrapper for snappy PDF - this utillitiy relies uppon the wkhtmltopdf binary
15  * please ensure your server has access to this binary before using this class or it will return an error
16  * for information on this binary  - 
17  *
18  * @author accounts
19  */
20 class PDF {
21 
22     public $snappy;
23 
24     public function __construct($path = NULL) {
25         if (!empty($path)) {
26             $pdf_binary_path = $path;
27         } else if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
28             $pdf_binary_path = 'C:\wkhtmltopdf\bin\wkhtmltopdf.exe';
29         } else {
30             $pdf_binary_path = '/usr/local/bin/wkhtmltopdf';
31         }
32         $this->snappy = new Snappy($pdf_binary_path);
33         $upload_dir = wp_upload_dir();
34         $this->snappy->setTemporaryFolder($upload_dir['path']);
35     }
36 
37     /**
38      * Generate a PDF from a URL and return its filename
39      * 
40      * @param String $url
41      * @param String $filename
42      * @param Array $options
43      * @return String
44      */
45     public function generate($url, $filename, $options = array()) {
46         $this->cleanExport($filename);
47         $this->snappy->generate($url, $filename, $options);
48         return $filename;
49     }
50 
51     /**
52      * Generate a PDF from HTML and reutrn it's filename
53      *  
54      * @param String $html
55      * @param String $filename
56      * @param Array $options
57      * @return String 
58      */
59     public function generateFromHtml($html, $filename, $options = array()) {
60         $this->cleanExport($filename);
61         $this->snappy->generateFromHtml($html, $filename, $options);
62         return $filename;
63     }
64 
65     /**
66      * show the output of a PDF generated on the fly with a URL
67      * 
68      * @param String $url
69      * @param Array $options
70      */
71     public function output($url, $filename, $options = array()) {
72         header('Content-Type: application/pdf');
73         header('Content-Disposition: attachment; filename="' . $filename . '"');
74         echo $this->snappy->getOutput($url, $options);
75     }
76 
77     /**
78      * show the output of a PDF generated on the fly with HTML
79      * 
80      * @param String $html
81      * @param Array $options
82      */
83     public function outputFromHtml($html, $filename, $options = array()) {
84         header('Content-Type: application/pdf');
85         header('Content-Disposition: attachment; filename="' . $filename . '"');
86         echo $this->snappy->getOutputFromHtml($html, $options);
87     }
88 
89     private function cleanExport($filename) {
90         if (isset($filename) && file_exists($filename)) {
91             unlink($filename);
92         }
93     }
94 
95 }
96 
API documentation generated by ApiGen