Reputation: 6063
One of my projects requires the conversion of DOCX to PDF. I came across the phpdocx project and everything converts fine, but it serves the file to the browser, which prompts the user to download after conversion. I need to keep the file, just read the data for MySQL storage. Any ideas?
Here is the code I'm using:
$docx = new TransformDoc();
$docx ->setStrFile($tmpName);
$docx ->generatePDF();
Using Tim's modifications below produces the following error:
i
Warning: session_start() [function.session-start]:
Cannot send session cache limiter - headers already sent
(output started at /home/zbtech/public_html/DocCon/classes/TransformDoc.inc:1)
in /home/zbtech/public_html/scribpub.php on line 5
Unable to generate PDF file string. exception 'DOMPDF_Exception' with message
'Unknown image type: files/files_/tmp/phpNQFatu/media/word/.'
in /home/zbtech/public_html/DocCon/pdf/include/image_cache.cls.php:175 Stack trace:
#0 /home/zbtech/public_html/DocCon/pdf/include/image_frame_decorator.cls.php(88):
Image_Cache::resolve_url('files/files_/tm...', NULL, '', '')
#1 /home/zbtech/public_html/DocCon/pdf/include/frame_factory.cls.php(173): Image_Frame_Decorator-
>__construct(Object(Frame), Object(DOMPDF))
#2 /home/zbtech/public_html/DocCon/pdf/include/dompdf.cls.php(499): Frame_Factory::decorate_frame
(Object(Frame), Object(DOMPDF)) #3 /home/zbtech/public_html/DocCon/classes/TransformDoc.inc
(282): DOMPDF->render() #4 /home/zbtech/public_html/scribpub.php(68): TransformDoc->generatePDF
() #5 {main}
Upvotes: 2
Views: 7416
Reputation: 558
A clear way to achive this is to editing the provided file:
/phpdocx/examples/easy/createPDF.php
public function generatePDF($outputFileName)
{
$this->generateXHTML();
$this->cleanXHTML();
try {
$domPDF = new DOMPDF();
$domPDF->load_html($this->_xhtml);
$domPDF->render();
//
// ADD THIS: (dont forget the outputFileName method argument)
//
$handler = fopen($outputFileName,'w');
fwrite($handler, $domPDF->output());
fclose($handler);
}
catch (Exception $err) {
echo 'Unable to generate PDF file string. ';
echo $err;
}
return $out;
}
Upvotes: 0
Reputation: 1822
If you search the files for PHP Docx for "Unknown image type:" you'll find it in pdf/include/image_cache.cls.php.
// line 81
static function resolve_url($url, $proto, $host, $base_path) {
...
// line 168
$resolved_url = build_url($proto, $host, $base_path, $url);
if ($DEBUGPNG) print 'build_url('.$proto.','.$host.','.$base_path.','.$url.')('.$resolved_url.')';
if ( !preg_match("/.*\.(\w+)/",$url,$match) ) {
//debugpng
if ($DEBUGPNG) print '[resolve_url exception '.$url.']';
throw new DOMPDF_Exception("Unknown image type: $url.");
}
....
the code is throwing this error because it can't find an extension on the url to guess the image type from. I have no idea why this is happening with the new code that utilizes the ->output method and not with the original code - you'd think that generating a pdf would work no matter which way we use.
Two choices now: comment out (or remove) the throw new DOMPDF_Exception line referenced above, or using output buffering with the original function.
Upvotes: 2
Reputation: 1822
Here's what I'd do.
The phpdocx library does this to stream the pdf to the browser.
This is located in classes/TransformDoc.inc on or about line 275 (as of the version I downloaded on 9/17/2012)
public function generatePDF()
{
$this->generateXHTML();
$this->cleanXHTML();
try {
$domPDF = new DOMPDF();
$domPDF->load_html($this->_xhtml);
$domPDF->render();
$fileName = $this->getFileName() . '.pdf';
$domPDF->stream($fileName);
}
catch (Exception $err) {
echo 'Unable to generate PDF file. ';
echo $err;
}
}
Looking at the source reveals that you could write your own function to do something similar. Here is an untested, example function based on the above function.
/**
* Convert DOCX to PDF, using dompdf. DOCX->XHTML->PDF and returns in a string
*
* @access public
*/
public function generatePDF()
{
$this->generateXHTML();
$this->cleanXHTML();
try {
$domPDF = new DOMPDF();
$domPDF->load_html($this->_xhtml);
$domPDF->render();
$out = $domPDF->output();
}
catch (Exception $err) {
echo 'Unable to generate PDF file string. ';
echo $err;
}
return $out;
}
Upvotes: 4