added transform package from github

This commit is contained in:
Dr Masroor Ehsan 2025-01-12 11:53:42 +06:00
parent a01fe8028a
commit 5b412be4f8
13 changed files with 11449 additions and 0 deletions

View File

@ -0,0 +1,395 @@
<?php
namespace Phpdocx\Transform;
/**
* Transform DOCX to PDF or XHTML
*
* @category Phpdocx
* @package transform
* @copyright Copyright (c) Narcea Producciones Multimedia S.L.
* (http://www.2mdc.com)
* @license phpdocx LICENSE
* @link http://www.phpdocx.com
*/
error_reporting(E_ALL & ~E_NOTICE);
class TransformDoc
{
const SCHEMA_IMAGEDOCUMENT = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image';
const SCHEMA_OFFICEDOCUMENT = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument';
/**
*
* @access private
* @var string
*/
private $_document;
/**
*
* @access private
* @var array
*/
private $_docProps;
/**
*
* @access private
* @var string
*/
private $_xhtml;
/**
* Construct
*
* @access public
*/
public function __construct()
{
}
/**
* Destruct
*
* @access public
*/
public function __destruct()
{
}
/**
* Getter. Document
*
* @access public
*/
public function getDocument()
{
return $this->_document;
}
/**
* Getter. Document
*
* @access public
*/
public function setDocument($document)
{
$this->_document = $document;
}
/**
* Getter. DocProps
*
* @access public
*/
public function getDocProps()
{
return $this->_docProps;
}
/**
* Getter. DocProps
*
* @access public
*/
public function setDocProps($props)
{
$this->_docProps = $props;
}
/**
* Getter. File
*
* @access public
*/
public function getstrFile()
{
return $this->strFile;
}
/**
* Getter. XHTML
*
* @access public
*/
public function getStrXHTML()
{
return $this->_xhtml;
}
/**
* Getter. Output file
*
* @access public
*/
public function getStrOutputFile()
{
return $this->strOutputFile;
}
/**
* Setter. File
*
* @access public
*/
public function setstrFile($file)
{
$this->strFile = $file;
}
/**
* Setter. XHTML
*
* @access public
*/
public function setStrXHTML($strXHTML)
{
$this->_xhtml = $strXHTML;
}
/**
*
* @param string $outputFile
*/
public function setStrOutputFile($outputFile)
{
$this->strOutputFile = $outputFile;
}
/**
* Return zip path
*
* @access public
* @return string
*/
public function absoluteZipPath($path)
{
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
$parts = array_filter(
explode(DIRECTORY_SEPARATOR, $path), 'strlen'
);
$arrAbsolutes = array();
foreach ($parts as $datParts) {
if ('.' == $datParts)
continue;
if ('..' == $datParts) {
array_pop($arrAbsolutes);
} else {
$arrAbsolutes[] = $datParts;
}
}
return implode('/', $arrAbsolutes);
}
/**
* Clean HTML
*
* @access public
*/
public function cleanXHTML()
{
$sectionOne = explode('<head>', $this->_xhtml);
$sectionTwo = explode('</head>', $this->_xhtml);
$sectionTwo = str_replace(
'src="?image="', 'src="imagen_not_found.jpg"', $sectionTwo
);
if (!isset($sectionTwo[1])) {
$sectionTwo[1] = $this->_xhtml;
$sectionOne[0] = '';
}
$this->_xhtml = $sectionOne[0] .
'<head><meta http-equiv="Content-Type" ' .
'content="text/html; charset=utf-8" /></head>' . $sectionTwo[1];
}
/**
* Return file name
*
* @access public
*/
public function getFileName()
{
try {
$partsFile = explode('/', $this->strFile);
$divideFile = explode('.', array_pop($partsFile));
$fileName = array_shift($divideFile);
} catch (\Exception $e) {
$fileName = 'file';
}
return $fileName;
}
/**
* Convert DOCX to XHTML
*
* @access public
*/
public function generateXHTML()
{
$package = new \ZipArchive();
$openPackage = $package->open($this->strFile);
if (!$openPackage) {
echo 'Unable to find the DOCX file';
exit();
}
$relsData = $package->getFromName('_rels/.rels');
$optionEntityLoader = libxml_disable_entity_loader(true);
$relations = simplexml_load_string($relsData);
libxml_disable_entity_loader($optionEntityLoader);
foreach ($relations->Relationship as $rel) {
if ($rel["Type"] == TransformDoc::SCHEMA_OFFICEDOCUMENT) {
$xml = $package->getFromName(
$this->absoluteZipPath(
dirname($rel['Target']) . '/' .
basename($rel['Target'])
)
);
$this->setDocument($xml);
$xmlDOM = new \DOMDocument();
$xml = str_replace('</w:wordDocument>', '', $xml);
$xml = preg_replace(
'/(<w:wordDocument)+(.)*(><w:body>)/', '<w:body>', $xml
);
$optionEntityLoader = libxml_disable_entity_loader(true);
@$xmlDOM->loadXML($xml);
libxml_disable_entity_loader($optionEntityLoader);
$xsl = new \DOMDocument();
$xsl->load(dirname(__FILE__) . '/../../../xsl/docx2html.xsl');
$xsltProc = new \XSLTProcessor();
$xsltProc->importStylesheet($xsl);
$this->_xhtml = $xsltProc->transformToXML($xmlDOM);
}
}
$pattern = "'src\s*=\s*([\"\'])?(?(1) (.*?)\\1 | ([^\s\>]+))'isx";
preg_match_all($pattern, $this->_xhtml, $domImgs);
$idImgs = array();
foreach ($domImgs[0] as $dats) {
$datsFiltered = explode('"', $dats);
if (preg_match('/^\?image=rId/', $datsFiltered[1])) {
$datFiltered = explode('?image=', $dats);
$idImgs[] = substr($datFiltered[1], 0, -1);
}
}
$relsDocumentData = $package->getFromName('word/_rels/document.xml.rels');
$optionEntityLoader = libxml_disable_entity_loader(true);
$relationsImgs = simplexml_load_string($relsDocumentData);
libxml_disable_entity_loader($optionEntityLoader);
$pathImgs = array();
foreach ($relationsImgs->Relationship as $relImg) {
if ($relImg["Type"] == TransformDoc::SCHEMA_IMAGEDOCUMENT) {
$pathImgs[(string) $relImg["Id"]] = (string) $relImg["Target"];
$pathZip[] = 'word/' . (string) $relImg["Target"];
}
}
foreach ($idImgs as $datsIdImgs) {
$this->_xhtml = str_replace(
"src=\"?image=$datsIdImgs\"", "src=\"files/files_" .
"$this->strFile/media/word/$pathImgs[$datsIdImgs]\"", $this->_xhtml
);
}
if (!empty($pathZip)) {
$package->extractTo(
"files/files_$this->strFile/media", $pathZip
);
$package->close();
}
}
/**
* Convert DOCX to PDF, using dompdf. DOCX->XHTML->PDF
*
* @access public
*/
public function generatePDF($path = '')
{
$this->generateXHTML();
$this->cleanXHTML();
$this->_extractProps();
try {
$domPDF = new \DOMPDF();
$domPDF->load_html($this->_xhtml);
$docProps = $this->getDocProps();
$domPDF->set_paper($docProps['size'], $docProps['orientation']);
$domPDF->render();
$fileName = $this->getFileName() . '.pdf';
if ($path == '') {
$domPDF->stream($fileName);
} else {
file_put_contents($path, $domPDF->output());
}
} catch (\Exception $err) {
echo 'Unable to generate PDF file. ';
echo $err;
}
}
/**
* Validate HTML using tidy
*
* @access public
*/
public function validatorXHTML()
{
ob_start();
echo $this->_xhtml;
$html = ob_get_clean();
$config = array(
'indent' => true,
'output-xhtml' => true,
'wrap' => 200);
$tidy = new tidy();
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();
$this->_xhtml = $tidy;
}
/**
* Get the document properties
*
* @return array
*/
private function _extractProps()
{
$xmlDOM = new \DOMDocument();
$optionEntityLoader = libxml_disable_entity_loader(true);
$xmlDOM->loadXML($this->getDocument());
libxml_disable_entity_loader($optionEntityLoader);
//Get the page size and orientation
$node = $xmlDOM->getElementsByTagName('pgSz');
$docProps = array();
$width = number_format(
$node->item(0)->getAttribute('w:w') / 20, 2, '.', '');
$height = number_format(
$node->item(0)->getAttribute('w:h') / 20, 2, '.', '');
$orient = $node->item(0)->getAttribute('w:orient');
if (empty($orient) || $orient == 'portrait') {
$docProps['orientation'] = 'portrait';
$docProps['size'] = array(0, 0, $width, $height);
} else {
$docProps['orientation'] = 'landscape';
$docProps['size'] = array(0, 0, $height, $width);
}
//Get the page margins
$node = $xmlDOM->getElementsByTagName('pgMar');
$margin = 'margin: ' .
floor($node->item(0)->getAttribute('w:top') / 15) . 'px ' .
floor($node->item(0)->getAttribute('w:right') / 15) . 'px ' .
floor($node->item(0)->getAttribute('w:bottom') / 15) . 'px ' .
floor($node->item(0)->getAttribute('w:left') / 15) . 'px;';
$xml = str_replace('$MARGIN$', $margin, $this->getStrXHTML());
$this->setStrXHTML($xml);
$this->setDocProps($docProps);
}
}

