addObject() function is not working with .docx file in php - php

I am trying to attach(embedding) a .docx file to word document using phpword addObject() function, it's attaching file but while clicking on attached file it's not opening. If i do it for .doc file it's opening the attached file. I am using phpword library.
<?php
require_once '../PHPWord.php';
// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
// Add text elements
$section->addText('You can open this OLE object by double clicking on the icon:');
$section->addTextBreak(2);
// Add object
$section->addObject('Test.docx');
//if i use $section->addObject('Test.doc'); it's opening attached file. here Test.doc is word97-2003 format.
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Object.docx');
?>

Multiple issues were already logged for this, but no solution proposed for the issue yet :-(
addObject() function is not working with .docx file, it's working with .doc(word97-2003) file
Add Object Issue
Unable to open embedded object

Related

How to translate the content of a PDF file and then replace it with the translated content?

I know how to use the Google Translate API. I want to know one thing do you have an idea to translate the PDF file without losing in the file format. I had tried to convert the PDF file to DOCX and then translate the file and then return it to PDF but the conversion from PDF to DOCX failed because of many PHPWORD BUGs.
Here is the question and the codes that I had received in order to request the conversion from PDF to DOCX.
<?php
require_once 'vendor/autoload.php';
// Create a new PDF reader
$reader = \PhpOffice\PhpWord\IOFactory::createReader('PDF');
$reader->setReadDataOnly(true);
// Load the PDF file
$phpWord = $reader->load('example.pdf');
// Save the DOCX file
$writer = $PhpOfficePhpWordIOFactory::createWriter($phpWord, 'Word2007');
$writer->save('example.docx');
echo 'PDF file converted to DOCX successfully!';
<?php
require_once 'vendor/autoload.php';
use PhpOffice\PhpWord\IOFactory as WordIOFactory;
// Convert PDF to text
exec('pdftotext -layout input.pdf output.txt');
// Load text file
$text = file_get_contents('output.txt');
// Create new DOCX file
$phpWord = new \PhpOffice\PhpWord();
$section = $phpWord->addSection();
$textrun = $section->addTextRun();
$textrun->addText($text);
// Save DOCX file
$objWriter = WordIOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('output.docx');
Do you have any idea how I can translate the PDF file without going through the conversion to other formats?

PHP how to create a textfile containing hyperlinks

Using PHP, creating hyperlinks on the screen is no problem using an ECHO statement.
Now that everything is working OK. I would like to save the output to a textfile in such a way that the hyperlinks still work when the file is opened in Word or excel.
You should have a look at the PHPWord library.
With this library you can create Word documents in PHP including hyperlinks and other awesome stuff.
Hyperlink example:
<?php
require_once '../PHPWord.php';
// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
// Add hyperlink elements
$section->addLink('http://www.google.com', 'Best search engine', array('color'=>'0000FF', 'underline'=>PHPWord_Style_Font::UNDERLINE_SINGLE));
$section->addTextBreak(2);
$PHPWord->addLinkStyle('myOwnLinkStyle', array('bold'=>true, 'color'=>'808000'));
$section->addLink('http://www.bing.com', null, 'myOwnLinkStyle');
$section->addLink('http://www.yahoo.com', null, 'myOwnLinkStyle');
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Link.docx');
?>

Loading A .docx with PHPWord

I am attempting to load a .docx file with PHPWord, set a value in it, and save the file. The issue is the saved file comes out as a blank word document, 7kb in size. The document I put in is large and has multiple pages. Obviously its not loading it right. My code is as follows:
$PHPWord = new \PhpOffice\PhpWord\PhpWord();
$document = $PHPWord->loadTemplate('Resources/documents/test.docx');
$document->setValue('theDate', '2014-07-25');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
$xmlWriter->save("php://output");
I have tried with multiple test.docx files of different sorts, but the output file is always blank.
I was able to solve this issue with what Progi1984 linked, calling $document->saveAs() and then reading the file to download it. Thank you :)

Convert Doc File into Docx with php?

i want to read content of Doc file or convert a doc file into Docx
I have used COM object but it's not working because i've linux based server.
I have also tried with shell_exec command but it doesn't work because there's no any feature provide on shared server .
is there any api ? so that i can convert a Doc file using Docx
If you want to use PHP, try out PHPWord.
There's an example on how to convert from .doc to .docx on this page:
require_once '../PHPWord.php';
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('Excel2003.doc');
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Excel2007.docx');

Error in generated pdf file using zend_pdf under Magento

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.

Categories