0

I am looking for a way to convert Word or text files to PDF using PHP.

Or is there a way to create image files from Word documents.

I tried with this code, it is well works in localhost but server side no.

<?php
require_once 'vendor/autoload.php';
require_once 'vendor/phpoffice/phpword/src/PhpWord/PHPWord.php';

$objReader= \PhpOffice\PhpWord\IOFactory::createReader('Word2007');
$contents=$objReader->load("2003.docx");

$rendername= \PhpOffice\PhpWord\Settings::PDF_RENDERER_TCPDF;

$renderLibrary="TCPDF";
$renderLibraryPath=''.$renderLibrary;
if(!\PhpOffice\PhpWord\Settings::setPdfRenderer($rendername,$renderLibrary){
    die("Provide Render Library And Path");
}
$renderLibraryPath=''.$renderLibrary;
$objWriter= \PhpOffice\PhpWord\IOFactory::createWriter($contents,'PDF');
$objWriter->save("2003.pdf");
?>
Sarra
  • 11
  • 5
  • Duplicate - https://stackoverflow.com/questions/5538584/convert-word-doc-docx-and-excel-xls-xlsx-to-pdf-with-php – Mihsak Yamato Oct 23 '19 at 11:45
  • 6
    Possible duplicate of [Convert Word doc, docx and Excel xls, xlsx to PDF with PHP](https://stackoverflow.com/questions/5538584/convert-word-doc-docx-and-excel-xls-xlsx-to-pdf-with-php) – Kalana Oct 23 '19 at 11:53
  • Thank you for your reply, yes I saw it but I do not know how to install "Openoffice.org" in the server – Sarra Oct 23 '19 at 11:55
  • Hi Sarra, first, welcome to Stackoverflow. Your question is too broad/primarily opinion-based. Please show some effort in searching, trying something instead of just asking for a full solution. Searching for "php Convert docx to pdf" on google shows tons of answers for your problem. Did you tried any of it? – Elias Soares Oct 23 '19 at 12:48
  • I tried a lot of solutions for example phpword, it is well worked in localhost but not on the server – Sarra Oct 23 '19 at 13:10

1 Answers1

0
<?php
require("easyPDFPrinter.php");

if(count($argv) != 3)
{
   echo "Please pass input file name ([qualityforge.net][1]) and output file name.\n";
   return;
}
$inputFileName = realpath($argv[1]);
if(!file_exists(dirname($argv[2])))
{
   echo "Invalid output file name.\n";
    return;
}
$outputFileName = rtrim(realpath(dirname($argv[2])), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . basename($argv[2]);

$printer = new BCL\easyPDF\Printer\Printer();
try
{
   $printjob = $printer->getWordPrintJobEx();
   $printjob->PrintOut($inputFileName, $outputFileName);
}
catch(BCL\easyPDF\Printer\PrinterException $ex)
{
   echo $ex->getMessage(), "\n";
}
finally
{
   $printer = null;
}
?>