【问题标题】:DOCX to PDF Conversion using PHP Docx - storing instead of streaming to browser使用 PHP Docx 将 DOCX 转换为 PDF - 存储而不是流式传输到浏览器
【发布时间】:2012-09-09 19:46:56
【问题描述】:

我的一个项目需要将 DOCX 转换为 PDF。我遇到了phpdocx 项目,一切都转换得很好,但是它将文件提供给浏览器,在转换后提示用户下载。我需要保留文件,只需读取 MySQL 存储的数据。有任何想法吗?

这是我正在使用的代码:

$docx = new TransformDoc();
$docx ->setStrFile($tmpName);
$docx ->generatePDF();

使用以下 Tim 的修改会产生以下错误:

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}

【问题讨论】:

  • 查看TransformDoc类,找到一种分配二进制数据而不是输出的方法。
  • 还没有玩过 phpdocx.. 无论如何 $output=$docx->generatePDF() 会工作吗?
  • 我知道副本是针对文本文件的,但问题相同,您需要在 php 页面中添加正确的标题
  • 托比,我不确定这是标题问题。 phpdocx 类转换文档并将其返回给浏览器。 @Moe,要是这么简单就好了!这是我尝试的第一件事,但它仍然返回到类中的浏览器。迈克,我有。就是找不到。

标签: php pdf docx file-conversion


【解决方案1】:

这就是我要做的。

phpdocx 库执行此操作以将 pdf 流式传输到浏览器。

这位于第 275 行或附近的 classes/TransformDoc.inc 中(截至我 2012 年 9 月 17 日下载的版本)

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;
    }
}

查看源代码表明您可以编写自己的函数来执行类似的操作。 这是基于上述函数的未经测试的示例函数。

/**
 * 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;
}

【讨论】:

  • 哦,呃!我已经一遍又一遍地查看那个来源几个小时了!谢谢,@蒂姆!为你 +1!
  • 我刚刚做了 2 次快速编辑 - 确保在阅读此评论后,您获得了最新的代码。
  • 我不得不将代码追踪到 pdf/include/dompdf.cls.php 以找到 output 方法。它不需要文件名,但需要选项 - phpdocx 不发送选项,但如果您愿意,显然可以发送选项。
  • 另外,编写 phpdocx 代码的人可能不太了解异常处理 - 在这里只回显异常似乎有点毫无意义 - 最好让它从库中冒泡到要处理的申请。 :/哦,好吧。
  • 使用这些更改只会对我产生错误。我回去抓住了你更新的来源。我应该在这里发布错误,还是根据您的修改和错误开始一个新问题?
【解决方案2】:

如果您在 PHP Docx 文件中搜索“未知图像类型:”,您会在 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.");
    }
    ....

代码抛出此错误,因为它无法在 url 上找到扩展来猜测图像类型。我不知道为什么使用 ->output 方法的新代码而不是原始代码会发生这种情况 - 你会认为无论我们使用哪种方式生成 pdf 都可以工作。

现在有两个选择:注释掉(或删除)上面引用的 throw new DOMPDF_Exception 行,或者使用原始函数的输出缓冲。

【讨论】:

  • docx 文件也可能引用了未上传的外部图像。我对 docx 格式的理解有点不够深入。
  • DOCX 很像一个 zip。您甚至可以在 zip 应用程序中打开 DOCX 文档。我已经确认所有内容都在上传。
  • 您可以查看 DOMPDF 项目 code.google.com/p/dompdf 并查看是否可以将 PHP Docx 中的代码升级到最新版本 - 这可能是一个已修复的已知错误。
  • 整理好了!你是一个才华横溢的人。介意我在网站主页上添加信用,链接到您的网站吗,蒂姆?
【解决方案3】:

实现此目的的一个明确方法是编辑提供的文件:

/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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2019-02-22
    • 2015-05-19
    • 2013-04-18
    相关资源
    最近更新 更多