[英]Greek letters are replaced with ??? once I convert a docx into pdf using PHPWord?
[英]How I can convert an docx into pdf using phpWord template engine?
我制作了一个快速控制器,以测试在替换占位符值后如何将 docx 模板转换为 pdf。 .docx 是:
https://drive.google.com/file/d/14-Nh0L-R7ulHgpmTN5s-T0PilwWSsE7L/view?usp=sharing
我使用以下 Laravel 代码将它从 docx 模板转换为 pdf
namespace App\Controllers\Services;
use App\Controllers\BaseController;
use Illuminate\Support\Facades\Response;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
class Contract extends BaseController
{
public function getContract()
{
$file = storage_path()."/contracts/contract.docx";
$tmpFile = storage_path()."/contracts/output.pdf";
$template = new TemplateProcessor($file);
$template->setValue('COMP_NAME',"LOREM IPSUM INC");
$template->setValue('ADDRESS',"Nowhere Str Tsastikistan");
$template->saveAs($tmpFile);
return Response::make("OK");
}
}
但代码无法生成可读的 pdf: https : //drive.google.com/file/d/1wYsVrjwQNMN8r3sWZlOm6x7cNeja3ET0/view? usp =sharing
我还尝试生成一个 html,以便我可以将其转换为 pdf: https : //drive.google.com/file/d/1wYsVrjwQNMN8r3sWZlOm6x7cNeja3ET0/view? usp =sharing
但是输出仍然非常垃圾。
因此,您知道如何在 PDF 中转换 docx 模板吗?如果是,我如何使用内存文件而不是文件系统?
直接你不能。 您需要先将其另存为 docx,然后再次阅读并将其另存为 PDF:
namespace App\Controllers\Services;
use App\Controllers\BaseController;
use Illuminate\Support\Facades\Response;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\IOFactory;
class Contract extends BaseController
{
public function getContract()
{
$file = storage_path()."/contracts/contract.docx";
$tmpFile = storage_path()."/contracts/output.pdf";
$outfile = storage_path()."/contracts/output.docx";
$template = new TemplateProcessor($file);
$template->setValue('COMP_NAME',"LOREM IPSUM INC");
$template->setValue('ADDRESS',"Nowhere Str Tsastikistan");
$template->saveAs($outfile);
$phpWord = IOFactory::load($outfile);
$phpWord->save($tmpFile,'PDF');
return Response::make("OK");
}
}
我还没有测试过的一个解决方案是使用php://temp
或php://memory
以避免文件系统 IO。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.