View File

@ -0,0 +1,77 @@
<?php
namespace Phpdocx\Transform;
use Phpdocx\Clean\CleanTemp;
use Phpdocx\Create\CreateDocx;
use Phpdocx\Logger\PhpdocxLogger;
use Phpdocx\Parse\RepairPDF;
use Phpdocx\Utilities\PhpdocxUtilities;
/**
* Transform DOCX to PDF, ODT, SXW, RTF, DOC, TXT, HTML or WIKI
*
* @category Phpdocx
* @package trasform
* @copyright Copyright (c) Narcea Producciones Multimedia S.L.
* (http://www.2mdc.com)
* @license phpdocx LICENSE
* @link https://www.phpdocx.com
*/
abstract class TransformDocAdv
{
/**
*
* @access protected
* @var array
*/
protected $phpdocxconfig;
/**
* Construct
*
* @access public
*/
public function __construct()
{
$this->phpdocxconfig = PhpdocxUtilities::parseConfig();
}
/**
* Transform document formats
*
* @access public
*
* @abstract
* @param $source
* @param $target
* @param array $options
*/
abstract public function transformDocument($source, $target, $options = array());
/**
* Check if the extension if supproted
*
* @param string $fileExtension
* @param array $supportedExtensions
* @return array files extensions
*/
protected function checkSupportedExtension($source, $target, $supportedExtensionsSource, $supportedExtensionsTarget) {
// get the source file info
$sourceFileInfo = pathinfo($source);
$sourceExtension = strtolower($sourceFileInfo['extension']);
if (!in_array($sourceExtension, $supportedExtensionsSource)) {
PhpdocxLogger::logger('The chosen extension \'' . $sourceExtension . '\' is not supported as source format.', 'fatal');
}
// get the target file info
$targetFileInfo = explode('.', $target);
$targetExtension = strtolower(array_pop($targetFileInfo));
if (!in_array($targetExtension, $supportedExtensionsTarget)) {
PhpdocxLogger::logger('The chosen extension \'' . $targetExtension . '\' is not supported as target format.', 'fatal');
}
return array('sourceExtension' => $sourceExtension, 'targetExtension' => $targetExtension);
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,168 @@
<?php
namespace Phpdocx\Transform;
/**
* Transform DOCX to PDF using native PHP classes and DOMPDF. DOMPDF plugin
*
* @category Phpdocx
* @package trasform
* @copyright Copyright (c) Narcea Producciones Multimedia S.L.
* (http://www.2mdc.com)
* @license phpdocx LICENSE
* @link https://www.phpdocx.com
*/
class TransformDocAdvHTMLDOMPDFPlugin extends TransformDocAdvHTMLPlugin
{
/**
* Conversion factor, used by the transformSizes method
* @var float
*/
protected $conversionFactor = 1.3;
/**
* Generate section tags
* @var boolean
*/
protected $generateSectionTags = true;
/**
* Add image src as base64
* @var boolean
*/
protected $imagesAsBase64 = true;
/**
* Target folder for images and other external contents. Not used for images is $imagesAsBase64 is true
* @var string
*/
protected $outputFilesPath = 'output_files/';
/**
* OOXML => HTML tags
* @var array
*/
protected $tags = array(
'bidi' => 'bidi',
'br' => 'br',
'comboBox' => 'select',
'comboBoxItem' => 'option',
'footer' => 'footer',
'header' => 'header',
'heading' => 'h',
'hyperlink' => 'a',
'image' => 'img',
'itemList' => 'li',
'orderedList' => 'ol',
'paragraph' => 'p',
'section' => 'section',
'span' => 'span',
'subscript' => 'sub',
'superscript' => 'sup',
'table' => 'table',
'tc' => 'td',
'tr' => 'tr',
'unorderedList' => 'ul',
);
protected $unit = 'px';
/**
* Constructor. Init HTML, CSS, meta and javascript base contents
*/
public function __construct() {
$this->baseCSS = '<style>p { margin-top: 0px;margin-bottom: 0px;}.page_break { page-break-before: always; }</style>';
$this->baseHTML = '<!DOCTYPE html><html>';
$this->baseJavaScript = '';
$this->baseMeta = '<meta charset="UTF-8">';
}
/**
* Generate class name to be added to tags
* @return string Class name
*/
public function generateClassName()
{
return str_replace('.', '_', uniqid('docx_', true));
}
/**
* Transform colors
* @return string New color
*/
public function transformColors($color) {
$colorTarget = $color;
if ($color == 'auto') {
$colorTarget = '000000';
}
return $colorTarget;
}
/**
* Transform content sizes
* @param string $value OOXML size
* @param string $source OOXML type size
* @param string $target Target size
* @return string HTML/CSS size
*/
public function transformSizes($value, $source, $target = null)
{
$returnValue = 0;
if ($target === null) {
$target = $this->unit;
}
if ($source == 'twips') {
if ($target == 'px') {
if ($value) {
$returnValue = ($value / 20) * $this->conversionFactor;
}
}
}
if ($source == 'eights') {
if ($target == 'px') {
if ($value) {
$returnValue = ($value / 8) * $this->conversionFactor;
}
}
// minimum value
if ($returnValue < 2) {
$returnValue = 2;
}
}
if ($source == 'half-points') {
if ($target == 'px') {
if ($value) {
$returnValue = ($value / 2) * $this->conversionFactor;
}
}
}
if ($source == 'pts') {
if ($target == 'px') {
if ($value) {
$returnValue = $value * $this->conversionFactor;
}
}
}
if ($source == 'fifths-percent') {
if ($target == '%') {
if ($value) {
$returnValue = ($value / 50);
}
}
}
// normalize decimal values to use dots
$returnValue = str_replace(',', '.', $returnValue);
return (string)$returnValue . $target;
}
}

View File

@ -0,0 +1,167 @@
<?php
namespace Phpdocx\Transform;
/**
* Transform DOCX to HTML using native PHP classes. Default plugin
*
* @category Phpdocx
* @package trasform
* @copyright Copyright (c) Narcea Producciones Multimedia S.L.
* (http://www.2mdc.com)
* @license phpdocx LICENSE
* @link https://www.phpdocx.com
*/
class TransformDocAdvHTMLDefaultPlugin extends TransformDocAdvHTMLPlugin
{
/**
* Conversion factor, used by the transformSizes method
* @var float
*/
protected $conversionFactor = 1.3;
/**
* Generate section tags
* @var boolean
*/
protected $generateSectionTags = true;
/**
* Add image src as base64
* @var boolean
*/
protected $imagesAsBase64 = true;
/**
* Target folder for images and other external contents. Not used for images is $imagesAsBase64 is true
* @var string
*/
protected $outputFilesPath = 'output_files/';
/**
* OOXML => HTML tags
* @var array
*/
protected $tags = array(
'bidi' => 'bidi',
'br' => 'br',
'comboBox' => 'select',
'comboBoxItem' => 'option',
'footer' => 'footer',
'header' => 'header',
'heading' => 'h',
'hyperlink' => 'a',
'image' => 'img',
'itemList' => 'li',
'orderedList' => 'ol',
'paragraph' => 'p',
'section' => 'section',
'span' => 'span',
'subscript' => 'sub',
'superscript' => 'sup',
'table' => 'table',
'tc' => 'td',
'tr' => 'tr',
'unorderedList' => 'ul',
);
protected $unit = 'px';
/**
* Constructor. Init HTML, CSS, meta and javascript base contents
*/
public function __construct() {
$this->baseCSS = '<style>p { margin-top: 0px;margin-bottom: 0px;} span.tabcontent{margin-left: 50px;}</style>';
$this->baseHTML = '<!DOCTYPE html><html>';
$this->baseJavaScript = '';
$this->baseMeta = '<meta charset="UTF-8">';
}
/**
* Generate class name to be added to tags
* @return string Class name
*/
public function generateClassName()
{
return str_replace('.', '_', uniqid('docx_', true));
}
/**
* Transform colors
* @return string New color
*/
public function transformColors($color) {
$colorTarget = $color;
if ($color == 'auto' || empty($color)) {
$colorTarget = '000000';
}
return $colorTarget;
}
/**
* Transform content sizes
* @param string $value OOXML size
* @param string $source OOXML type size
* @param string $target Target size
* @return string HTML/CSS size
*/
public function transformSizes($value, $source, $target = null)
{
$returnValue = 0;
if ($target === null) {
$target = $this->unit;
}
if ($source == 'twips') {
if ($target == 'px') {
if ($value) {
$returnValue = ($value / 20) * $this->conversionFactor;
}
}
}
if ($source == 'eights') {
if ($target == 'px') {
if ($value) {
$returnValue = ($value / 8) * $this->conversionFactor;
}
}
// minimum value
if ($returnValue < 2) {
$returnValue = 2;
}
}
if ($source == 'half-points') {
if ($target == 'px') {
if ($value) {
$returnValue = ($value / 2) * $this->conversionFactor;
}
}
}
if ($source == 'pts') {
if ($target == 'px') {
if ($value) {
$returnValue = $value * $this->conversionFactor;
}
}
}
if ($source == 'fifths-percent') {
if ($target == '%') {
if ($value) {
$returnValue = ($value / 50);
}
}
}
// normalize decimal values to use dots
$returnValue = str_replace(',', '.', $returnValue);
return (string)$returnValue . $target;
}
}

View File

@ -0,0 +1,299 @@
<?php
namespace Phpdocx\Transform;
/**
* Transform DOCX to HTML using native PHP classes. Abstract class
*
* @category Phpdocx
* @package trasform
* @copyright Copyright (c) Narcea Producciones Multimedia S.L.
* (http://www.2mdc.com)
* @license phpdocx LICENSE
* @link https://www.phpdocx.com
*/
abstract class TransformDocAdvHTMLPlugin
{
/**
* Base CSS
* @var string
*/
protected $baseCSS;
/**
* Base HTML
* @var string
*/
protected $baseHTML;
/**
* Base JavaScript
* @var string
*/
protected $baseJavaScript;
/**
* Base Meta
* @var string
*/
protected $baseMeta;
/**
* Conversion factor, used by the transformSizes method
* @var float
*/
protected $conversionFactor;
/**
* Generate section tags
* @var bool
*/
protected $generateSectionTags = true;
/**
* Images as base64
* @var bool
*/
protected $imagesAsBase64;
/**
* Target folder for images and other external contents. Not used for images is $imagesAsBase64 is true
* @var string
*/
protected $outputFilesPath;
/**
* Conversion unit
* @var int
*/
protected $unit;
/**
* Generate class name to be added to tags
*/
abstract public function generateClassName();
/**
* Transform colors
* @param string $color
* @return string New color
*/
abstract public function transformColors($color);
/**
* Transform content sizes
* @param string $value OOXML size
* @param string $source OOXML type size
* @param string $target Target size
* @return string HTML/CSS size
*/
abstract public function transformSizes($value, $source, $target = null);
/**
* Getter $baseCSS
* @return string
*/
public function getBaseCSS()
{
return $this->baseCSS;
}
/**
* Setter $baseCSS
* @param string $baseCSS
*/
public function setBaseCSS($baseCSS)
{
$this->baseCSS = $baseCSS;
}
/**
* Getter $baseHTML
* @return string
*/
public function getBaseHTML()
{
return $this->baseHTML;
}
/**
* Setter $baseHTML
* @param string $baseHTML
*/
public function setBaseHTML($baseHTML)
{
$this->baseHTML = $baseHTML;
}
/**
* Getter $baseJavaScript
* @return string
*/
public function getBaseJavaScript()
{
return $this->baseJavaScript;
}
/**
* Setter $baseJavaScript
* @param string $baseJavaScript
*/
public function setBaseJavaScript($baseJavaScript)
{
$this->baseJavaScript = $baseJavaScript;
}
/**
* Getter $baseMeta
* @return string
*/
public function getBaseMeta()
{
return $this->baseMeta;
}
/**
* Setter $baseMeta
* @param string $baseMeta
*/
public function setBaseMeta($baseMeta)
{
$this->baseMeta = $baseMeta;
}
/**
* Getter $conversionFactor
* @return float
*/
public function getConversionFactor()
{
return $this->conversionFactor;
}
/**
* Setter $conversionFactor
* @param float $conversionFactor
*/
public function setConversionFactor($conversionFactor)
{
$this->conversionFactor = $conversionFactor;
}
/**
* Getter extra class value
* @return string
*/
public function getExtraClass($tag)
{
if (isset($this->extraClasses[$tag])) {
return $this->extraClasses[$tag];
}
}
/**
* Getter $extraClasses
* @return string
*/
public function getExtraClasses()
{
return $this->extraClasses;
}
/**
* Setter $extraClasses
* @param string $tag
* @param string $class
*/
public function setExtraClasses($tag, $class)
{
$this->extraClasses[$tag] = $class;
}
/**
* Getter $generateSectionTags
* @return bool
*/
public function getGenerateSectionTags()
{
return $this->generateSectionTags;
}
/**
* Setter $generateSectionTags
* @param bool $generateSectionTags
*/
public function setGenerateSectionTags($generateSectionTags)
{
$this->generateSectionTags = $generateSectionTags;
}
/**
* Getter $imagesAsBase64
* @return bool
*/
public function getImagesAsBase64()
{
return $this->imagesAsBase64;
}
/**
* Setter $imagesAsBase64
* @param bool $imagesAsBase64
*/
public function setImagesAsBase64($imagesAsBase64)
{
$this->imagesAsBase64 = $imagesAsBase64;
}
/**
* Getter $outputFilesPath
* @return string
*/
public function getOutputFilesPath()
{
return $this->outputFilesPath;
}
/**
* Setter $outputFilesPath
* @param string $outputFilesPath
*/
public function setOutputFilesPath($outputFilesPath)
{
$this->outputFilesPath = $outputFilesPath;
}
/**
* Getter $tag value
* @param string $tag
* @return string
*/
public function getTag($tag)
{
return $this->tags[$tag];
}
/**
* Setter $setTag
* @param string $tag
* @param string $value
*/
public function setTag($tag, $value)
{
$this->tags[$tag] = $value;
}
/**
* Getter $tags
* @return array
*/
public function getTags()
{
return $this->tags;
}
/**
* Getter $unit
* @return int
*/
public function getUnit()
{
return $this->unit;
}
/**
* Setter $setUnit
* @param int $unit
*/
public function setUnit($unit)
{
$this->unit = $unit;
}
}

View File

@ -0,0 +1,168 @@
<?php
namespace Phpdocx\Transform;
/**
* Transform DOCX to PDF using native PHP classes and TCPDF. TCPDF plugin
*
* @category Phpdocx
* @package trasform
* @copyright Copyright (c) Narcea Producciones Multimedia S.L.
* (http://www.2mdc.com)
* @license phpdocx LICENSE
* @link https://www.phpdocx.com
*/
class TransformDocAdvHTMLTCPDFPlugin extends TransformDocAdvHTMLPlugin
{
/**
* Conversion factor, used by the transformSizes method
* @var float
*/
protected $conversionFactor = 1;
/**
* Generate section tags
* @var boolean
*/
protected $generateSectionTags = true;
/**
* Add image src as base64
* @var boolean
*/
protected $imagesAsBase64 = true;
/**
* Target folder for images and other external contents. Not used for images is $imagesAsBase64 is true
* @var string
*/
protected $outputFilesPath = 'output_files/';
/**
* OOXML => HTML tags
* @var array
*/
protected $tags = array(
'bidi' => 'bidi',
'br' => 'br',
'comboBox' => 'select',
'comboBoxItem' => 'option',
'footer' => 'footer',
'header' => 'header',
'heading' => 'h',
'hyperlink' => 'a',
'image' => 'img',
'itemList' => 'li',
'orderedList' => 'ol',
'paragraph' => 'p',
'section' => 'section',
'span' => 'span',
'subscript' => 'sub',
'superscript' => 'sup',
'table' => 'table',
'tc' => 'td',
'tr' => 'tr',
'unorderedList' => 'ul',
);
protected $unit = 'px';
/**
* Constructor. Init HTML, CSS, meta and javascript base contents
*/
public function __construct() {
$this->baseCSS = '<style>p { margin-top: 0px;margin-bottom: 0px;}</style>';
$this->baseHTML = '<!DOCTYPE html><html>';
$this->baseJavaScript = '';
$this->baseMeta = '<meta charset="UTF-8">';
}
/**
* Generate class name to be added to tags
* @return string Class name
*/
public function generateClassName()
{
return str_replace('.', '_', uniqid('docx_', true));
}
/**
* Transform colors
* @return string New color
*/
public function transformColors($color) {
$colorTarget = $color;
if ($color == 'auto') {
$colorTarget = '000000';
}
return $colorTarget;
}
/**
* Transform content sizes
* @param string $value OOXML size
* @param string $source OOXML type size
* @param string $target Target size
* @return string HTML/CSS size
*/
public function transformSizes($value, $source, $target = null)
{
$returnValue = 0;
if ($target === null) {
$target = $this->unit;
}
if ($source == 'twips') {
if ($target == 'px') {
if ($value) {
$returnValue = ($value / 20) * $this->conversionFactor;
}
}
}
if ($source == 'eights') {
if ($target == 'px') {
if ($value) {
$returnValue = ($value / 8) * $this->conversionFactor;
}
}
// minimum value
if ($returnValue < 2) {
$returnValue = 2;
}
}
if ($source == 'half-points') {
if ($target == 'px') {
if ($value) {
$returnValue = ($value / 2) * $this->conversionFactor;
}
}
}
if ($source == 'pts') {
if ($target == 'px') {
if ($value) {
$returnValue = $value * $this->conversionFactor;
}
}
}
if ($source == 'fifths-percent') {
if ($target == '%') {
if ($value) {
$returnValue = ($value / 50);
}
}
}
// normalize decimal values to use dots
$returnValue = str_replace(',', '.', $returnValue);
return (string)$returnValue . $target;
}
}

View File

@ -0,0 +1,182 @@
<?php
namespace Phpdocx\Transform;
use Phpdocx\Clean\CleanTemp;
use Phpdocx\Create\CreateDocx;
use Phpdocx\Logger\PhpdocxLogger;
use Phpdocx\Parse\RepairPDF;
use Phpdocx\Transform\TransformDocAdv;
use Phpdocx\Utilities\PhpdocxUtilities;
/**
* Transform documents using LibreOffice
*
* @category Phpdocx
* @package trasform
* @copyright Copyright (c) Narcea Producciones Multimedia S.L.
* (http://www.2mdc.com)
* @license phpdocx LICENSE
* @link https://www.phpdocx.com
*/
require_once dirname(__FILE__) . '/TransformDocAdv.php';
class TransformDocAdvLibreOffice extends TransformDocAdv
{
/**
* Calculate and return the document statistics
*
* @param string $source DOCX document
* @return array
*/
public function getStatistics($source)
{
if (!file_exists($source)) {
PhpdocxLogger::logger('The file not exist', 'fatal');
}
$phpdocxconfig = PhpdocxUtilities::parseConfig();
$libreOfficePath = $phpdocxconfig['transform']['path'];
// storage the output as ASCII text file
$tempFile = realpath($source) . uniqid('_txt');
// run the statistics macro
passthru($libreOfficePath . ' --invisible "macro:///Standard.Module1.GetStatistics(' . realpath($source) . ',' . $tempFile . ')" ');
// parse the statistics and return them
$statistics = array();
$statisticsFile = fopen($tempFile, 'r');
if (!$statisticsFile) {
throw new \Exception('Unable to open the stats file.');
}
while (($statistic = fgets($statisticsFile)) !== false) {
$dataStatistic = explode(': ', $statistic);
$statistics[$dataStatistic[0]] = $dataStatistic[1];
}
fclose($statisticsFile);
return $statistics;
}
/**
* Transform documents:
* DOCX to PDF, HTML, DOC, ODT, PNG, RTF, TXT
* DOC to DOCX, PDF, HTML, ODT, PNG, RTF, TXT
* ODT to DOCX, PDF, HTML, DOC, PNG, RTF, TXT
* RTF to DOCX, PDF, HTML, DOC, ODT, PNG, TXT
*
* @access public
* @param $source
* @param $target
* @param array $options :
* 'comments' (bool) : false (default) or true. Exports the comments
* 'debug' (bool) : false (default) or true. Shows debug information about the plugin conversion
* 'formsfields' (bool) : false (default) or true. Exports the form fields
* 'homeFolder' (string) : set a custom home folder to be used for the conversions
* 'lossless' (bool) : false (default) or true. Lossless compression
* 'method' (string) : 'direct' (default), 'script' ; 'direct' method uses passthru and 'script' uses a external script. If you're using Apache and 'direct' doesn't work use 'script'
* 'outdir' (string) : set the outdir path. Useful when the PDF output path is not the same than the running script
* 'pdfa1' (bool) : false (default) or true. Generates the TOC before exporting the document
* 'toc' (bool) : false (default) or true. Generates the TOC before transforming the document
* @return void
*/
public function transformDocument($source, $target, $options = array())
{
$allowedExtensionsSource = array('doc', 'docx', 'html', 'odt', 'rtf', 'txt', 'xhtml');
$allowedExtensionsTarget = array('doc', 'docx', 'html', 'odt', 'pdf', 'png', 'rtf', 'txt', 'xhtml');
$filesExtensions = $this->checkSupportedExtension($source, $target, $allowedExtensionsSource, $allowedExtensionsTarget);
if (!isset($options['method'])) {
$options['method'] = 'direct';
}
if (!isset($options['debug'])) {
$options['debug'] = false;
}
if (!isset($options['toc'])) {
$options['toc'] = false;
}
// get the file info
$sourceFileInfo = pathinfo($source);
$sourceExtension = $sourceFileInfo['extension'];
$phpdocxconfig = PhpdocxUtilities::parseConfig();
$libreOfficePath = $phpdocxconfig['transform']['path'];
$customHomeFolder = false;
if (isset($options['homeFolder'])) {
$currentHomeFolder = getenv("HOME");
putenv("HOME=" . $options['homeFolder']);
$customHomeFolder = true;
} else if (isset($phpdocxconfig['transform']['home_folder'])) {
$currentHomeFolder = getenv("HOME");
putenv("HOME=" . $phpdocxconfig['transform']['home_folder']);
$customHomeFolder = true;
}
// set outputstring for debugging
$outputDebug = '';
if (PHP_OS == 'Linux' || PHP_OS == 'Darwin' || PHP_OS == ' FreeBSD') {
if (!$options['debug']) {
$outputDebug = ' > /dev/null 2>&1';
}
} elseif (substr(PHP_OS, 0, 3) == 'Win' || substr(PHP_OS, 0, 3) == 'WIN') {
if (!$options['debug']) {
$outputDebug = ' > nul 2>&1';
}
}
// if the outdir option is set use it as target path, instead use the dir path
if (isset($options['outdir'])) {
$outdir = $options['outdir'];
} else {
$outdir = $sourceFileInfo['dirname'];
}
if ($options['method'] == 'script') {
passthru('php ' . dirname(__FILE__) . '/../lib/convertSimple.php -s ' . $source . ' -e ' . $filesExtensions['targetExtension'] . ' -p ' . $libreOfficePath . ' -t ' . $options['toc'] . ' -o ' . $outdir . $outputDebug);
} else {
if ((isset($options['toc']) && $options['toc'] === true) && (!isset($options['pdfa1']) || (isset($options['pdfa1']) && $options['pdfa1'] === false))) {
if ($filesExtensions['targetExtension'] == 'docx') {
// TOC DOCX
passthru($libreOfficePath . ' --invisible "macro:///Standard.Module1.SaveToDocxToc(' . realpath($source) . ')" ' . $outputDebug);
} else {
// TOC PDF
passthru($libreOfficePath . ' --invisible "macro:///Standard.Module1.SaveToPdfToc(' . realpath($source) . ')" ' . $outputDebug);
}
} elseif ((isset($options['toc']) && $options['toc'] === true) && (isset($options['pdfa1']) && $options['pdfa1'] === true)) {
// TOC and PDFA-1
passthru($libreOfficePath . ' --invisible "macro:///Standard.Module1.SaveToPdfA1Toc(' . realpath($source) . ')" ' . $outputDebug);
} elseif ((isset($options['pdfa1']) && $options['pdfa1'] === true) && (!isset($options['toc']) || (!isset($options['toc']) || $options['toc'] === false))) {
// PDFA-1
passthru($libreOfficePath . ' --invisible "macro:///Standard.Module1.SaveToPdfA1(' . realpath($source) . ')" ' . $outputDebug);
} elseif ((isset($options['comments']) && $options['comments'] === true)) {
// comments
passthru($libreOfficePath . ' --invisible "macro:///Standard.Module1.ExportNotesToPdf(' . realpath($source) . ')" ' . $outputDebug);
} elseif ((isset($options['lossless']) && $options['lossless'] === true)) {
// lossless
passthru($libreOfficePath . ' --invisible "macro:///Standard.Module1.LosslessPdf(' . realpath($source) . ')" ' . $outputDebug);
} elseif ((isset($options['formsfields']) && $options['formsfields'] === true)) {
// forms fields
passthru($libreOfficePath . ' --invisible "macro:///Standard.Module1.ExportFormFieldsToPdf(' . realpath($source) . ')" ' . $outputDebug);
} else {
// default
passthru($libreOfficePath . ' --invisible --convert-to ' . $filesExtensions['targetExtension'] . ' ' . $source . ' --outdir ' . $outdir . $outputDebug);
}
}
// get the converted document, this is the name of the source and the extension
$newDocumentPath = $outdir . '/' . $sourceFileInfo['filename'] . '.' . $filesExtensions['targetExtension'];
// move the document to the guessed destination
rename($newDocumentPath, $target);
// restore the previous HOME value if a custom one has been set
if ($customHomeFolder) {
putenv("HOME=" . $currentHomeFolder);
}
}
}

View File

@ -0,0 +1,150 @@
<?php
namespace Phpdocx\Transform;
use Phpdocx\Logger\PhpdocxLogger;
/**
* Transform documents using native PHP classes
*
* @category Phpdocx
* @package trasform
* @copyright Copyright (c) Narcea Producciones Multimedia S.L.
* (http://www.2mdc.com)
* @license phpdocx LICENSE
* @link https://www.phpdocx.com
*/
require_once dirname(__FILE__) . '/TransformDocAdv.php';
class TransformDocAdvMSWord extends TransformDocAdv
{
/**
* Calculate and return the document statistics
*
* @param string $source DOCX document
* @param array $options :
* 'selectedContent' (string) : documents or active (default)
* @return array
*/
public function getStatistics($source, $options = array())
{
if (!file_exists($source)) {
PhpdocxLogger::logger('The file not exist', 'fatal');
}
// statistics
$statistics = array();
// start a Word instance
$MSWordInstance = new \COM("word.application") or exit('Check that PHP COM is enabled and a working copy of Word is installed.');
// check that the version of MS Word is 12 or higher
if ($MSWordInstance->Version >= 12) {
// hide MS Word
$MSWordInstance->Visible = 0;
// open the source document
$MSWordInstance->Documents->Open($source);
if (isset($options['selectedContent']) && $options['selectedContent'] == 'documents') {
$statistics['Characters'] = $MSWordInstance->Documents[1]->ComputeStatistics(3);
$statistics['CharactersWithSpaces'] = $MSWordInstance->Documents[1]->ComputeStatistics(5);
$statistics['Lines'] = $MSWordInstance->Documents[1]->ComputeStatistics(1);
$statistics['Pages'] = $MSWordInstance->Documents[1]->ComputeStatistics(2);
$statistics['Paragraphs'] = $MSWordInstance->Documents[1]->ComputeStatistics(4);
$statistics['Words'] = $MSWordInstance->Documents[1]->ComputeStatistics(0);
// close Word
$MSWordInstance->Documents[1]->Close();
} else {
$statistics['Characters'] = $MSWordInstance->ActiveDocument->ComputeStatistics(3);
$statistics['CharactersWithSpaces'] = $MSWordInstance->ActiveDocument->ComputeStatistics(5);
$statistics['Lines'] = $MSWordInstance->ActiveDocument->ComputeStatistics(1);
$statistics['Pages'] = $MSWordInstance->ActiveDocument->ComputeStatistics(2);
$statistics['Paragraphs'] = $MSWordInstance->ActiveDocument->ComputeStatistics(4);
$statistics['Words'] = $MSWordInstance->ActiveDocument->ComputeStatistics(0);
// close Word
$MSWordInstance->ActiveDocument->Close();
}
} else {
exit('The version of Word should be 12 (Word 2007) or higher');
}
$MSWordInstance->Quit();
$MSWordInstance = null;
return $statistics;
}
/**
* Transform documents:
* DOCX to PDF, DOC
* PDF to DOCX, DOC
* DOC to DOCX, PDF
*
* @access public
* @param $source
* @param $target
* @param array $options :
* 'selectedContent' (string) : documents or active (default)
* 'toc' (bool) : false (default) or true. It generates the TOC before transforming the document
* @return void
*/
public function transformDocument($source, $target, $options = array())
{
$allowedExtensionsSource = array('doc', 'docx', 'pdf');
$allowedExtensionsTarget = array('doc', 'docx', 'pdf');
$filesExtensions = $this->checkSupportedExtension($source, $target, $allowedExtensionsSource, $allowedExtensionsTarget);
$code = array(
'doc' => new \VARIANT(0, VT_I4),
'docx' => new \VARIANT(16, VT_I4),
'pdf' => new \VARIANT(17, VT_I4),
);
// start a Word instance
$MSWordInstance = new \COM("word.application") or exit('Please check that PHP COM is enabled and a working copy of Word is installed.');
// check that the version of MS Word is 12 or higher
if ($MSWordInstance->Version >= 12) {
// hide MS Word
$MSWordInstance->Visible = 0;
// open the source document
$MSWordInstance->Documents->Open($source);
if (isset($options['selectedContent']) && $options['selectedContent'] == 'documents') {
// generate the TOC content
if (isset($options['toc']) && $options['toc']) {
$MSWordInstance->Documents[1]->TablesOfContents(1);
}
// save the target document
$MSWordInstance->Documents[1]->SaveAs($target, $code[$filesExtensions['targetExtension']]);
// close Word
$MSWordInstance->Documents[1]->Close();
} else {
// generate the TOC content
if (isset($options['toc']) && $options['toc']) {
$MSWordInstance->ActiveDocument->TablesOfContents(1);
}
// save the target document
$MSWordInstance->ActiveDocument->SaveAs($target, $code[$filesExtensions['targetExtension']]);
// close Word
$MSWordInstance->ActiveDocument->Close();
}
} else {
exit('The version of Word should be 12 (Word 2007) or higher');
}
$MSWordInstance->Quit();
$MSWordInstance = null;
}
}

View File

@ -0,0 +1,79 @@
<?php
namespace Phpdocx\Transform;
use Phpdocx\Create\CreateDocx as CreateDocx;
/**
* Transform documents using native PHP classes
*
* @category Phpdocx
* @package trasform
* @copyright Copyright (c) Narcea Producciones Multimedia S.L.
* (http://www.2mdc.com)
* @license phpdocx LICENSE
* @link https://www.phpdocx.com
*/
require_once dirname(__FILE__) . '/TransformDocAdv.php';
class TransformDocAdvNative extends TransformDocAdv
{
/**
* Transform documents:
* DOCX to PDF, HTML
*
* @access public
* @param $source
* @param $target
* @param array $options :
* 'htmlPlugin' (TransformDocAdvHTMLPlugin): plugin to use to do the transformation to HTML. TransformDocAdvHTMLDefaultPlugin as default
* 'stream' (bool): enable the stream mode. False as default
* @return void or stream
*/
public function transformDocument($source, $target, $options = array())
{
$allowedExtensionsSource = array('docx', 'html');
$allowedExtensionsTarget = array('html', 'docx', 'pdf');
$filesExtensions = $this->checkSupportedExtension($source, $target, $allowedExtensionsSource, $allowedExtensionsTarget);
if ($filesExtensions['sourceExtension'] == 'docx') {
if ($filesExtensions['targetExtension'] == 'html') {
if (!isset($options['htmlPlugin'])) {
$options['htmlPlugin'] = new TransformDocAdvHTMLDefaultPlugin();
}
$transform = new TransformDocAdvHTML($source);
$html = $transform->transform($options['htmlPlugin']);
if ((isset($options['stream']) && $options['stream']) || CreateDocx::$streamMode == true) {
// stream mode enabled
echo $html;
} else {
// stream mode disabled, save the document
file_put_contents($target, $html);
}
} else if ($filesExtensions['targetExtension'] == 'pdf') {
$transform = new TransformDocAdvPDF($source);
$transform->transform($target);
}
} else if ($filesExtensions['sourceExtension'] == 'html') {
if ($filesExtensions['targetExtension'] == 'docx') {
$docx = new CreateDocx();
$docx->embedHTML(file_get_contents($source));
$docx->createDocx($target);
} else if ($filesExtensions['targetExtension'] == 'pdf') {
// first transform HTML to DOCX and then DOCX to PDF
$docx = new CreateDocx();
$docx->embedHTML(file_get_contents($source));
$docx->createDocx($target . '.docx');
$transform = new TransformDocAdvPDF($source);
$transform->transform($target);
}
}
}
}

View File

@ -0,0 +1,344 @@
<?php
namespace Phpdocx\Transform;
use Phpdocx\Clean\CleanTemp;
use Phpdocx\Create\CreateDocx;
use Phpdocx\Logger\PhpdocxLogger;
use Phpdocx\Parse\RepairPDF;
use Phpdocx\Transform\TransformDocAdv;
use Phpdocx\Utilities\PhpdocxUtilities;
/**
* Transform DOCX to PDF, ODT, SXW, RTF, DOC, TXT, HTML or WIKI using OpenOffice
*
* @category Phpdocx
* @package trasform
* @copyright Copyright (c) Narcea Producciones Multimedia S.L.
* (http://www.2mdc.com)
* @license phpdocx LICENSE
* @link https://www.phpdocx.com
*/
require_once dirname(__FILE__) . '/TransformDocAdv.php';
class TransformDocAdvOpenOffice extends TransformDocAdv
{
/**
* Prepare docx before pdf transformation
*
* @access public
* @param $source
* @param $tempDir
* @param $options
* @return string
*/
public function prepareDocx($source, $tempDir = null, $options)
{
if ($tempDir === null) {
$tempDir = $this->getTempDirPath();
$tempName = $tempDir . '/tempDocX_' . uniqid() . '.docx';
} else {
$tempName = $tempDir . '/tempDocX_' . uniqid() . '.docx';
}
copy($source, $tempName);
return $tempName;
}
/**
* Replace charts as images
*
* @access public
* @param $source
*/
public function replaceChartsWithImages($source){
$sourceDocx = new \ZipArchive();
$sourceDocx->open($source);
// if jpgraph exists use it, instead use ezComponents
if (file_exists(dirname(__FILE__) . '/../lib/jpgraph/')) {
$image = new CreateChartImageJpgraph();
} else {
$image = new CreateChartImageEzComponents();
}
// get the images
$image->getChartsDocx($source);
$image->parseCharts();
$listChartImages = $image->getListImages();
if (!is_array($listChartImages)) {
$listChartImages = array();
}
// parse de docx and add the images
$contentTypesXML = $sourceDocx->getFromName('[Content_Types].xml');
// get the document.xml.rels file from the DOCX
$documentRelsXML = $sourceDocx->getFromName('word/_rels/document.xml.rels');
$documentRelsDOM = new \SimpleXMLElement($documentRelsXML);
$documentRelsDOM->registerXPathNamespace('ns', 'http://schemas.openxmlformats.org/package/2006/relationships');
// get the document.xml file from the DOCX
$documentXML = $sourceDocx->getFromName('word/document.xml');
$documentDOM = new \SimpleXMLElement($documentXML);
// get the chart elements of the DOM
$contentTypesDOM = new \SimpleXMLElement($contentTypesXML);
$contentTypesDOM->registerXPathNamespace('ns', 'http://schemas.openxmlformats.org/package/2006/content-types');
$elementsCharts = $contentTypesDOM->xpath('ns:Override[@ContentType="application/vnd.openxmlformats-officedocument.drawingml.chart+xml"]');
// as some nodes are removed, iterate the charts in reverse order
//$elementsCharts = array_reverse($elementsCharts);
// index of the image to be added to the ZIP
$indexImage = 0;
foreach ($elementsCharts as $value) {
// get the attributes of the element
$attributes = $value->attributes();
// get the width and height and add them to the charts array
// get the rId of the chart from the documentRels
$relationshipChart = $documentRelsDOM->xpath('ns:Relationship[@Target="'.substr($attributes['PartName'], 6).'"]');
$documentDOM->registerXPathNamespace('a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
$documentDOM->registerXPathNamespace('c', 'http://schemas.openxmlformats.org/drawingml/2006/chart');
// get the a:graphicData element of the chart
$elementAGraphicData = $documentDOM->xpath('//a:graphicData[c:chart[@r:id="'. $relationshipChart[0]->attributes()->Id . '"]]');
$elementAGraphicData[0]['uri'] = 'http://schemas.openxmlformats.org/drawingml/2006/picture';
//get and remove the c:chart child
$elementAGraphicData[0]->registerXPathNamespace('c', 'http://schemas.openxmlformats.org/drawingml/2006/chart');
$elementCChart = $elementAGraphicData[0]->xpath('//c:chart');
//unset($elementCChart[0][0]);
// remove the chart content keeping w:drawing tag
$domElementAGraphicData = dom_import_simplexml($elementAGraphicData[0]);
$picture = $this->getTemplateImage(uniqid(), $relationshipChart[0]->attributes()->Id);
$pictureFragment = $domElementAGraphicData->ownerDocument->createDocumentFragment();
$pictureFragment->appendXML($picture);
$domElementAGraphicData->appendChild($pictureFragment);
$sourceDocx->addFile($listChartImages[$indexImage], 'word/media/' . $listChartImages[$indexImage]);
//Modify the Type attribute of document.xml.rels to http://schemas.openxmlformats.org/officeDocument/2006/relationships/image
//and the Target to media/'.$listChartImages[$indexImage]
$relsImage = $documentRelsDOM->xpath('//ns:Relationship[@Id="'. $relationshipChart[0]->attributes()->Id.'"]');
$relsImage[0]['Type'] = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image';
$relsImage[0]['Target'] = 'media/' . $listChartImages[$indexImage];
$indexImage++;
}
// save the modified document.xml.rels file
$docXML = $documentDOM->asXML();
$docXML = str_replace('<pic:pic xmlns:r="http://schemas.openxmlformats.org/package/2006/relationships" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">', '<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">', $docXML);
$docXML = str_replace('<pic:pic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">', '<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">', $docXML);
$docXML = str_replace('<pic:pic xmlns:r="http://schemas.openxmlformats.org/package/2006/relationships" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"', '<pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">', $docXML);
$sourceDocx->addFromString('word/document.xml', $docXML);
// save the modified document.xml.rels file
$relsDoc = $documentRelsDOM->asXML();
$sourceDocx->addFromString('word/_rels/document.xml.rels', $relsDoc);
// make sure that there is the associated content type "png"
$position = strpos('Extension="png"', $contentTypesXML);
if($position === false){
$contentTypesXML = str_replace('</Types>', '<Default Extension="png" ContentType="image/png"/></Types>', $contentTypesXML);
$sourceDocx->addFromString('[Content_Types].xml', $contentTypesXML);
}
// close the zip
$sourceDocx->close();
// remove the generated images
foreach ($listChartImages as $listChartImage) {
unlink($listChartImage);
}
}
/**
* To add support of sys_get_temp_dir for PHP versions under 5.2.1
*
* @access protected
* @return string
*/
protected function getTempDirPath()
{
if ($this->phpdocxconfig['settings']['temp_path']) {
return $this->phpdocxconfig['settings']['temp_path'];
}
if (!function_exists('sys_get_temp_dir')) {
function sys_get_temp_dir()
{
if ($temp = getenv('TMP')) {
return $temp;
}
if ($temp = getenv('TEMP')) {
return $temp;
}
if ($temp = getenv('TMPDIR')) {
return $temp;
}
$temp = tempnam(__FILE__, '');
if (file_exists($temp)) {
unlink($temp);
return dirname($temp);
}
return null;
}
} else {
return sys_get_temp_dir();
}
}
/**
*
*/
protected function getTemplateImage($name, $id)
{
$templateImage = '<pic:pic xmlns:r="http://schemas.openxmlformats.org/package/2006/relationships" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<pic:nvPicPr>
<pic:cNvPr id="0" name="' . $name .'"/>
<pic:cNvPicPr/>
</pic:nvPicPr>
<pic:blipFill>
<a:blip r:embed="' . $id . '"/>
<a:stretch>
<a:fillRect/>
</a:stretch>
</pic:blipFill>
<pic:spPr>
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="4876800" cy="3657600"/>
</a:xfrm>
<a:prstGeom prst="rect">
<a:avLst/>
</a:prstGeom>
</pic:spPr>
</pic:pic>';
return $templateImage;
}
/**
* Transform all documents supported by OpenOffice
*
* @access public
* @param $source
* @param $target
* @param array $options :
* 'debug' (bool) : false (default) or true. It shows debug information about the plugin conversion
* 'homeFolder' (string) : set a custom home folder to be used for the conversions
* 'method' (string) : 'direct' (default), 'script' ; 'direct' method uses passthru and 'script' uses a external script. If you're using Apache and 'direct' doesn't work use 'script'
* 'odfconverter' (bool) : true (default) or false. Use odf-converter to preproccess documents
* 'tempDir' (string) : uses a custom temp folder
* 'version' (string) : 32-bit or 64-bit architecture. 32, 64 or null (default). If null autodetect
* @return void
*/
public function transformDocument($source, $target, $options = array())
{
$allowedExtensionsSource = array('doc', 'docx', 'html', 'odt', 'rtf', 'txt', 'xhtml');
$allowedExtensionsTarget = array('doc', 'docx', 'html', 'odt', 'pdf', 'rtf', 'txt', 'xhtml');
$filesExtensions = $this->checkSupportedExtension($source, $target, $allowedExtensionsSource, $allowedExtensionsTarget);
// get the file info
$sourceFileInfo = pathinfo($source);
$sourceExtension = $sourceFileInfo['extension'];
if (!isset($options['method'])) {
$options['method'] = 'direct';
}
if (!isset($options['odfconverter'])) {
$options['odfconverter'] = true;
}
if (!isset($options['debug'])) {
$options['debug'] = false;
}
$tempDir = null;
if (isset($options['tempDir'])) {
$tempDir = $options['tempDir'];
}
$version = '64';
if (isset($options['version'])) {
$version = $options['version'];
}
if (isset($options['homeFolder'])) {
$currentHomeFolder = getenv("HOME");
putenv("HOME=" . $options['homeFolder']);
}
if ($sourceExtension == 'docx') {
// set path to OdfConverter: 32-bit or 64-bit
$odfconverterPath = '';
// set outputstring for debugging
$outputDebug = '';
if (PHP_OS == 'Linux') {
if (!$options['debug']) {
$outputDebug = ' > /dev/null 2>&1';
}
if ($version == '32') {
$odfconverterPath = '/../../../lib/OdfConverter/32/OdfConverter';
} elseif ($version == '64') {
$odfconverterPath = '/../../../lib/OdfConverter/64/OdfConverter';
} else {
// detect if 32bit or 64bit
if (PHP_INT_SIZE * 8 == 64) {
$odfconverterPath = '/../../../lib/OdfConverter/64/OdfConverter';
} else {
$odfconverterPath = '/../../../lib/OdfConverter/32/OdfConverter';
}
}
} elseif (substr(PHP_OS, 0, 3) == 'Win' || substr(PHP_OS, 0, 3) == 'WIN') {
if (!$options['debug']) {
$outputDebug = ' > nul 2>&1';
}
$odfconverterPath = '/../../../lib/OdfConverter/Windows/OdfConverter.exe';
}
$newDocx = $this->prepareDocx($source, $tempDir, $options);
if (file_exists(dirname(__FILE__) . '/CreateChartImage.php') && (file_exists(dirname(__FILE__) . '/../../../lib/jpgraph/') || file_exists(dirname(__FILE__) . '/../../../lib/ezcomponents'))) {
$this->replaceChartsWithImages($newDocx);
}
if ($tempDir === null) {
$tempDir = $this->getTempDirPath();
$tempDoc = $tempDir . '/tempOdt_' . uniqid() . '.odt';
} else {
$tempDoc = $tempDir . '/tempOdt_' . uniqid() . '.odt';
}
if ($options['method'] == 'script') {
passthru('php ' . dirname(__FILE__) . '/../../../lib/convert.php -s ' . $newDocx . ' -t ' . $tempDoc . ' -d ' . $docDestination . ' -o ' . $options['odfconverter'] . ' -p ' . $odfconverterPath . $outputDebug);
} else {
if ($extension != 'rtf' && $options['odfconverter']) {
passthru(dirname(__FILE__) . $odfconverterPath . ' /I ' . $newDocx . ' /O ' . $tempDoc . $outputDebug);
} else {
copy($source, $tempDoc);
}
// How to start OpenOffice in headless mode: lib/openoffice/openoffice.org3/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard;
passthru('java -jar ' . dirname(__FILE__) . '/../../../lib/openoffice/jodconverter-2.2.2/lib/jodconverter-cli-2.2.2.jar ' . $tempDoc . ' ' . $docDestination . $outputDebug);
}
CleanTemp::clean($tempDoc);
} else {
if ($options['method'] == 'script') {
passthru('php ' . dirname(__FILE__) . '/../../../lib/convert.php -s ' . $source . ' -d ' . $docDestination . $outputDebug);
} else {
// how to start OpenOffice in headless mode: lib/openoffice/openoffice.org3/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard;
passthru('java -jar ' . dirname(__FILE__) . '/../../../lib/openoffice/jodconverter-2.2.2/lib/jodconverter-cli-2.2.2.jar ' . $source . ' ' . $docDestination . $outputDebug);
}
}
// restore the previous HOME value
if (isset($options['homeFolder'])) {
putenv("HOME=" . $currentHomeFolder);
}
}
}

File diff suppressed because it is too large Load Diff