From 6313190bde720d81a81013d380e8eb809fba0e92 Mon Sep 17 00:00:00 2001 From: Kevin Fourie Date: Sun, 25 Feb 2007 19:31:57 +0000 Subject: [PATCH] KTS-1735 "PDF Generation Plugin" Implemented. --- bin/openoffice/pdfgen.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/ktstandard/PDFGeneratorAction.php | 252 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------- plugins/ktstandard/PDFGeneratorPlugin.php | 30 ++++++++++++++++++++++++------ templates/ktstandard/PDFPlugin/PDFPlugin.smarty | 4 +++- 4 files changed, 309 insertions(+), 41 deletions(-) create mode 100755 bin/openoffice/pdfgen.py diff --git a/bin/openoffice/pdfgen.py b/bin/openoffice/pdfgen.py new file mode 100755 index 0000000..c10498c --- /dev/null +++ b/bin/openoffice/pdfgen.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python +# +# +# 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: +# +# http://www.knowledgetree.com/ +# + +import uno +import sys +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 + +### 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) +doc = desktop.loadComponentFromURL(url_original, "_blank", 0, properties) + +### Save File +properties = [] +p = PropertyValue() +p.Name = "Overwrite" +p.Value = True +properties.append(p) +p = PropertyValue() +p.Name = "FilterName" +p.Value = 'writer_pdf_Export' +properties.append(p) +properties = tuple(properties) + +doc.storeToURL(url_save, properties) +doc.dispose() diff --git a/plugins/ktstandard/PDFGeneratorAction.php b/plugins/ktstandard/PDFGeneratorAction.php index 7c20a80..22e88b3 100644 --- a/plugins/ktstandard/PDFGeneratorAction.php +++ b/plugins/ktstandard/PDFGeneratorAction.php @@ -1,4 +1,29 @@ HAVE_GRAPHVIZ = false; - $dotCommand = KTUtil::findCommand("ui/dot", 'dot'); - if (!empty($dotCommand)) { - $this->HAVE_GRAPHVIZ = true; - $this->dotCommand = $dotCommand; - $sDocType = $this->getMimeExtension(); - // make sure that the selected document id of an acceptable extension - foreach($this->aAcceptedMimeTypes as $acceptType){ - if($acceptType == $sDocType){ - return _kt('Generate PDF'); - } - } - } + // Check if openoffice and python are available + if(file_exists('../openoffice/program/python')) { + $sDocType = $this->getMimeExtension(); + // make sure that the selected document id of an acceptable extension + foreach($this->aAcceptedMimeTypes as $acceptType){ + if($acceptType == $sDocType){ + return _kt('Generate PDF') . "  $this->oDocument->getId(), "action" => "pdfdownload") ) . "\" PDF"; + } + } + } return ''; } + + function form_main() { + $oForm = new KTForm; + $oForm->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, + )); + + $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, + )), + )); + + return $oForm; + } - function do_main() { - $oTemplating =& KTTemplating::getSingleton(); - $oTemplate = $oTemplating->loadTemplate('ktstandard/PDFPlugin/PDFPlugin'); - - $aTemplateData = array( - 'context' => $this, - ); - - return $oTemplate->render($aTemplateData); + function do_selectType() { + + switch($_REQUEST[data][convert_type]){ + case '0': + $this->do_pdfdownload(); + break; + case '1': + $this->do_pdfduplicate(); + break; + case '2': + $this->do_pdfreplace(); + break; + default: + $this->do_pdfdownload(); + } + redirect(KTUtil::ktLink( 'action.php', 'ktstandard.pdf.generate', array( "fDocumentId" => $this->oDocument->getId() ) ) ); + exit(0); } - + + function do_main() { + $this->oPage->setBreadcrumbDetails(_kt('Generate PDF')); + $oTemplate =& $this->oValidator->validateTemplate('ktstandard/PDFPlugin/PDFPlugin'); + + $oForm = $this->form_main(); + + $oTemplate->setData(array( + 'context' => &$this, + 'form' => $oForm, + )); + return $oTemplate->render(); + } + /** * Method for getting the MIME type extension for the current document. * @@ -51,20 +126,129 @@ class PDFGeneratorAction extends KTDocumentAction { */ function getMimeExtension() { $oDocument = $this->oDocument; - $iMimeTypeId = $oDocument->getMimeTypeID(); - $mimetypename = KTMime::getMimeTypeName($iMimeTypeId); // mime type name - + $iMimeTypeId = $oDocument->getMimeTypeID(); + $mimetypename = KTMime::getMimeTypeName($iMimeTypeId); // mime type name + $sTable = KTUtil::getTableName('mimetypes'); $sQuery = "SELECT filetypes FROM " . $sTable . " WHERE mimetypes = ?"; $aQuery = array($sQuery, array($mimetypename)); $res = DBUtil::getResultArray($aQuery); - if (PEAR::isError($res)) { + if (PEAR::isError($res)) { return $res; } else if (count($res) != 0){ - return $res[0]['filetypes']; + return $res[0]['filetypes']; } - + return _kt('Unknown Type'); } + + /** + * Method for downloading the document as a pdf. + * + * @return true on success else false + */ + function do_pdfdownload() { + + $oDocument = $this->oDocument; + $oStorage =& KTStorageManagerUtil::getSingleton(); + $oConfig =& KTConfig::getSingleton(); + + //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 + $sTempFilename = tempnam('/tmp', 'ktpdf'); + + // 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) ); + + # 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(), '.') ); + $browser = $_SERVER['HTTP_USER_AGENT']; + if ( strpos( strtoupper( $browser), 'MSIE') !== false) { + $sUrlEncodedFileName = rawurlencode($sUrlEncodedFileName); + } + // Set the correct headers + header("Content-Type: application/pdf"); + header("Content-Length: ". filesize($sTempFilename)); + header("Content-Disposition: attachment; filename=\"" . $sUrlEncodedFileName . ".pdf\""); + header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); + 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 + $oFile = new KTFSFileLike($sTempFilename); + KTFileLikeUtil::send_contents($oFile); + # Remove the tempfile + unlink($sTempFilename); + + # Create the document transaction + $oDocumentTransaction = & new DocumentTransaction($oDocument, 'Document downloaded as PDF', 'ktcore.transactions.download', $aOptions); + $oDocumentTransaction->create(); + + return true; + + } else { + # 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 + $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); + } + + + } + + /** + * Method for duplicating the document as a pdf. + * + */ + function do_pdfduplicate() { + + $this->oPage->setBreadcrumbDetails(_kt('Generate PDF')); + $oTemplate =& $this->oValidator->validateTemplate('ktstandard/PDFPlugin/PDFPlugin'); + + $oForm = $this->form_main(); + + $oTemplate->setData(array( + '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. + * + */ + function do_pdfreplace() { + + $this->oPage->setBreadcrumbDetails(_kt('Generate PDF')); + $oTemplate =& $this->oValidator->validateTemplate('ktstandard/PDFPlugin/PDFPlugin'); + + $oForm = $this->form_main(); + + $oTemplate->setData(array( + '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(); + + } } -?> \ No newline at end of file +?> diff --git a/plugins/ktstandard/PDFGeneratorPlugin.php b/plugins/ktstandard/PDFGeneratorPlugin.php index c0a79b8..2c8f791 100644 --- a/plugins/ktstandard/PDFGeneratorPlugin.php +++ b/plugins/ktstandard/PDFGeneratorPlugin.php @@ -1,11 +1,29 @@ registerAction('documentaction', 'PDFGeneratorAction', 'ktstandard.pdf.action', $sFilename = null); + $this->registerAction('documentaction', 'PDFGeneratorAction', 'ktstandard.pdf.generate', $sFilename = null); } } $oPluginRegistry =& KTPluginRegistry::getSingleton(); diff --git a/templates/ktstandard/PDFPlugin/PDFPlugin.smarty b/templates/ktstandard/PDFPlugin/PDFPlugin.smarty index c19f2af..b387d29 100644 --- a/templates/ktstandard/PDFPlugin/PDFPlugin.smarty +++ b/templates/ktstandard/PDFPlugin/PDFPlugin.smarty @@ -1 +1,3 @@ -

PDF Generation

+

{i18n arg_docname=$context->oDocument->getName()}Convert to PDF: #docname#{/i18n}

+ +{$form->render()} -- libgit2 0.21.4