Nov 12, 2024
0
Level 1
phpoffice/phpword generate broken format for pdf files
After I successfully upgraded my project from laravel 5 to laravel 9, there is issue with pdf generator. The pdf generated in broken format. Originally, the pdf generator should follow the format from the Word file after generate to pdf but now it produce a broken format pdf file.
So should I working on fixing the code? Or should I working on fixing the phpoffice/phpword dependency files?
Below are the codes for the pdf generator:
SchemeController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Libs\Template\Template;
use App\Models\AuditLog;
use App\Models\File;
use App\Models\ProjectUnitSchemeSetting;
use Carbon\Carbon;
use App\Models\Project;
use App\Models\Constant;
use App\Models\ProjectUnit;
use Illuminate\Http\Request;
use App\Models\ProjectUnitScheme;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use Sprn\Uploader\DocumentUploader;
class SchemeController extends Controller
{
//other codes
public function templatePreview(ProjectUnitScheme $project_unit_scheme)
{
abort_if(is_null($project_unit_scheme->template_surat_tawaran), 404);
$filename = 'sample_surat_tawaran.pdf';
$generate_surat = app(Template::class)->generateFile($project_unit_scheme->template_surat_tawaran->realPath(), null, $project_unit_scheme);
return response($generate_surat, 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => "inline; filename=$filename",
]);
}
//other codes
}
app\Libs\Template\Template.php
<?php
namespace App\Libs\Template;
use App\Models\Application;
use App\Models\Constant;
use App\Models\ProjectUnitScheme;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
use Sprn\Application\TotalIncomeConfig;
class Template
{
//other codes
public function generateFile($inputFile, $application = null, $project_unit_scheme = null) {
$outputBaseDir = 'generated';
if (!Storage::disk('template')->exists($outputBaseDir)) {
Storage::disk('template')->makeDirectory($outputBaseDir);
}
$replaceValues = $this->dataForTemplate($application, $project_unit_scheme);
$outputFile = app('converter')->replaceAndConvert(
$inputFile,
Storage::disk('template')->path($outputBaseDir),
$replaceValues
);
$outputContent = file_get_contents($outputFile);
Storage::disk('template')->delete($outputBaseDir . DIRECTORY_SEPARATOR . basename($outputFile));
return $outputContent;
}
//other codes
}
app\Libs\PDF\WordReplacer.php
<?php
namespace App\Libs\PDF;
use Illuminate\Support\Str;
use PhpOffice\PhpWord\Settings;
use Illuminate\Support\Facades\File;
use PhpOffice\PhpWord\TemplateProcessor;
class WordReplacer
{
//other codes
public function replaceAndConvert($inputFile, $outputDir, $replaceValues = [])
{
$tempFile = $this->replace($inputFile, $outputDir, $replaceValues);
$outputFile = $this->converter->convert($tempFile, $outputDir);
File::delete($tempFile);
return $outputFile;
}
//other codes
}
composer.lock
"name": "phpoffice/phpword",
"version": "0.18.3",
"source": {
"type": "git",
"url": "https://github.com/PHPOffice/PHPWord.git",
"reference": "be0190cd5d8f95b4be08d5853b107aa4e352759a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHPOffice/PHPWord/zipball/be0190cd5d8f95b4be08d5853b107aa4e352759a",
"reference": "be0190cd5d8f95b4be08d5853b107aa4e352759a",
"shasum": ""
},
composer.json
"require": {
"php": "^8.0",
"ext-json": "*",
"artisaninweb/laravel-soap": "0.3.0.9",
"barryvdh/laravel-debugbar": "^3.6",
"barryvdh/laravel-dompdf": "^1.0",
"davejamesmiller/laravel-breadcrumbs": "^5.3",
"doctrine/dbal": "^2.13",
"eduardokum/laravel-mail-auto-embed": "^2.10",
"guzzlehttp/guzzle": "^7.2",
"jaspersoft/rest-client": "^2.0",
"laravel/framework": "^9.52",
"laravel/tinker": "^2.7",
"laravel/ui": "^3.3",
"league/csv": "^9.6",
"league/flysystem-aws-s3-v3": "^3.27",
"maatwebsite/excel": "^3.1",
"phpoffice/phpword": "0.18.3",
"predis/predis": "^1.1",
"spatie/laravel-ignition": "^1.4",
"spatie/laravel-permission": "^5.5",
"webklex/laravel-pdfmerger": "^1.3"
},
Please or to participate in this conversation.