I have an existing word document in my computer and like to edit this file from my website (using PHP). I was able to find PHPWORD but this deals with new documents only. I don't want to code PHP for the whole document, instead wish to use it for the stuff that varies.
Does anybody know any way out?
https://github.com/PHPOffice/PHPWord
PHPWord also features a Reader which can be used to edit existing documents.
#nssmart,
Yes,mark a section on your word document with a variable such as {value1} and then, if you have PHPWORD downloaded, you can replace that section using code
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate(Doc location);
$document->setValue('value1', 'Description');
This can be used to fill data in tables also.
https://github.com/PHPOffice/PHPWord
I also did this problem in phpword using the following code
if(!file_exists('file/word.docx')){
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('demo.docx');
$templateProcessor->setValue('name', 'Akbarali');
$templateProcessor->setValue('time','13.02.2021');
$templateProcessor->setValue('month', 'January');
$templateProcessor->setValue('state','Uzbekistan');
$templateProcessor->saveAs('file/word.docx');
}
This will change the words in the demo.docx file. The new file is then saved in the word.docx file folder.
you can define variables in the form of $ {name} in the demo word file.
that is, ${name}, ${time}, ${month} and ${state}
Related
I have a word document with various sections.
I need to append some more text in a particular section of word document using PHP.
After searching in google, came across phpoffice/phpword.
Using that I am able to write a new word document.
Not getting how to insert text in a given section.
Looks there is template processing in phpoffice.
Using template processing is it possible to insert text in a given section.
Is there any such example ?
Or is there any other good open source (php based) available, that provides easy apis to append text in a particular section.
Using below code, I can generate a new word document in phpoffice.
$filename = "sample";
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText(htmlspecialchars("Some text"));
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('./doc/' . $filename . '.docx');
However I want to write section wise (with section numbers like 1.1 or 1.2).
At a later stage need to append some more text to a particular section.
If it can be done using phpoffice, that will be my most preferred approach.
I have a word document (template) which contains some images that I have to replace with others that are present on the drive. The images that exist in the documents are currently blank. And the ones that will replace them have some text written on them. One way to do was to edit each document and then by double-clicking the image I use the Pbrush to edit the image. Other way is below.
I used PHPWord to do the job. Open the document and replace the image in the word/media.
The problem: At first look it looks like the image has been replaced. Also, if I check through zip/winrar the images are replaced. But when I double click the image in the doc I see the old(blank) image in PBrush. I want the new edited image to show up. Or an alternate way to do the job.
This function does not exist in the original PHPWord but found it as the quick fix to do the job. (PhpWord/Template.php)
public function replaceImage($path,$imageName) {
$this->_objZip->deleteName('word/media/'.$imageName);
$this->_objZip->addFile($path,'word/media/'.$imageName);
}
My Code:
$PHPWord = new PHPWord();
$document = $PHPWord[$i]->loadTemplate('ALL_FILES/Template.docx');
$document->replaceImage($oldImage, $newImage);
$document->save('ALL_FILES/genForms/updatedDoc.docx');
P.S. Hope it is clear what I am trying to say. If need be, I can upload a test document somewhere, in case the problem is not clear.
I'm coding a script in PHP which is taking an xml document with file_get_contents, i replace some caracters with str_replace and i write this file in a Word document with a fwrite.
Exemple :
$myContent = file_get_contents("../ressources/fichiers/modeles_conventions/modele_convention.xml");
$lettre = str_replace("#NOMENT#",utf8_encode($data['nomentreprise']),$lettre);
$newFileHandler = fopen("../ressources/fichiers/conventions/lettre_convention_1.doc","a");
fwrite($newFileHandler,$lettre);
fclose($newFileHandler);
In localhost it's working but on a server the problem is :
My xml file contains images, but my final .doc document doesn't retrieves these images.
I don't understand why my images are not retrieved.
Well i didn't find out a solution to my problem.
I get my xml file (which is in reallity a .doc file with an .xml extension)
$myContent = file_get_contents("../ressources/fichiers/modeles_conventions/modele_convention.xml");
I replace some stuff
$myContent = str_replace("#NOM_ENTREPRISE#",stripslashes($data['nomentreprise']),$myContent);
$myContent = str_replace("#STATUT_ENTREPRISE#",stripslashes($data['juridique']),$myContent);
I save my document
//On génère la convention
$newFileHandler = fopen("../ressources/fichiers/conventions/convention_".$data2['nomeleve']."_".$data2['prenomeleve']."_".$data3['idstage'].".doc","ab");
fwrite($newFileHandler,$myContent);
fclose($newFileHandler);
The xml document contains images, in localhost it retrieves images but not on the server.
exemple of xml code :
<w:r>
<w:rPr>
<w:rFonts w:ascii="Arial" w:h-ansi="Arial" w:cs="Arial"/>
<wx:font wx:val="Arial"/>
</w:rPr>
<w:pict>
<v:shape id="_x0000_i1028" type="#_x0000_t75" style="width:48.75pt;height:24pt">
<v:imagedata src="wordml://06000003.emz" o:title=""/>
</v:shape>
</w:pict>
</w:r>
</w:p>
Creating Word files using HTML is a way to let Word think that it is a Word document. Naming it .doc creates a file that is by default opened in Word. However, it isn't an actual Word document, you are faking it. It only works because Word also supports opening HTML. Other clients may not support HTML or not support all HTML entirely. For instance, the image tag does not work with TextMate on Mac, altough the bold tag works just fine.
In your XML, you must refer to the image using an absolute path, i.e. a path on the internet or a local file system path. For instance, <img src="image.png"> will not work, since the Word file does not know how to locate it. However, you may use <img src="http://yoursite.com/image.png">. I'm sure that you can also refer to your local file system, with for example the file: 'protocol'. This only works when the file is present on the file system where the file is opened though.
If this does not solve your problem, you should probably post your XML file here.
However, if you are creating this for a client or external system (so anything but yourself), I'd suggest using something like:
COM objects
This only works when Word is actually installed on the system where the web application runs.
<?php
$word = new COM("word.application") or die ("Can't create Word file");
$word->visible = 1;
$word->Documents->Add();
$word->Selection->TypeText("this is some sample text in the document");
$word->Documents[1]->SaveAs("sampleword.doc");
$word->Quit();
$word->Release();
$word = null;
?>
(source)
Office Open XML or other format
The new XML format used by Word is open source and can be modified more easily. I don't know the exact details, but it's basically some XML files compressed into a zip file and given the extension .docx.
If possible, you can also use the ODT format of OpenOffice. Most recent Word versions can also read this file and the format is open source. It is also more feasible to create PDF files than Word files using PHP.
phpLiveDocs
phpLiveDocs is an extension for PHP and can be used to create Word files.
I'm going to use JpGraph in my web-project. To do this, I downloaded the library into my project folder and created the following test code:
<?php
include ( "jpgraph-3.5.0b1/src/jpgraph.php");
include ("jpgraph-3.5.0b1/src/jpgraph_gantt.php");
// A new graph with automatic size
$graph = new GanttGraph (0,0, "auto");
// A new activity on row '0'
$activity = new GanttBar (0,"Project", "2001-12-21", "2002-02-20");
$graph->Add( $activity);
// Display the Gantt chart
$graph->Stroke();
?>
The error message is:
The image “http://localhost:10088/test/check.php” cannot be displayed,
because it contains errors.
What's wrong with my code? In fact, it was just copied from here. The only thing I changed was the path to the JpGraph library. So, do I need to import this library in some special way into my project (so far, I've just copied the JpGraph's folder into the project's folder)? I'm using Zend Studio.
This can be caused by extra whitespace on the page. Make sure you don't have any spaces or line breaks before your opening <?php tag.
I'm trying to create a PDF file, under a Magento phtml file, this is my code :
$pdf = new Zend_Pdf();
$pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
$page=$pdf->pages[0]; // this will get reference to the first page.
$style = new Zend_Pdf_Style();
$style->setLineColor(new Zend_Pdf_Color_Rgb(0,0,0));
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
$style->setFont($font,12);
$page->setStyle($style);
$page->drawText('example text here',100,($page->getHeight()-100));
$pdf->render();
$pdf->save('test.pdf','true');
My PDF file is created, but I can't open it with acrobat reader.
When I open it with a text editor and compare it with another simple pdf files, I noticed that in the first line was missing in my generated pdf file. it contains "%PDF-1.4"
How can I add this line programmatically with zend_pdf in my pdf file ?
Thanks for help.
According to the zend manual the second save parameter is only for updating files that already exist. In this case you are creating a new file so don't use that option.
$pdf->save('test.pdf');
PS. This answer is technically an RTM statement.