I want to convert a docx file to pdf using phpword
my code looks like this:
$FilePath = APPPATH."media/Documentos/Facturas/Factura ".$FacturaId.".docx";
$FilePathPdf = APPPATH."media/Documentos/Facturas/Factura ".$FacturaId.".pdf";
//DOCX TO PDF
require_once APPPATH.'third_party/phpword/bootstrap.php';
$rendererLibraryPath = PHPWORD_BASE_DIR . '/vendor/dompdf/dompdf';
\PhpOffice\PhpWord\Settings::setPdfRendererPath($rendererLibraryPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');
$phpWord = new \PhpOffice\PhpWord\PhpWord();
//Load temp file
$phpWord = \PhpOffice\PhpWord\IOFactory::load($FilePath);
//Save it
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save($FilePathPdf, true);
But i generate an empty pdf file: LINK TO FILE
Paths are correct and .docx have content
I try with tcpdf render:
$rendererLibraryPath = PHPWORD_BASE_DIR . '/vendor/tecnickcom/tcpdf';
\PhpOffice\PhpWord\Settings::setPdfRendererPath($rendererLibraryPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');
and my pdf file have 2 pages without content: LINK TO 2ND FILE
Finally i found a free alternative solution installing libreoffice:
$FilePath = APPPATH."media/Documentos/Facturas/Factura ".$FacturaId.".docx";
$FilePathPdf = APPPATH."media/Documentos/Facturas/Factura ".$FacturaId.".pdf";
require_once APPPATH.'third_party/phpword/bootstrap.php';
$template = new \PhpOffice\PhpWord\TemplateProcessor(APPPATH.'media/Plantillas/Factura/Factura.docx');
foreach($data as $key => $value){
$template->setValue($key, $value);
}
$template->saveAs($FilePath);
shell_exec($this->CI->config->item('libreoffice_exec')." --headless --convert-to pdf --outdir \"".$Path."\" \"$FilePath\"");
Calling soffice.exe CLI with this parameters:
soffice.exe --headless --convert-to pdf --outdir "C:/media/Documentos/Facturas/pdf" "C:/media/Documentos/Facturas/Factura1.docx";
Related
I installed this laravel package to convert pdf file into text. https://github.com/spatie/pdf-to-text
I'm getting this error:
Could not read sample_1610656868.pdf
I tried passing the file statically by putting it in public folder and by giving path of uploaded file.
Here's my controller:
public function pdftotext(Request $request)
{
if($request->hasFile('file'))
{
// Get the file with extension
$filenameWithExt = $request->file('file')->getClientOriginalName();
//Get the file name
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
//Get the ext
$extension = $request->file('file')->getClientOriginalExtension();
//File name to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//Upload File
$path = $request->file('file')->storeAS('public/ebutifier/pdf', $fileNameToStore);
}
// dd($path);
$location = public_path($path);
// $pdf = $request->input('file');
// dd($location);
// echo Pdf::getText($location, 'usr/bin/pdftotext');
$text = (new Pdf('public/ebutifier/pdf/'))
->setPdf($fileNameToStore)
->text();
return $text;
}
Not sure why it's not working any help would be appreciated.
You used spatie/pdf-to-text Package.
I also tried to use this package but it gave me this kind of error.
So, I used asika/pdf2text package and it worked properly
Asika/pdf2text pacakge
And this is my code
$path = public_path('/uploads/documents/'. $file_name);
$reader = new \Asika\Pdf2text;
$output = $reader->decode($path);
dd($output);
Hopefully, it will be helpful for you.
I use dompdf for print invoices, but after upgrading php 7.2, it is stop working. When I print html then it is showing good but it is not render in pdf.
I use this code:
$invoice_template_dir = $smarty->template_dir
.'/middle/expenses/';
html = $exp_obj->printPDFInvoice($invoice_template_dir);
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("dspl-invoice-" . $exp_obj->expense_id . ".pdf",
array('Attachment' => 0));
exit;
use ZanySoft\LaravelPDF\PDF;
$mpdf = new PDF();
$mpdf->SetImportUse();
$pagecount = $mpdf->SetSourceFile(storage_path() . '/app/public/applications/test.pdf');
// test.pdf have 2 pages $pagecount have value 2.
for($i=1; $i <= $pagecount ; $i++) {
$tplId = $mpdf->ImportPage($i);
$mpdf->addPage();
$mpdf->UseTemplate($tplId);
// store each page as pdf file
$new_file_name = "new-".$i.".pdf";
$mpdf->Output($new_file_name, \Mpdf\Output\Destination::FILE);
}
I am getting an error Class 'Mpdf\Output\Destination' not found. I have tried below things as second argument of Output();
$mpdf->Output($new_file_name, \PDF\Output\Destination::FILE);
$mpdf->Output($new_file_name, \Mpdf\Mpdf\Output\Destination::FILE);
But, it is not working. Please help to store fetch page store as PDF file direct on directory.
ZanySoft\LaravelPDF\PDF uses mPDF in version 6.1 which does not have output destination class constants yet.
Use a plain string:
$mpdf->Output($new_file_name, 'F');
Or use composer package mpdf/mpdf in version >=7 directly.
I'm new to CodeIgniter and having the following issue. When I upload a file, its successfully uploaded on my local folder and the file name saved to the db. The problem is, file name has been changed after uploading.
eg:
file name "screenshot image 123.png" -> "screenshot_image_123.png"
It just convert the space into underscore;this issue occurred only when the file name having space.Other than this issue all the other files uploading successfully. Given below is my code which I used on my controller page:
public function upload_pic()
{
$image_path = realpath(APPPATH . '../uploads');
$config['upload_path'] = $image_path;
$config['allowed_types'] = "gif|jpg|jpeg|png";
$config['file_name'] = $_FILES['file']['name'];
$config['encrypt_name'] = TRUE;
$config['overwrite'] = TRUE;
$this->load->library('upload',$config);
$this->upload->initialize($config);
if(!$this->upload->do_upload('file'))
{
echo $this->upload->display_errors();
}
else
{
$finfo=$this->upload->data();
$data['uploadInfo'] = $finfo;
}
}
Can anyone help me????
Try before saving file name generate some unique
$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$filename = sha1_file($filename). md5(date('Y-m-d H:i:s:u')) . '.'. $ext; //not only this you can generate any format
$config['file_name'] = $filename; //Use this file name is db too
I want to use the extnsions provided in yii.
Please suggest some extensions in yii to compress/decompress uploaded files. and store on the server or directory and again to decompress them while downloading..
Here I have used an extension EAjaxUpload extension for uploading the file.
IN VIEW
<?php
$this->widget('ext.EAjaxUpload.EAjaxUpload',
array(
'id'=>'uploadResume',
'config'=>array(
'action'=>Yii::app()->baseUrl.'/index.php?r=site/word',
'allowedExtensions'=>array("doc","docx"),
'sizeLimit'=>10*1024*1024,
'minSizeLimit'=>1*1024,
'multiple'=>false,
'onComplete'=>'js:function(id, fileName, responseJSON){alert(messages);}),
?>
IN CONTROLLER
action{
Yii::import("ext.EAjaxUpload.qqFileUploader");
$folder = 'Resumes/';
$allowedExtensions = array("doc","docx");
$sizeLimit = 10 * 1024 *1024;
$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
$result = $uploader->handleUpload($folder);
if(isset($result['success']))
if($result['success']==true)
{
$fileSize=filesize($folder.$result['filename']);
$fileName=$result['filename'];
$mimetype = $fileName['mime'];
}
// rename($folder.$result['filename'],$folder.$fileName);
$return = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
echo $return;
}
.
You should use your systems tools, like tar or zip/unzip, from yii you could call this commands with exec or system