Commit 6313190bde720d81a81013d380e8eb809fba0e92

Authored by Kevin Fourie
1 parent 09e07d2e

KTS-1735

"PDF Generation Plugin"
Implemented.

Reviewed By: Conrad



git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@6326 c91229c3-7414-0410-bfa2-8a42b809f60b
bin/openoffice/pdfgen.py 0 → 100755
  1 +#!/usr/bin/env python
  2 +#
  3 +#
  4 +# Copyright (c) 2007 Jam Warehouse http://www.jamwarehouse.com
  5 +#
  6 +# This program is free software; you can redistribute it and/or modify
  7 +# it under the terms of the GNU General Public License as published by
  8 +# the Free Software Foundation; using version 2 of the License.
  9 +#
  10 +# This program is distributed in the hope that it will be useful,
  11 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13 +# GNU General Public License for more details.
  14 +#
  15 +# You should have received a copy of the GNU General Public License
  16 +# along with this program; if not, write to the Free Software
  17 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18 +#
  19 +# -------------------------------------------------------------------------
  20 +#
  21 +# You can contact the copyright owner regarding licensing via the contact
  22 +# details that can be found on the KnowledgeTree web site:
  23 +#
  24 +# http://www.knowledgetree.com/
  25 +#
  26 +
  27 +import uno
  28 +import sys
  29 +from com.sun.star.beans import PropertyValue
  30 +
  31 +url_original = uno.systemPathToFileUrl(sys.argv[1])
  32 +url_save = uno.systemPathToFileUrl(sys.argv[2])
  33 +
  34 +### Get Service Manager
  35 +context = uno.getComponentContext()
  36 +resolver = context.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", context)
  37 +ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
  38 +smgr = ctx.ServiceManager
  39 +
  40 +### Load document
  41 +properties = []
  42 +p = PropertyValue()
  43 +p.Name = "Hidden"
  44 +p.Value = True
  45 +properties.append(p)
  46 +properties = tuple(properties)
  47 +
  48 +desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
  49 +doc = desktop.loadComponentFromURL(url_original, "_blank", 0, properties)
  50 +
  51 +### Save File
  52 +properties = []
  53 +p = PropertyValue()
  54 +p.Name = "Overwrite"
  55 +p.Value = True
  56 +properties.append(p)
  57 +p = PropertyValue()
  58 +p.Name = "FilterName"
  59 +p.Value = 'writer_pdf_Export'
  60 +properties.append(p)
  61 +properties = tuple(properties)
  62 +
  63 +doc.storeToURL(url_save, properties)
  64 +doc.dispose()
... ...
plugins/ktstandard/PDFGeneratorAction.php
1 1 <?php
  2 +/**
  3 + *
  4 + * Copyright (c) 2007 Jam Warehouse http://www.jamwarehouse.com
  5 + *
  6 + * This program is free software; you can redistribute it and/or modify
  7 + * it under the terms of the GNU General Public License as published by
  8 + * the Free Software Foundation; using version 2 of the License.
  9 + *
  10 + * This program is distributed in the hope that it will be useful,
  11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13 + * GNU General Public License for more details.
  14 + *
  15 + * You should have received a copy of the GNU General Public License
  16 + * along with this program; if not, write to the Free Software
  17 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18 + *
  19 + * -------------------------------------------------------------------------
  20 + *
  21 + * You can contact the copyright owner regarding licensing via the contact
  22 + * details that can be found on the KnowledgeTree web site:
  23 + *
  24 + * http://www.knowledgetree.com/
  25 + */
  26 +
