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");
?>
<?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;
}
?>
Related
I am trying to translate my ODT file into any language. But I can't do it. Can you help me?
I think the problem comes from this comment in my code that I have added down:
Here is the comment:
// Re-pack the translated files into a new ODT file
Here is the error:
[01-Feb-2023 07:29:51 UTC] PHP Warning: ZipArchive::close(): Read
error: Is a directory in
/home/bloggors/translatedocs.bloggors.com/controller/test.php on line
57
Here is my code:
<?php
require_once 'includes/init.php';
// Load the ODT file into a ZipArchive object
$zip = new ZipArchive;
$res = $zip->open('input.odt');
if ($res === TRUE) {
$zip->extractTo('input_extracted/');
if (!$zip->close()) {
echo "Error: could not close the archive.";
}
} else {
echo "Error opening the archive: " . $res;
}
// Load the content.xml file into a SimpleXML object
if (file_exists('input_extracted/content.xml')) {
$xml = simplexml_load_file('input_extracted/content.xml');
} else {
echo "Error: could not find content.xml in the extracted archive.";
}
// Translate the text in the SimpleXML object
foreach ($xml->xpath('//text:p') as $paragraph) {
$translatedText = translate($paragraph, 'la', 'fr');
$paragraph[0] = $translatedText;
}
// Save the translated SimpleXML object to a file
if ($xml->asXML('input_extracted/content_translated.xml')) {
// Replace the original content.xml file with the translated one
if (unlink('input_extracted/content.xml')) {
if (!rename('input_extracted/content_translated.xml', 'input_extracted/content.xml')) {
echo "Error: could not rename content_translated.xml to content.xml.";
}
} else {
echo "Error: could not delete content.xml.";
}
} else {
echo "Error: could not save content_translated.xml.";
}
// Re-pack the translated files into a new ODT file
$zip = new ZipArchive;
$res = $zip->open('output.odt', ZipArchive::CREATE);
if ($res === TRUE) {
$dir = opendir('input_extracted/');
while ($file = readdir($dir)) {
if ($file != '.' && $file != '..') {
$zip->addFile('input_extracted/' . $file, $file);
}
}
closedir($dir);
if (!$zip->close()) {
echo "Error: could not close the archive.";
}
} else {
echo "Error opening the archive: " . $res;
}
Any help would be greatly appreciated
I want to convert an Excel file to PDF.
Here is my code:
function excel() {
$excel = new COM("Excel.Application") or die ("ERROR: Unable to instantaniate COM!\r\n");
$file = base_url('archieve/ADAF - 2018000007.xlsx');
$Workbook = $excel->Workbooks->Open($file) or die("ERROR: Unable to open " . $file . "!\r\n");
$Worksheet = $Workbook->Worksheets(1);
echo 'file loaded';
$xlTypePDF = 0;
$xlQualityStandard = 0;
try {
$Worksheet->ExportAsFixedFormat($xlTypePDF, base_url('archieve/ADAF test.pdf'), $xlQualityStandard);
} catch(com_exception $e) {
echo $e->getMessage()."\n";
exit;
}
echo 'file convert';
$excel = NULL;
unset($excel);
}
When I run the function, the server is still loading and reaches timeout. The code is in a different server.
Is my code wrong? Since I have tested in local PC and it works.
I tried to run the COM using Python and it worked fine. Turns out there is a problem in PHP when export as pdf. I still have the issue. Any suggestions?
I am splitting PDF file and after split I want to read that split page text but that is returning me nothing. If I am reading before splitting is is working fine and returning me all PDF text but don't know why it is not working after splitting. Here is my code :-
<?php
function split_pdf($filename, $end_directory = false)
{
require_once('../fpdf/fpdf.php');
require_once('../fpdi/fpdi.php');
require_once('../pdf2text/class.pdf2text.php');
$end_directory = $end_directory ? $end_directory : './';
$new_path = preg_replace('/[\/]+/', '/', $end_directory.'/'.substr($filename, 0, strrpos($filename, '/')));
if (!is_dir($new_path))
{
// Will make directories under end directory that don't exist
// Provided that end directory exists and has the right permissions
mkdir($new_path, 0777, true);
}
$pdf = new FPDI();
$pagecount = $pdf->setSourceFile($filename); // How many pages?
// Split each page into a new PDF
for ($i = 1; $i <= $pagecount; $i++) {
$new_pdf = new FPDI();
$new_pdf->AddPage();
$new_pdf->setSourceFile($filename);
$new_pdf->useTemplate($new_pdf->importPage($i));
try {
$new_filename = $end_directory.str_replace('.pdf', '', $filename).'_'.$i.".pdf";
$new_pdf->Output($new_filename, "F");
echo "Page ".$i." split into ".$new_filename."<br />\n";
$pdf2text = new PDF2Text();
$pdf2text->setFilename($end_directory.$filename);
$pdf2text->decodePDF();
$text = $pdf2text->output();
echo "This is page text =====>> ".$text."<br><br>";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
}
// Create and check permissions on end directory!
split_pdf("test.pdf", 'split/');
?>
Please help me I don't have any idea why is this happening?
I want to open a directory in php and then display the link for downloading that file.
this is what I am trying.
if(is_dir($dir))
{
$op=opendir($dir);
echo"Files in the directiry are<br>";
while(($file=readdir($op))!==false)
{
if(strpos($file,$ext,1))
{
echo "".$file."<br>";
}
}
}
this shows downloading links but only upto space.
PHP function rawurlencode apparently gives you better system coverage. urlencode actually doesn't work on my localhost.
<?php
$dir = 'apps';
$ext = 'pdf';
if(is_dir($dir))
{
$op=opendir($dir);
echo"Files in the directory are<br>";
while(($file=readdir($op))!==false)
{
if(strpos($file,$ext,1))
{
echo '' . $file . '<br>';
}
}
}
?>
More on the subject here: urlencode vs rawurlencode?
My Web application stored in directory of XAMPP/htdocs/projectname/. And I have images(source) & img(destination) folders in above directory.I am writing following line of code to get the copy images from one folder to another. But I get the following Warnnigs: (Warning: copy(Resource id #3/image1.jpg): failed to open stream: No such file or directory in C:\xampp\htdocs) and images are not copied into destination.
<?php
$src = opendir('../images/');
$dest = opendir('../img/');
while($readFile = readdir($src)){
if($readFile != '.' && $readFile != '..'){
if(!file_exists($readFile)){
if(copy($src.$readFile, $dest.$readFile)){
echo "Copy file";
}else{
echo "Canot Copy file";
}
}
}
}
?>
Just a guess (sorry) but I don't believe you can use $src = opendir(...) and $src.$readFile like that. Try doing this:
$srcPath = '../images/';
$destPath = '../img/';
$srcDir = opendir($srcPath);
while($readFile = readdir($srcDir))
{
if($readFile != '.' && $readFile != '..')
{
/* this check doesn't really make sense to me,
you might want !file_exists($destPath . $readFile) */
if (!file_exists($readFile))
{
if(copy($srcPath . $readFile, $destPath . $readFile))
{
echo "Copy file";
}
else
{
echo "Canot Copy file";
}
}
}
}
closedir($srcDir); // good idea to always close your handles
Replace this line in your code, this will work definitely.
if(copy("../images/".$readFile, "../img/".$readFile))
you are giving wrong path ,if path of your file say script.php is "XAMPP/htdocs/projectname/script.php" and images and img both are in "projectname" folder than you should use following values for $srcPath and $destPath,change their values to
$srcPath = 'images/';
$destPath = 'img/';
public function getImage()
{
$Path='image/'; //complete image directory path
$destPath = '/edit_image/';
// makes new folder, if not exists.
if(!file_exists($destPath) || file_exists($destPath))
{
rmdir($destPath);
mkdir($destPath, 0777);
}
$imageName='abc.jpg';
$Path=$Path.$imageName;
$dest=$destPath.$imageName;
if(copy($Path, $dest));
}