How can I convert a document (docx) to PDF format using PHP Laravel?
Before converting this, I'm using PHPWord for set variables, and after that, I want to save it or convert it to PDF.
How can I convert a document (docx) to PDF format using PHP Laravel?
Before converting this, I'm using PHPWord for set variables, and after that, I want to save it or convert it to PDF.
You can use the PHPWord library to read the contents of a .docx file, and then use another library such as Dompdf or mPDF to convert the contents to a .pdf file. Here is an example:
use PhpOffice\PhpWord\IOFactory;
use Dompdf\Dompdf;
$phpWord = IOFactory::load('path/to/file.docx');
$dompdf = new Dompdf();
$dompdf->loadHtml($phpWord->saveHTML());
$dompdf->render();
$dompdf->stream();
You can also use the package like "phpoffice/phpword" and "barryvdh/laravel-dompdf"
composer require phpoffice/phpword
composer require barryvdh/laravel-dompdf
and then use them in the controller to convert docx to pdf
use PhpOffice\PhpWord\PhpWord;
use Barryvdh\DomPDF\Facade as PDF;
$phpWord = new PhpWord();
$phpWord->loadTemplate('path/to/file.docx');
$pdf = PDF::loadView('view', compact('phpWord'))->save('path/to/file.pdf');
You can also use the package like "phpoffice/phpword" and "barryvdh/laravel-dompdf"
composer require barryvdh/laravel-dompdf
composer require phpoffice/phpword
Register Service Provider In config/app.php
To register the service provider, open the config/app.php file. And add the following line in 'providers' array at the end:
'providers' => [
.....
Barryvdh\DomPDF\ServiceProvider::class,
]
Also, add the following line to the 'aliases' array at the end. You can use any aliases as you want like we have used ‘PDF’ but you can also use aliases like ‘DoPDF’, ‘myPDF’, ‘PF’, etc
'aliases' => [
.....
'PDF' => Barryvdh\DomPDF\Facade::class,
]
and then use them in the controller to convert docx to pdf
use PDF;
public function convertWordToPDF()
{
/* Set the PDF Engine Renderer Path */
$domPdfPath = base_path('vendor/dompdf/dompdf');
\PhpOffice\PhpWord\Settings::setPdfRendererPath($domPdfPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');
//Load word file
$Content = \PhpOffice\PhpWord\IOFactory::load(public_path('result.docx'));
//Save it into PDF
$PDFWriter = \PhpOffice\PhpWord\IOFactory::createWriter($Content,'PDF');
$PDFWriter->save(public_path('new-result.pdf'));
}
Detailed Summary:
Initial Setup: Begin by installing the required packages—
PHPWord
for DOCX manipulation andDompdf
for PDF conversion—using Composer.DOCX Creation or Manipulation: Utilize the
PHPWord
library to either create a new DOCX file or manipulate an existing one. Save this intermediate DOCX file.DOCX to HTML Conversion: Use
PHPWord
to convert the DOCX file into an HTML file, which will serve as an intermediate step before the PDF conversion.HTML to PDF Conversion: Utilize the
Dompdf
library to convert the intermediate HTML file to a PDF file. Save this PDF file.Laravel Integration: Integrate the entire process within a Laravel application by wrapping the code into a controller method and linking it to a route.
Step 1: Install Required Packages
First, make sure you have
PHPWord
installed. If not, you can install it via Composer:For PDF conversion, you can use
Dompdf
. Install it as well:Step 2: Create a DOCX File using PHPWord
Assuming you are already using PHPWord for
.docx
manipulation, you can create or load an existing.docx
file like this:Step 3: Convert DOCX to PDF
Convert the
.docx
file to.pdf
usingDompdf
:Step 4: Integrate into Laravel
Wrap the above code inside a Laravel controller method:
Then route a URL to this controller method.
Critique and Suggestions for Improvement
.docx
to.html
might not be perfect for complex documents. For better accuracy, consider usingwkhtmltopdf
..html
and.pdf
files. Secure the temporary location and delete files after use.Note: This solution is not fully tested and is provided as a guideline. Please test thoroughly before implementing in a production environment.