2 27 require_once(KT_LIB_DIR . '/actions/folderaction.inc.php');
3 28 require_once(KT_LIB_DIR . '/permissions/permission.inc.php');
4 29 require_once(KT_LIB_DIR . '/permissions/permissionutil.inc.php');
... ... @@ -10,40 +35,90 @@ require_once(KT_LIB_DIR . &#39;/plugins/pluginregistry.inc.php&#39;);
10 35 require_once(KT_LIB_DIR . '/roles/Role.inc');
11 36  
12 37 class PDFGeneratorAction extends KTDocumentAction {
13   - var $sName = 'ktstandard.pdf.action';
  38 + var $sName = 'ktstandard.pdf.generate';
14 39 var $_sShowPermission = "ktcore.permissions.read";
15 40 var $sDisplayName = 'Generate PDF';
16   - var $aAcceptedMimeTypes = array('doc', 'ods', 'odt');
17   -
  41 + // Note: 'asc' below seems to be a catchall for plain text docs.
  42 + // 'htm' and 'html' should work but are not so have been removed for now.
  43 + var $aAcceptedMimeTypes = array('doc', 'ods', 'odt', 'ott', 'txt', 'rtf', 'sxw', 'stw',
  44 +// 'html', 'htm',
  45 + 'xml' , 'pdb', 'psw', 'ods', 'ots', 'sxc',
  46 + 'stc', 'dif', 'dbf', 'xls', 'xlt', 'slk', 'csv', 'pxl',
  47 + 'odp', 'otp', 'sxi', 'sti', 'ppt', 'pot', 'sxd', 'odg',
  48 + 'otg', 'std', 'asc');
  49 +
18 50 function getDisplayName() {
19   - // check if open office and plugin available
20   - $this->HAVE_GRAPHVIZ = false;
21   - $dotCommand = KTUtil::findCommand("ui/dot", 'dot');
22   - if (!empty($dotCommand)) {
23   - $this->HAVE_GRAPHVIZ = true;
24   - $this->dotCommand = $dotCommand;
25   - $sDocType = $this->getMimeExtension();
26   - // make sure that the selected document id of an acceptable extension
27   - foreach($this->aAcceptedMimeTypes as $acceptType){
28   - if($acceptType == $sDocType){
29   - return _kt('Generate PDF');
30   - }
31   - }
32   - }
  51 + // Check if openoffice and python are available
  52 + if(file_exists('../openoffice/program/python')) {
  53 + $sDocType = $this->getMimeExtension();
  54 + // make sure that the selected document id of an acceptable extension
  55 + foreach($this->aAcceptedMimeTypes as $acceptType){
  56 + if($acceptType == $sDocType){
  57 + return _kt('Generate PDF') . "&nbsp;<a href=\"" . KTUtil::ktLink( 'action.php', 'ktstandard.pdf.generate', array( "fDocumentId" => $this->oDocument->getId(), "action" => "pdfdownload") ) . "\" <img src='resources/mimetypes/pdf.png' alt='PDF' border=0/></a>";
  58 + }
  59 + }
  60 + }
33 61 return '';
34 62 }
  63 +
  64 + function form_main() {
  65 + $oForm = new KTForm;
  66 + $oForm->setOptions(array(
  67 + 'label' => _kt('Convert Document to PDF'),
  68 + 'action' => 'selectType',
  69 + 'fail_action' => 'main',
  70 + 'cancel_url' => KTBrowseUtil::getUrlForDocument($this->oDocument),
  71 + 'submit_label' => _kt('Convert Document'),
  72 + 'context' => &$this,
  73 + ));
  74 +
  75 + $oForm->setWidgets(array(
  76 + array('ktcore.widgets.selection', array(
  77 + 'label' => _kt("Type of conversion"),
  78 + 'description' => _kt('The following are the types of conversions you can perform on this document.'),
  79 + 'important_description' => _kt('QA NOTE: Permissions checks are required here...'),
  80 + 'name' => 'convert_type',
  81 + 'vocab' => array('Download as PDF', 'Duplicate as PDF', 'Replace as PDF'),
  82 + 'simple_select' => true,
  83 + 'required' => true,
  84 + )),
  85 + ));
  86 +
  87 + return $oForm;
  88 + }
35 89  
36   - function do_main() {
37   - $oTemplating =& KTTemplating::getSingleton();
38   - $oTemplate = $oTemplating->loadTemplate('ktstandard/PDFPlugin/PDFPlugin');
39   -
40   - $aTemplateData = array(
41   - 'context' => $this,
42   - );
43   -
44   - return $oTemplate->render($aTemplateData);
  90 + function do_selectType() {
  91 +
  92 + switch($_REQUEST[data][convert_type]){
  93 + case '0':
  94 + $this->do_pdfdownload();
  95 + break;
  96 + case '1':
  97 + $this->do_pdfduplicate();
  98 + break;
  99 + case '2':
  100 + $this->do_pdfreplace();
  101 + break;
  102 + default:
  103 + $this->do_pdfdownload();
  104 + }
  105 + redirect(KTUtil::ktLink( 'action.php', 'ktstandard.pdf.generate', array( "fDocumentId" => $this->oDocument->getId() ) ) );
  106 + exit(0);
45 107 }
46   -
  108 +
  109 + function do_main() {
  110 + $this->oPage->setBreadcrumbDetails(_kt('Generate PDF'));
  111 + $oTemplate =& $this->oValidator->validateTemplate('ktstandard/PDFPlugin/PDFPlugin');
  112 +
  113 + $oForm = $this->form_main();
  114 +
  115 + $oTemplate->setData(array(
  116 + 'context' => &$this,
  117 + 'form' => $oForm,
  118 + ));
  119 + return $oTemplate->render();
  120 + }
  121 +
47 122 /**
48 123 * Method for getting the MIME type extension for the current document.
49 124 *
... ... @@ -51,20 +126,129 @@ class PDFGeneratorAction extends KTDocumentAction {
51 126 */
52 127 function getMimeExtension() {
53 128 $oDocument = $this->oDocument;
54   - $iMimeTypeId = $oDocument->getMimeTypeID();
55   - $mimetypename = KTMime::getMimeTypeName($iMimeTypeId); // mime type name
56   -
  129 + $iMimeTypeId = $oDocument->getMimeTypeID();
  130 + $mimetypename = KTMime::getMimeTypeName($iMimeTypeId); // mime type name
  131 +
57 132 $sTable = KTUtil::getTableName('mimetypes');
58 133 $sQuery = "SELECT filetypes FROM " . $sTable . " WHERE mimetypes = ?";
59 134 $aQuery = array($sQuery, array($mimetypename));
60 135 $res = DBUtil::getResultArray($aQuery);
61   - if (PEAR::isError($res)) {
  136 + if (PEAR::isError($res)) {
62 137 return $res;
63 138 } else if (count($res) != 0){
64   - return $res[0]['filetypes'];
  139 + return $res[0]['filetypes'];
65 140 }
66   -
  141 +
67 142 return _kt('Unknown Type');
68 143 }
  144 +
  145 + /**
  146 + * Method for downloading the document as a pdf.
  147 + *
  148 + * @return true on success else false
  149 + */
  150 + function do_pdfdownload() {
  151 +
  152 + $oDocument = $this->oDocument;
  153 + $oStorage =& KTStorageManagerUtil::getSingleton();
  154 + $oConfig =& KTConfig::getSingleton();
  155 +
  156 + //get the actual path to the document on the server
  157 + $sPath = sprintf("%s/%s", $oConfig->get('urls/documentRoot'), $oStorage->getPath($oDocument));
  158 +
  159 + if (file_exists($sPath)) {
  160 +
  161 + # Get a tmp file
  162 + $sTempFilename = tempnam('/tmp', 'ktpdf');
  163 +
  164 + // TODO: Check for more errors here
  165 + // SECURTIY: Ensure $sPath and $sTempFilename are safe or they could be used to excecute arbitrary commands!
  166 + // Excecute the python script. TODO: Check this works with Windows
  167 + $res = exec('../openoffice/program/python bin/openoffice/pdfgen.py ' . escapeshellcmd($sPath) . ' ' . escapeshellcmd($sTempFilename) );
  168 +
  169 + # Check the tempfile exists and the python script did not return anything (which would indicate an error)
  170 + if (file_exists($sTempFilename) && $res == '') {
  171 +
  172 + $sUrlEncodedFileName = substr($oDocument->getFileName(), 0, strrpos($oDocument->getFileName(), '.') );
  173 + $browser = $_SERVER['HTTP_USER_AGENT'];
  174 + if ( strpos( strtoupper( $browser), 'MSIE') !== false) {
  175 + $sUrlEncodedFileName = rawurlencode($sUrlEncodedFileName);
  176 + }
  177 + // Set the correct headers
  178 + header("Content-Type: application/pdf");
  179 + header("Content-Length: ". filesize($sTempFilename));
  180 + header("Content-Disposition: attachment; filename=\"" . $sUrlEncodedFileName . ".pdf\"");
  181 + header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  182 + header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  183 + header("Cache-Control: must-revalidate");
  184 +
  185 + # Get a filelike object and send it to the browser
  186 + $oFile = new KTFSFileLike($sTempFilename);
  187 + KTFileLikeUtil::send_contents($oFile);
  188 + # Remove the tempfile
  189 + unlink($sTempFilename);
  190 +
  191 + # Create the document transaction
  192 + $oDocumentTransaction = & new DocumentTransaction($oDocument, 'Document downloaded as PDF', 'ktcore.transactions.download', $aOptions);
  193 + $oDocumentTransaction->create();
  194 +
  195 + return true;
  196 +
  197 + } else {
  198 + # Set the error messsage and redirect to view document
  199 + $this->addErrorMessage(_kt('An error occured generating the PDF - please contact the system administrator.'));
  200 + redirect(generateControllerLink('viewDocument',sprintf(_kt('fDocumentId=%d'),$oDocument->getId())));
  201 + exit(0);
  202 + }
  203 +
  204 + } else {
  205 + # Set the error messsage and redirect to view document
  206 + $this->addErrorMessage(_kt('An error occured generating the PDF - please contact the system administrator.'));
  207 + redirect(generateControllerLink('viewDocument',sprintf(_kt('fDocumentId=%d'),$oDocument->getId())));
  208 + exit(0);
  209 + }
  210 +
  211 +
  212 + }
  213 +
  214 + /**
  215 + * Method for duplicating the document as a pdf.
  216 + *
  217 + */
  218 + function do_pdfduplicate() {
  219 +
  220 + $this->oPage->setBreadcrumbDetails(_kt('Generate PDF'));
  221 + $oTemplate =& $this->oValidator->validateTemplate('ktstandard/PDFPlugin/PDFPlugin');
  222 +
  223 + $oForm = $this->form_main();
  224 +
  225 + $oTemplate->setData(array(
  226 + 'context' => &$this,
  227 + 'form' => $oForm,
  228 + ));
  229 + $this->addErrorMessage(_kt('NOT IMPLEMENTED YET: This will create a pdf copy of the document as a new document.'));
  230 + return $oTemplate->render();
  231 +
  232 + }
  233 +
  234 + /**
  235 + * Method for replacing the document as a pdf.
  236 + *
  237 + */
  238 + function do_pdfreplace() {
  239 +
  240 + $this->oPage->setBreadcrumbDetails(_kt('Generate PDF'));
  241 + $oTemplate =& $this->oValidator->validateTemplate('ktstandard/PDFPlugin/PDFPlugin');
  242 +
  243 + $oForm = $this->form_main();
  244 +
  245 + $oTemplate->setData(array(
  246 + 'context' => &$this,
  247 + 'form' => $oForm,
  248 + ));
  249 + $this->addErrorMessage(_kt('NOT IMPLEMENTED YET: This will replace the document with a pdf copy of the document.'));
  250 + return $oTemplate->render();
  251 +
  252 + }
69 253 }
70   -?>
71 254 \ No newline at end of file
  255 +?>
... ...
plugins/ktstandard/PDFGeneratorPlugin.php
1 1 <?php
2   -/*
3   - * Created on 03 Jan 2007
  2 +/**
4 3 *
5   - * To change the template for this generated file go to
6   - * Window - Preferences - PHPeclipse - PHP - Code Templates
  4 + * Copyright (c) 2007 Jam Warehouse http://www.jamwarehouse.com
  5 + *
  6 + * This program is free software; you can redistribute it and/or modify
  7 + * it under the terms of the GNU General Public License as published by
  8 + * the Free Software Foundation; using version 2 of the License.
  9 + *
  10 + * This program is distributed in the hope that it will be useful,
  11 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13 + * GNU General Public License for more details.
  14 + *
  15 + * You should have received a copy of the GNU General Public License
  16 + * along with this program; if not, write to the Free Software
  17 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18 + *
  19 + * -------------------------------------------------------------------------
  20 + *
  21 + * You can contact the copyright owner regarding licensing via the contact
  22 + * details that can be found on the KnowledgeTree web site:
  23 + *
  24 + * http://www.knowledgetree.com/
7 25 */
8   -
  26 +
9 27 require_once(KT_LIB_DIR . "/plugins/plugin.inc.php");
10 28 require_once(KT_LIB_DIR . "/plugins/pluginregistry.inc.php");
11 29 require_once('PDFGeneratorAction.php');
... ... @@ -21,7 +39,7 @@ require_once(&#39;PDFGeneratorAction.php&#39;);
21 39 }
22 40  
23 41 function setup() {
24   - $this->registerAction('documentaction', 'PDFGeneratorAction', 'ktstandard.pdf.action', $sFilename = null);
  42 + $this->registerAction('documentaction', 'PDFGeneratorAction', 'ktstandard.pdf.generate', $sFilename = null);
25 43 }
26 44 }
27 45 $oPluginRegistry =& KTPluginRegistry::getSingleton();
... ...
templates/ktstandard/PDFPlugin/PDFPlugin.smarty
1   -<h2><img src="{$rootUrl}/resources/graphics/title_bullet.png"/>PDF Generation</h2>
  1 +<h2><img src="{$rootUrl}/resources/graphics/title_bullet.png"/>{i18n arg_docname=$context->oDocument->getName()}Convert to PDF: #docname#{/i18n}</h2>
  2 +
  3 +{$form->render()}
... ...