使用 PHP 将 docx 转换为 pdf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9299030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:31:02  来源:igfitidea点击:

Convert docx to pdf using PHP

php

提问by Keith

I am currently generating multiple .docx files using PHPWord. I need to find a way to combine these docx files and save them as 1 pdf file. Is there a way that this can be done?

我目前正在使用 PHPWord 生成多个 .docx 文件。我需要找到一种方法来组合这些 docx 文件并将它们保存为 1 个 pdf 文件。有没有办法做到这一点?

回答by Juanma

Open the generated docx with PHPDOCX http://www.phpdocx.com/

用 PHPDOCX 打开生成的 docx http://www.phpdocx.com/

    require_once 'phpdocx_pro/classes/TransformDoc.inc';
    $docx = new TransformDoc();
    $docx->setStrFile('document.docx');
    $docx->generateXHTML();
    $html = $docx->getStrXHTML();

Also, you can export the docx to PDF with

此外,您可以使用以下命令将 docx 导出为 PDF

    $docx->generatePDF();

Note this is not a free library

请注意,这不是免费图书馆

回答by Arend

You can look at http://www.phplivedocx.org/, they support docx and through the zend framework also the generation of pdfs.

您可以查看http://www.phplivedocx.org/,它们支持 docx 并且通过 zend 框架还可以生成 pdf。

回答by NIF

Humm I use that : https://github.com/benskay/PHP-Digital-Format-Convert-Epub-Mobi-PDF/tree/master/library/phpDocxand that :

嗯,我使用它:https: //github.com/benskay/PHP-Digital-Format-Convert-Epub-Mobi-PDF/tree/master/library/phpDocx和:

require_once dirname(__FILE__) .'/phpdocx/classes/TransformDoc.inc.php';
require_once dirname(__FILE__) .'/phpdocx/classes/CreateDocx.inc.php';
$docx = new TransformDoc();
$docx->setStrFile('document.docx');
$docx->generatePDF();

==> Its seems work but... where is the generated PDF ? How I can get PDF file ?

==> 它似乎有效,但是...生成的 PDF 在哪里?我如何获得 PDF 文件?

回答by user4372270

  /**
   * return the pdf stream as a string returned from the function
   */
  function  output($debug = false) {
  ...
  }

so just write the result of 'generatePDF()' to a file.

所以只需将 'generatePDF()' 的结果写入文件。

for example:

例如:

$content = $docx->generatePDF();
$myfile = fopen("newfile.pdf", "w");
fwrite($myfile, $content);
fclose($myfile);