/** * @see Html2Pdf::save() * @param string $file * @return mixed|void */ public function save($file) { if (!class_exists('\\DOMPDF')) { } $pdfs = array(); foreach ($this->_html as $i => $page) { $f = '/tmp/' . StringUtils::randString(10); $pdf = new \DOMPDF(); $pdf->load_html($page); $pdf->render(); file_put_contents("{$f}.pdf", $pdf->output()); $pdfs[] = "{$f}.pdf"; } $pdfMerge = Merge::factory($this->_page['merge'], $pdfs); $pdfMerge->save($file); }
/** * Minify function. Returns a minified content, or writes it to a file if path is specified. * * @param null|string $file * @return string|true */ public function compile($path = null) { // create a file from the given path, or a temproary resource of a max of 20 Mb $this->res = $path ? new FileObject($path, 'w+') : new TempFileObject($this->options['tmp'] . '/' . StringUtils::randString(50)); // merge all resources in the minfier's resource foreach ($this->resources as $handle) { while (!$handle->eof()) { $chunk = $handle->read(8192); $this->res->write($chunk); } unset($handle); $this->res->write("\n\n"); } $this->res->rewind(); // return the minified text as a string or as a file $string = null !== $path ? true : $this->res->read(); return $string; }
/** * Convert all the possible elements from which you can import content * to a resource. * * @param string|resource $res * @return TempFileObject */ protected function convertToResource($res) { $file = new TempFileObject($this->options['tmp'] . '/' . StringUtils::randString(50)); switch (true) { case $res instanceof FileObject: $file->write($res->read()); unset($res); break; case is_resource($res): $file->write(stream_get_contents($res)); break; case file_exists($res): case filter_var($res, FILTER_VALIDATE_URL): $file->write(stream_get_contents(fopen($res, 'r'))); break; default: $file->write($res); } $file->rewind(); return $file; }
/** * @return string */ public function compile($path = null) { parent::compile(); $file = $this->getMinifiedResource(); $file->rewind(); $rand = false; if (null === $path) { $path = $this->options['tmp'] . '/' . StringUtils::randString(50); $rand = true; } $cmd = sprintf($this->options['cli_run'], $file->getPath() . '/' . $file->getFilename(), $path); $out = exec($cmd, $output, $return); unset($file); if ($return) { throw new Exception\ShellExec(sprintf('External minify tool failed executing. To understand the problem, ' . 'please try running in command line: \'%s\' ', $cmd)); } $this->res = $rand ? new FileObject($path, 'r') : new TempFileObject($path, 'r'); // return the minified text as a string or as a file $string = $rand ? trim($this->res->read()) : true; return $string; }