diff --git a/bin/openoffice/pdfgen.py b/bin/openoffice/pdfgen.py index c10498c..111f329 100755 --- a/bin/openoffice/pdfgen.py +++ b/bin/openoffice/pdfgen.py @@ -1,28 +1,28 @@ #!/usr/bin/env python # +# +# $Id: view.php 6203 2007-02-08 10:41:00Z kevin_fourie $ +# +# The contents of this file are subject to the KnowledgeTree Public +# License Version 1.1 ("License"); You may not use this file except in +# compliance with the License. You may obtain a copy of the License at +# http://www.knowledgetree.com/KPL # -# Copyright (c) 2007 Jam Warehouse http://www.jamwarehouse.com -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; using version 2 of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# -# ------------------------------------------------------------------------- -# -# You can contact the copyright owner regarding licensing via the contact -# details that can be found on the KnowledgeTree web site: +# Software distributed under the License is distributed on an "AS IS" +# basis, +# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License +# for the specific language governing rights and limitations under the +# License. # -# http://www.knowledgetree.com/ +# The Original Code is: KnowledgeTree Open Source # +# The Initial Developer of the Original Code is The Jam Warehouse Software +# (Pty) Ltd, trading as KnowledgeTree. +# Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright +# (C) 2007 The Jam Warehouse Software (Pty) Ltd; +# All Rights Reserved. +# +# import uno import sys @@ -31,22 +31,47 @@ from com.sun.star.beans import PropertyValue url_original = uno.systemPathToFileUrl(sys.argv[1]) url_save = uno.systemPathToFileUrl(sys.argv[2]) -### Get Service Manager -context = uno.getComponentContext() -resolver = context.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", context) -ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext") -smgr = ctx.ServiceManager +try: + ### Get Service Manager + context = uno.getComponentContext() + resolver = context.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", context) + ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext") + smgr = ctx.ServiceManager -### Load document -properties = [] -p = PropertyValue() -p.Name = "Hidden" -p.Value = True -properties.append(p) -properties = tuple(properties) + ### Load document + properties = [] + p = PropertyValue() + p.Name = "Hidden" + p.Value = True + properties.append(p) + properties = tuple(properties) + + desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx) + +except NoConnectException, e: + sys.stderr.write("OpenOffice process not found or not listening (" + e.Message + ")\n") + sys.exit(1) +except IllegalArgumentException, e: + sys.stderr.write("The url is invalid ( " + e.Message + ")\n") + sys.exit(1) +except RuntimeException, e: + sys.stderr.write("An unknown error occured: " + e.Message + "\n") + +try: + doc = desktop.loadComponentFromURL(url_original, "_blank", 0, properties) +except IOException, e: + sys.stderr.write("URL couldn't be found or was corrupt (" + e.Message + ")\n") + sys.exit(1) +except IllegalArgumentException, e: + sys.stderr.write("Given parameters doesn't conform to the specification ( " + e.Message + ")\n") + sys.exit(1) +except RuntimeException, e: + sys.stderr.write("An unknown error occured: " + e.Message + "\n") -desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx) -doc = desktop.loadComponentFromURL(url_original, "_blank", 0, properties) +if doc == None: + sys.stderr.write("Could not load doc.\n") + sys.exit(1) + ### Save File properties = [] diff --git a/plugins/ktstandard/PDFGeneratorAction.php b/plugins/ktstandard/PDFGeneratorAction.php index 74d6b6f..6f84903 100644 --- a/plugins/ktstandard/PDFGeneratorAction.php +++ b/plugins/ktstandard/PDFGeneratorAction.php @@ -1,27 +1,26 @@ setOptions(array( - 'label' => _kt('Convert Document to PDF'), - 'action' => 'selectType', - 'fail_action' => 'main', - 'cancel_url' => KTBrowseUtil::getUrlForDocument($this->oDocument), - 'submit_label' => _kt('Convert Document'), - 'context' => &$this, - )); - + 'label' => _kt('Convert Document to PDF'), + 'action' => 'selectType', + 'fail_action' => 'main', + 'cancel_url' => KTBrowseUtil::getUrlForDocument($this->oDocument), + 'submit_label' => _kt('Convert Document'), + 'context' => &$this, + )); + $oForm->setWidgets(array( - array('ktcore.widgets.selection', array( - 'label' => _kt("Type of conversion"), - 'description' => _kt('The following are the types of conversions you can perform on this document.'), - 'important_description' => _kt('QA NOTE: Permissions checks are required here...'), - 'name' => 'convert_type', - 'vocab' => array('Download as PDF', 'Duplicate as PDF', 'Replace as PDF'), - 'simple_select' => true, - 'required' => true, - )), - )); - + array('ktcore.widgets.selection', array( + 'label' => _kt("Type of conversion"), + 'description' => _kt('The following are the types of conversions you can perform on this document.'), + 'important_description' => _kt('QA NOTE: Permissions checks are required here...'), + 'name' => 'convert_type', + 'vocab' => array('Download as PDF', 'Duplicate as PDF', 'Replace as PDF'), + 'simple_select' => true, + 'required' => true, + )), + )); + return $oForm; } function do_selectType() { - + switch($_REQUEST[data][convert_type]){ case '0': $this->do_pdfdownload(); @@ -120,20 +119,20 @@ class PDFGeneratorAction extends KTDocumentAction { $oForm = $this->form_main(); $oTemplate->setData(array( - 'context' => &$this, - 'form' => $oForm, - )); + 'context' => &$this, + 'form' => $oForm, + )); return $oTemplate->render(); } /** - * Method for getting the MIME type extension for the current document. - * - * @return string mime time extension - */ + * Method for getting the MIME type extension for the current document. + * + * @return string mime time extension + */ function getMimeExtension() { - if($this->Document == null || $this->Document == "" || PEAR::isError($this->Document) ) return _kt('Unknown Type'); + if($this->oDocument == null || $this->oDocument == "" || PEAR::isError($this->oDocument) ) return _kt('Unknown Type'); $oDocument = $this->oDocument; $iMimeTypeId = $oDocument->getMimeTypeID(); @@ -153,10 +152,10 @@ class PDFGeneratorAction extends KTDocumentAction { } /** - * Method for downloading the document as a pdf. - * - * @return true on success else false - */ + * Method for downloading the document as a pdf. + * + * @return true on success else false + */ function do_pdfdownload() { $oDocument = $this->oDocument; @@ -165,33 +164,40 @@ class PDFGeneratorAction extends KTDocumentAction { //get the actual path to the document on the server $sPath = sprintf("%s/%s", $oConfig->get('urls/documentRoot'), $oStorage->getPath($oDocument)); - + if (file_exists($sPath)) { - # Get a tmp file +# Get a tmp file $sTempFilename = tempnam('/tmp', 'ktpdf'); - - # We need to handle Windows differently - as usual ;) + +# We need to handle Windows differently - as usual ;) if (substr( PHP_OS, 0, 3) == 'WIN') { - - $cmd = "\"" . KT_DIR . "/../openoffice/openoffice/program/python.bat\" \"". KT_DIR . "/bin/openoffice/pdfgen.py\" \"" . $sPath . "\" \"" . $sTempFilename . "\""; - $cmd = str_replace( '/','\\',$cmd); - + + $cmd = "\"" . KT_DIR . "/../openoffice/openoffice/program/python.bat\" \"". KT_DIR . "/bin/openoffice/pdfgen.py\" \"" . $sPath . "\" \"" . $sTempFilename . "\""; + $cmd = str_replace( '/','\\',$cmd); + // TODO: Check for more errors here // SECURTIY: Ensure $sPath and $sTempFilename are safe or they could be used to excecute arbitrary commands! // Excecute the python script. TODO: Check this works with Windows - $res = `"$cmd" 2>&1`; - + $res = `"$cmd" 2>&1`; + //print($res); + //print($cmd); + //exit; + } else { // TODO: Check for more errors here // SECURTIY: Ensure $sPath and $sTempFilename are safe or they could be used to excecute arbitrary commands! - // Excecute the python script. TODO: Check this works with Windows - $res = exec('../openoffice/program/python bin/openoffice/pdfgen.py ' . escapeshellcmd($sPath) . ' ' . escapeshellcmd($sTempFilename) ); - + // Excecute the python script. + $cmd = '../openoffice/program/python bin/openoffice/pdfgen.py ' . escapeshellcmd($sPath) . ' ' . escapeshellcmd($sTempFilename); + $res = shell_exec($cmd." 2>&1"); + //print($res); + //print($cmd); + //exit; + } - - # Check the tempfile exists and the python script did not return anything (which would indicate an error) + +# Check the tempfile exists and the python script did not return anything (which would indicate an error) if (file_exists($sTempFilename) && $res == '') { $sUrlEncodedFileName = substr($oDocument->getFileName(), 0, strrpos($oDocument->getFileName(), '.') ); @@ -207,27 +213,27 @@ class PDFGeneratorAction extends KTDocumentAction { header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: must-revalidate"); - # Get a filelike object and send it to the browser +# Get a filelike object and send it to the browser $oFile = new KTFSFileLike($sTempFilename); KTFileLikeUtil::send_contents($oFile); - # Remove the tempfile +# Remove the tempfile unlink($sTempFilename); - # Create the document transaction +# Create the document transaction $oDocumentTransaction = & new DocumentTransaction($oDocument, 'Document downloaded as PDF', 'ktcore.transactions.download', $aOptions); $oDocumentTransaction->create(); - # Just stop here - the content has already been sent. +# Just stop here - the content has already been sent. exit(0); - + } else { - # Set the error messsage and redirect to view document +# Set the error messsage and redirect to view document $this->addErrorMessage(_kt('An error occured generating the PDF - please contact the system administrator.')); redirect(generateControllerLink('viewDocument',sprintf(_kt('fDocumentId=%d'),$oDocument->getId()))); exit(0); } - + } else { - # Set the error messsage and redirect to view document +# Set the error messsage and redirect to view document $this->addErrorMessage(_kt('An error occured generating the PDF - please contact the system administrator.')); redirect(generateControllerLink('viewDocument',sprintf(_kt('fDocumentId=%d'),$oDocument->getId()))); exit(0); @@ -237,9 +243,9 @@ class PDFGeneratorAction extends KTDocumentAction { } /** - * Method for duplicating the document as a pdf. - * - */ + * Method for duplicating the document as a pdf. + * + */ function do_pdfduplicate() { $this->oPage->setBreadcrumbDetails(_kt('Generate PDF')); @@ -248,18 +254,18 @@ class PDFGeneratorAction extends KTDocumentAction { $oForm = $this->form_main(); $oTemplate->setData(array( - 'context' => &$this, - 'form' => $oForm, - )); + 'context' => &$this, + 'form' => $oForm, + )); $this->addErrorMessage(_kt('NOT IMPLEMENTED YET: This will create a pdf copy of the document as a new document.')); return $oTemplate->render(); - + } /** - * Method for replacing the document as a pdf. - * - */ + * Method for replacing the document as a pdf. + * + */ function do_pdfreplace() { $this->oPage->setBreadcrumbDetails(_kt('Generate PDF')); @@ -268,12 +274,12 @@ class PDFGeneratorAction extends KTDocumentAction { $oForm = $this->form_main(); $oTemplate->setData(array( - 'context' => &$this, - 'form' => $oForm, - )); + 'context' => &$this, + 'form' => $oForm, + )); $this->addErrorMessage(_kt('NOT IMPLEMENTED YET: This will replace the document with a pdf copy of the document.')); return $oTemplate->render(); - + } } ?> diff --git a/plugins/ktstandard/PDFGeneratorPlugin.php b/plugins/ktstandard/PDFGeneratorPlugin.php index a783702..e3e9ed2 100644 --- a/plugins/ktstandard/PDFGeneratorPlugin.php +++ b/plugins/ktstandard/PDFGeneratorPlugin.php @@ -1,27 +1,26 @@