issue when saving rtf file (phpword lib) - php

I have some issue when i saving RTF file with phpword lib.
I open an RTF file with IOFactory::load, then i save it with function write and I have some metadata on the file i don't know why.
Here is my code:
<?php
include_once 'vendor/phpoffice/phpword/samples/Sample_Header.php';
// Read contents
$name = 'Sample';
$source = __DIR__ . "/vendor/phpoffice/phpword/samples/resources/{$name}.rtf";
echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'RTF');
// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
include_once 'vendor/phpoffice/phpword/samples/Sample_Footer.php';
and when i open the new file who have been save It shows:
"Normal;Default Paragraph Font;Normal Table;Body Text;Corps de texte Car;footer;Pied de page Car;page number;Balloon Text;Texte de bulles Car;?;;?;;?;;?;?;?;?;?;?;;http://schemas.microsoft.com/office/word/2003/wordml2450
6
1
...)()()()()()Annexe I"
If someone have an idea why I have some metadata on my file that will be good.

Related

mPDF Error: Data has already been sent to output, unable to output PDF file

I have created a php code to generate a PDF using mPDF. The code works correctly on the local. But on the server this error is shown:
Data has already been sent to output, unable to output PDF file
I searched over net and find these solution all from stackoverflow but non of them works. I tried all these solutions and no one works.
1- There is not output before $pdf->Output() like echo
2- I set ini_set("session.auto_start", 0); before creating PDF
3- I use ob_end_clean with include mPDF library
But when I change output parameter from $pdf->Output('file.pdf','D') to $pdf->Output('file.pdf','F'), the pdf is creating in the directory correctly. (D mean download the file and F means save file in directory)
Why mPDF can create PDF in the server but cannot send to browser and force file to be downloaded? Any suggestion for why file cannot be downloaded by D parameter?
Edit #1: The codes:
<?php
include "/DIR/cost/mpdf/vendor/autoload.php";
if (isset($_POST['hazine'])) {
try{
$mpdf = new \Mpdf\Mpdf();
$rand = rand();
$stylesheet1 = file_get_contents('/DIR/cost/style.css');
$mpdf->WriteHTML($stylesheet1, 1);
ob_start();
include "/DIR/cost/entere_data.php";
include "/DIR/cost/php.php";
ob_end_clean();
include '/DIR/cost/footer.php';
ob_end_clean();
$dir = '/DIR/DCC/DDC' . $rand . '.pdf';
$pdf_file = 'DDC' . $rand . '.pdf';
$mpdf->Output($pdf_file, 'D');
}
} catch(\Mpdf\MpdfException $e){
echo $e->getMessage();
}
}
include "/DIR/cost/form.php";
?>
This 3 includes: include "/DIR/cost/entere_data.php"; , include "/DIR/cost/php.php"; , include '/DIR/cost/footer.php'; are PDF file contents.

PHPWord: fill a template and output .pdf

I'm using a PHPWord to create word documents. To do so I'm using layouts:
$template = new \PhpOffice\PhpWord\TemplateProcessor($this->config->application->fileTemplateFolder.'invoice.docx');
$template->setValue('date', date("F jS, Y"));
$temp_file = tempnam(sys_get_temp_dir(), 'invoice_');
$temp_file .= '.docx';
$template->saveAs($temp_file);
It works fine. My problem is to create the a PDF file. Here is how I trying to create the file:
$path = '/Users/andredx/Projects/x/vendor/dompdf/dompdf';
require_once __DIR__ . '/../../vendor/dompdf/dompdf/autoload.inc.php';
\PhpOffice\PhpWord\Settings::setPdfRendererPath($path);
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');
$temp_doc = \PhpOffice\PhpWord\IOFactory::load($temp_file); // this line executes
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($temp_doc , 'PDF'); // this one get' my an error which I cannot see
$xmlWriter->save('/Users/andredx/Projects/x/_test.pdf', TRUE);
unlink($temp_file);
I can't figure out what I'm doing wrong. Maybe the path of the converter, but I'm trying to insert the path in many ways and it is not working.
Relevant info: the document has images and tables.
Can you help me to find out what I'm doing wrong?
Thanks

PHP - Editing a file in zip archive and saving as another archive name before close

