/** * Calls the wkhtmltopdf executable to generate a PDF. * * @param DOMDocument $structure * @param DocBlox_Transformer_Transformation $transformation * * @return void */ public function transform(DOMDocument $structure, DocBlox_Transformer_Transformation $transformation) { $artifact = $transformation->getTransformer()->getTarget() . DIRECTORY_SEPARATOR . $transformation->getArtifact(); $transformation->setArtifact($artifact); $source = substr($transformation->getSource(), 0, 1) != DIRECTORY_SEPARATOR ? $transformation->getTransformer()->getTarget() . DIRECTORY_SEPARATOR . $transformation->getSource() : $transformation->getSource(); $transformation->setSource($source); $options = ''; if ($transformation->getParameter('toc', 'false') == 'true') { $options = ' toc '; } // TODO: add parameter to provide a cover HTML // TODO: add a parameter to provide a header HTML // TODO: add a parameter to provide a footer HTML // first try if there is a wkhtmltopdf in the global path, this helps windows users exec('wkhtmltopdf ' . $options . ' ' . $transformation->getSource() . ' ' . $transformation->getArtifact() . ' 2>&1', $output, $error); $output = implode(PHP_EOL, $output); // this notice is linux specific; if it is found no global wkhtmltopdf was installed; try the one which is included // with docblox if (strpos($output, 'wkhtmltopdf: not found') !== false) { exec($this->getConfig()->paths->application . '/src/wkhtmltopdf/wkhtmltopdf-i386 ' . $options . ' ' . $transformation->getSource() . ' ' . $transformation->getArtifact() . ' 2>&1', $output, $error); $output = implode(PHP_EOL, $output) . PHP_EOL; } // log message and output $this->log('Generating PDF file ' . $transformation->getArtifact() . ' from ' . $transformation->getSource()); $this->log($output, $error == 0 ? DocBlox_Core_Log::INFO : DocBlox_Core_Log::CRIT); // CRASH! if ($error != 0) { throw new Exception('Conversion to PDF failed, see output for details'); } }
/** * This method combines the structure.xml and the given target template * and creates a static html page at the artifact location. * * @param DOMDocument $structure XML source. * @param DocBlox_Transformer_Transformation $transformation Transformation. * * @throws Exception * * @return void */ public function transform(DOMDocument $structure, DocBlox_Transformer_Transformation $transformation) { if (!class_exists('XSLTProcessor')) { throw new Exception('The XSL writer was unable to find your XSLTProcessor; ' . 'please check if you have installed the PHP XSL extension'); } $artifact = $transformation->getTransformer()->getTarget() . DIRECTORY_SEPARATOR . $transformation->getArtifact(); $xsl = new DOMDocument(); $xsl->load($transformation->getSourceAsPath()); $proc = new XSLTProcessor(); $proc->importStyleSheet($xsl); $proc->setParameter('', 'title', $structure->documentElement->getAttribute('title')); $proc->setParameter('', 'root', str_repeat('../', substr_count($transformation->getArtifact(), '/'))); $proc->setParameter('', 'search_template', $transformation->getParameter('search', 'none')); $proc->setParameter('', 'version', DocBlox_Core_Abstract::VERSION); // check parameters for variables and add them when found $this->setProcessorParameters($transformation, $proc); // if a query is given, then apply a transformation to the artifact // location by replacing ($<var>} with the sluggified node-value of the // search result if ($transformation->getQuery() !== '') { $xpath = new DOMXPath($transformation->getTransformer()->getSource()); $qry = $xpath->query($transformation->getQuery()); foreach ($qry as $element) { $proc->setParameter('', $element->nodeName, $element->nodeValue); $filename = str_replace('{$' . $element->nodeName . '}', $transformation->getTransformer()->generateFilename($element->nodeValue), $artifact); $this->log('Processing the file: ' . $element->nodeValue . ' as ' . $filename); $proc->transformToURI($structure, 'file://' . $filename); } } else { if (substr($transformation->getArtifact(), 0, 1) == '$') { // not a file, it must become a variable! if (!isset($this->getConfig()->transformations->{'xsl.variables'})) { $this->getConfig()->transformations->{'xsl-variables'} = new Zend_Config(array(), true); } $variable_name = substr($transformation->getArtifact(), 1); $this->getConfig()->transformations->{'xsl-variables'}->{$variable_name} = $proc->transformToXml($structure); } else { $proc->transformToURI($structure, 'file://' . $artifact); } } }