\PhpOffice\PhpWord issue converting HTML to DOCX - laravel

Thank you in advance
i want to convert HTML into DOCX so i used \PhpOffice\PhpWord library in laravel
The code for the same is as below
$html = "<html><body><h1>HELLO DEMO</h1></body></html>";
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
$phpWord->save(public_path('temp/demo.docx'), 'Word2007');
instead of saving the docx, it is showing the HTML on the webpage.
I want to save into a folder
is there anything missed by me, i used this in laravel so require_once "vendor/autoload.php"; might not be required?

You need the
require_once 'vendor/autoload.php';
also add and change
$path = public_path('temp/demo.docx');
$phpWord->save($path, 'Word2007');

Related

how to send dynamic PDF with mail() those generate by DOMPDF library in CI?

Actually, generate the dynamic PDF successfully with the help of dompdf library in CI but not send the pdf dynamic those generated here. So tell me how to do this?
$filename = "newpdffile";
require_once APPPATH.'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml($output);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream($filename);
$file_to_save="/application/dompdf/";
$pdf=file_put_contents($file_to_save, $dompdf->output());
$this->email->from('support#aurorax.co', 'aurora exchange');
$this->email->to('masnad#aurorax.co');
$this->email->subject('pdf');
$this->email->attach($pdf);
$this->email->message('Hello!');
If you have sucesssfully generated PDF and saved in dompfdf directory
then
$pdf= 'http://yourwebsite.com/dompfdf/<?php echo $pdfNameYouGenerated; ?>';
$this->email->attach($pdf);
Use APPPATH to get the file path
$file_to_save = APPPATH. "application/dompdf/";
$pdf = file_put_contents($file_to_save, $dompdf->output());
When PDF file successfully generated and save in the directory then attach while sending email
$this->email->from('support#aurorax.co', 'aurora exchange');
$this->email->to('masnad#aurorax.co');
$this->email->subject('pdf');
$this->email->attach($pdf);
$this->email->message('Hello!');
Please check below code:
$filename = "newpdffile";
require_once APPPATH.'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml($output);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$output = $dompdf->output();
$filePath = FCPATH.'application/dompdf/filename.pdf';
file_put_contents($filePath, $output);
$this->email->from('support#aurorax.co', 'aurora exchange');
$this->email->to('masnad#aurorax.co');
$this->email->subject('pdf');
$this->email->attach($filePath);
$this->email->message('Hello!');

codeigniter - How to get embed data in my case

i load data in my controller like
$name['name'] = "some text!";
$data['header'] = $this->load->view('header_view', $name, TRUE);
$this->load->view('myView', $data);
and in my view file: myView.php
echo $header;
how can i get data in $name ? thanks
Try:
$data['name'] = "some text!";
$data['header'] = $this->load->view('header_view', $data['name'], TRUE);
$this->load->view('myView', $data);
You can't because when it comes to myView, the header view is allready processed and so data is allready incoroporated into the output string.
The only way is to copy (merge) the contents of $name into $data.
You can get embed data by parsing the content of $data['header'] but it is a really bad solution.
To solve this issue you can generate header and footer along the content inside your controller like that:
$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');
Or you can use a template engine to include header/footer automaticly with the content template.
Infortunately, there is no buildin way to add an header easily in codeigniter. You'll have to come up with your own functions to do that in a helper or library (but you'll not be able to share data between header and content).

DOMPDF + Codeigniter, library upgraded, but now not working

I have been using the old version of dom pdf in codeigniter for pdf generation and recently I changed the core dompdf directory to newer version dompdf-0.5.2.zip.
Below is the code that I've been using to connect to dompdf class:
class Pdf{
var $_dompdf = NULL;
/**
* Constructor Method
**/
function __construct(){
ini_set("memory_limit", "1G");
require_once("Dompdf/dompdf_config.inc.php");
if(is_null($this->_dompdf)){
$this->_dompdf = new DOMPDF();
}
}
/**
* HTML to PDF Conversion method
**/
function convert_html_to_pdf($_html, $_filename = '', $_stream = false, $_orientation = "portrait"){
$this->_dompdf->set_paper("a4", $_orientation);
$this->_dompdf->load_html($_html);
$this->_dompdf->render();
if($_stream){
$this->_dompdf->stream($_filename);
} else{
file_put_contents($_filename, $this->_dompdf->output());
}
}
}
Now when I try to generate pdf, it gives me following error:
Fatal error: Class 'DOMPDF' not found in D:\xampp\htdocs\app\application\libraries\Pdf.php on line 21
any solution would be appreciated
Thanx
Please first try with this example. Just store this in a file under htdocs directory of your xampp or whatever server you are using.
<?php
require_once("dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
?>
Just change the dompdf directory path as per your requirement.
I solved this myself by uploading the dompdf version to the latest version, though it is beta, all seems to work perfectly now.

Display Cross-domain feed RSS in Wordpress site

I need to display cross-domain feeds-rss (XML format)in my site, but i get a error because ajax cross-domain call are not allowed. I've been told about json-p...anyone knows how to use or have some good tutorial?
Thanks
the simplest way is just to create an widget for wordpress or download some kind of like your requirement.
Because json-p load data in JSON format if you want to get data from JSON format then the given link will help you :
getJSON
ajax
or you can access the rss feed with php like given example :
$xml = 'http://blog.webtech11.com/feed';
$doc = new DOMDocument();
$doc->load($xml);
$item = $doc->getElementsByTagName('item');
//$data = array();
for($i=0; $i<=3; $i++){
$title = $item->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$link = $item->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
echo '<h2>' . $title . '</h2>';
}
in this example i access latest 4 blog entries..
hope this will help you

Magento - can't delete programmatically imported pictures

I hase used this code :
require_once("app/Mage.php");
Mage::app();
$fullImagePath = "app/code/local/Mycompany/Import/data/images/test_picture.jpg";
$product = Mage::getModel('catalog/product');
$product->load(16);
$product->setMediaGallery (array('images'=>array (), 'values'=>array ()));
$product->addImageToMediaGallery ($fullImagePath, array ('image'), false, false);
$product->save();
to import pictures to a product.
The problem in that is that when I delete the programmatically imported picture from the backend, it stay always visible in frontend.
I don't know what's wrong in my code.
Thanks for help.
I think in your import code instead of doing setMediaGallery() and hard coding your image url you should,
$config = $product->getMediaConfig();
//and get your file url from the set media path in your config.
$fileurl = $config->getMediaUrl('sth/test_picture.jpg');

Resources