I have a Microsoft Word file that I am using as a template. (testingsample.docx) The plan is to use form input values to create a lease agreement. The code I found below works very well for opening up the .docx file and finding and replacing the required strings. The problem is it only works once. The first time it is ran, it overwrites my template. I am trying to find a way to open testingsample.docx, make the required string changes, and save the archive as testingsamplecopy.docx without altering testingsample.docx.
Thanks in advance for any help!
// Create the Object.
$zip = new ZipArchive();
$inputFilename = 'testingsample.docx';
// Open the Microsoft Word .docx file as if it were a zip file... because it is.
if ($zip->open($inputFilename, ZipArchive::CREATE)!==TRUE) {
echo "Cannot open $inputFilename :( "; die;
}
// Fetch the document.xml file from the word subdirectory in the archive.
$xml = $zip->getFromName('word/document.xml');
// Replace the strings
$xml = str_replace("1111","Tenant Name Here",$xml);
$xml = str_replace("2222","Address Here",$xml);
// Write back to the document and close the object
if ($zip->addFromString('word/document.xml', $xml)) { echo 'File written!'; }
else { echo 'File not written. Go back and add write permissions to this folder!l'; }
$zip->close();
header("Location: testingsample.docx");
?>
Just copy from the template file to the target file, then open the target file and not the template.
Also, I changed the code at the header line to use the variable of the name of the file and not a static one.
<?php
// Create the Object.
$zip = new ZipArchive();
$templateFilename = 'testingsample.docx';
$inputFilename = 'testingsamplecopy.docx';
if(!copy($templateFilename, $inputFilename)) {
die("Could not copy '$templateFilename' to '$inputFilename');
}
// Open the Microsoft Word .docx file as if it were a zip file... because it is.
if ($zip->open($inputFilename, ZipArchive::CREATE)!==TRUE) {
echo "Cannot open $inputFilename :( "; die;
}
// Fetch the document.xml file from the word subdirectory in the archive.
$xml = $zip->getFromName('word/document.xml');
// Replace the strings
$xml = str_replace("1111","Tenant Name Here",$xml);
$xml = str_replace("2222","Address Here",$xml);
// Write back to the document and close the object
if ($zip->addFromString('word/document.xml', $xml)) { echo 'File written!'; }
else { echo 'File not written. Go back and add write permissions to this folder!l'; }
$zip->close();
// I also chaned this to use your variable instead of a static value.
header("Location: $inputFilename");
?>

Saving DOMPDF to specified directory and then sending as email

Hi I am trying to save dompdf to a specific directory in codeigniter file and then taking the saved file and sending it as a email. I am using codeigniter to do all this work.
Here is the function.
public function generateCreditAggreement(){
$this->load->helper('file');
require_once(APPPATH.'third_party/dompdf/dompdf_config.inc.php');
$pdfroot = dirname(dirname(__FILE__));
$pdfroot .= '/third_party/pdf/';
$dompdf = new Dompdf();
$msg = $this->load->view('credit_agreement_view', '', true);
$html = mb_convert_encoding($msg, 'HTML-ENTITIES', 'UTF-8');
$dompdf->load_html($html);
$paper_orientation = 'Potrait';
$dompdf->set_paper($paper_orientation);
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Download folder
// $dompdf->stream('document.pdf');
//save the pdf file on the server
$pdf_string = $dompdf->output();
file_put_contents($pdfroot, $pdf_string );
}
The error I am getting is that
Message: file_put_contents(C:\Apache24\htdocs\faceloan\faceloan\application/third_party/pdf/): failed to open stream: No such file or directory
But the directory does exist , and the path name is right.
The path .../third_party/pdf/ is probably a directory and not a file. PHP can't save your data as a directory. Specify a filename as first parameter of file_put_contents(), e.g. third_party/pdf/my_document.pdf.

Error when I try to open pdf file from PdfParser in PHP

I wanto to read the text into PDF File with PHP. So, for this, I have used PdfParser library. So I have build this code:
<?php
require '/vendor/autoload.php';
include '/Smalot/PdfParser/Parser.php';
$parser = new Smalot\PdfParser\Parser();
echo "pippo";
$pdf = $parser->parseFile('Ricardo.pdf');
echo "pippo1";
$text = $pdf->getText();
echo "pippo2";
echo $text;//all text from Ricardo.pdf
?>
Now, if I try to open this page I can read only
pippo
and I can't see other print.
I don't see some error on the page. How can I fix it?

Categories