I am using third party application PHPWord_0.6.2_Beta to displaying microsoft word content in to web page with codeigniter, but I don't find tutorial do that, just find this tutorial who used to create ms word file and writing inside that.
$this->load->library('word');
//our docx will have 'lanscape' paper orientation
$section = $this->word->createSection(array('orientation'=>'landscape'));
$section->addText('Hello I am tester');
$section->addTextBreak(1);
$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(1);
$this->word->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$this->word->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');
$section->addText('I have only a paragraph style definition.', null, 'pStyle');
$filename='test.docx'; //save our document as this file name
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPWord_IOFactory::createWriter($this->word, 'Word2007');
$objWriter->save('php://output');
how to displaying microsoft word content into web page in codeigniter?
Download PHPWord
Extract Zip.(You will get 3 files)
copy phpword folder and phpword.php file to third_party folder inside application Folder.
Create a new library inside your CodeIgniter’s application/libraries. Let’s call it Word.php
paste this code inside word.php
Check this as well
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/PHPWord.php";
class Word extends PHPWord {
public function __construct() {
parent::__construct();
}
}
and in controller use this
<?php
$this->load->library('word');//load librerry as usual
$section = $this->word->createSection(array('orientation'=>'portrait'));
$section->addText('Hello I am tester');
$section->addTextBreak(1);
$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(1);
$this->word->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$this->word->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');
$section->addText('I have only a paragraph style definition.', null, 'pStyle');
$filename='test.doc'; //save our document as this file name
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPWord_IOFactory::createWriter($this->word, 'Word2003');
$objWriter->save('php://output');
if this not work add this lines too
$this->output->set_header("HTTP/1.0 200 OK");
$this->output->set_header("HTTP/1.1 200 OK");
$this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");
set_header Reference
Related
Wrote a custom module for Joomla. The user enters a file name in the text filed and the pdf file with the entered value as name should be downloaded. But when clicked the page is redirected to home page. Here's the code
$file = $_POST['posttext'] . '.' . 'pdf';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;
filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
Tried adding ob_flush() as one of the asker suggested but no good. The code works perfectly as a separate php file.
Try the following
$app = JFactory::getApplication();
$file = $app->input->get('posttext') . '.pdf';
if (JFile::exists($file)) {
// File headers
header("Content-type: application/pdf");
header("Content-Disposition: attachment");
// File contents.
readfile($file);
}
If you are still having issues, you could check if the file exists in Joomla and download the file in a new tab/window using window.open()
I am creating a resource site for my campus where teachers upload files to folder and reference of that filed stored in db, and students can download those files using download option in student view side, and all these I have done in codeigniter using model view controller.
I need help for coding to download files. I managed to display a set of files but could not download yet, can anyone please help me with this? I'll be so thankful.
Try this
$download_file = 'C:\example.zip';//path of the file
$content_type = mime_content_type($download_file);//mime type of the file
$file_name = basename($download_file);//file name
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: ".$content_type);
header("Content-Disposition: attachment; filename=\"".$file_name."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($download_file));
while (ob_get_level()) {
ob_end_clean();
}
readfile($download_file);
I have followed all the instruction about the installation of PHPWord in Codeigniter here
I just don't know where to locate my output file (filename.docx) and how to know if it is working or not. there is no error message.
here is my code:
$PHPWord = $this->word;
$section = $PHPWord->createSection(array('orientation'=>'landscape'));
$section->addText('Hello World!');
$section->addTextBreak(2);
$section->addText('I am inline styled.', array('name'=>'Verdana', >'color'=>'006699'));
$section->addTextBreak(2);
$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, >'size'=>16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', >'spaceAfter'=>100));
$section->addText('I am styled by two style definitions.', 'rStyle', >'pStyle');
$section->addText('I have only a paragraph style definition.', null, >'pStyle');
$filename='kem.docx'; //save our document as this file name
header('Content-Type: application/vnd.openxmlformats->officedocument.wordprocessingml.document'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell >browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
btw, i'm using ubuntu 12.04 LTS
There are some errors in the code. In all the styling arrays there are extra >s. Maybe just copy&paste errors...
Because you output to php://output no file will be written it will just be sent directly to browser. Instead use something like $objWriter->save('d:\www\' . $filename); to create an output file.
Using the following code all works fine. it will both output to file and send it to browser.
<?php
require_once 'PHPWord.php';
$PHPWord = new PHPWord();
$section = $PHPWord->createSection(array('orientation'=>'landscape'));
$section->addText('Hello World!');
$section->addTextBreak(2);
$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(2);
$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');
$section->addText('I have only a paragraph style definition.', null, 'pStyle');
$filename='kem.docx'; //save our document as this file name
header('Content-Type: application/vnd.ms-word'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell >browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
$objWriter->save('d:\www\\php\\genDoc\\' . $filename);
/* For Unix / Linux
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
$objWriter->save('/home/ubuntu/workspace/uploads/'. $filename);
*/
?>
I have download the package PHPWord_0.6.2_Beta.zip from this siteThere are 2 directory : Examples and PHPWord, and 1 file : PHPWord.phpI put PHPWord directory and PHPWord.php in application/third_party and create new library named 'Word.php' in application/libraries as explained in this articleHere is the new library code Word.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/PHPWord.php";
class Word extends PHPWord {
public function __construct() {
parent::__construct();
}
}
?>
Now we can easily use PHPWord as CI libraryI have tried Text Example from directory Example in downloaded package, here is the original code
<?php
require_once '../PHPWord.php';
// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
// Add text elements
$section->addText('Hello World!');
$section->addTextBreak(2);
$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(2);
$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');
$section->addText('I have only a paragraph style definition.', null, 'pStyle');
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Text.docx');
?>
And I tried implement it in my CI controller, here is the code PHPWord_Text.php
<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');
class PHPWord_Text extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('word');
}
function index() {
$PHPWord = $this->word; // New Word Document
$section = $PHPWord->createSection(); // New portrait section
// Add text elements
$section->addText('Hello World!');
$section->addTextBreak(2);
$section->addText('I am inline styled.', array('name'=>'Verdana', 'color'=>'006699'));
$section->addTextBreak(2);
$PHPWord->addFontStyle('rStyle', array('bold'=>true, 'italic'=>true, 'size'=>16));
$PHPWord->addParagraphStyle('pStyle', array('align'=>'center', 'spaceAfter'=>100));
$section->addText('I am styled by two style definitions.', 'rStyle', 'pStyle');
$section->addText('I have only a paragraph style definition.', null, 'pStyle');
// Save File / Download (Download dialog, prompt user to save or simply open it)
$filename='just_some_random_name.docx'; //save our document as this file name
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
}
}
?>
Access it in
http://localhost/codeigniter/index.php/PHPWord_Text
Foilaa!!! my code works! But... I'm a bit confused when i tried to translate Template Example in directory Examples from downloaded package into a new CI controller, here is the original code of Template.php
<?php
require_once '../PHPWord.php';
$PHPWord = new PHPWord();
$document = $PHPWord->loadTemplate('Template.docx');
$document->setValue('Value1', 'Sun');
$document->setValue('Value2', 'Mercury');
$document->setValue('Value3', 'Venus');
$document->setValue('Value4', 'Earth');
$document->setValue('Value5', 'Mars');
$document->setValue('Value6', 'Jupiter');
$document->setValue('Value7', 'Saturn');
$document->setValue('Value8', 'Uranus');
$document->setValue('Value9', 'Neptun');
$document->setValue('Value10', 'Pluto');
$document->setValue('weekday', date('l'));
$document->setValue('time', date('H:i'));
$document->save('Solarsystem.docx');
?>
Can anyone please help me with this problem? please.. T_TNOTE : i dont want it to be saved in server, i want it to show download dialog prompt user wether to save it or open it, works like some code down here
// Save File / Download (Download dialog, prompt user to save or simply open it)
$filename='just_some_random_name.docx'; //save our document as this file name
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
I think you may be experiencing more than one issue with Template.php.
First, you may be trying to figure out what it is doing.
With the other examples x.php produces x.docx. In the case of Template.php, it uses Template.docx to produce Solarsystem.docx.
The variables Value1, Value2, Value3, etc. are all defined within Template.docx as ${Value1}, ${Value2}, ${Value3}, etc. If you change the values on the right in Template.php, it will change the corresponding values in Solarsystem.docx.
Second, you seem to be concerned with output.
What works for me is this:
$filename = 'nameOfFile.docx';
$document->save($filename);
header('Content-Description: File Transfer');
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);
This way still saves it on the server but downloads it too. If I use both $document->save(); and $objWriter->save(); I get a blank document when I try to use readfile(); on the file saved by objWriter.
I thing this is correct :
$filename = 'nameOfFile.docx';
$document->save($filename);
header('Content-Description: File Transfer');
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);
unlink($filename);
Method save() of Template class returns name of temp file document, so you just give it to user with readfile($tempName);
I'm not sure I get your question correctly, so excuse me if I'm wrong.
In your 4th code block you've got a line
$document->save('Solarsystem.docx');
If you don't want to save the file to the local disk, but instead send it to the client, you'll just have to remove that line and add the code from the 5th code block:
// Save File / Download (Download dialog, prompt user to save or simply open it)
$filename='just_some_random_name.docx'; //save our document as this file name
header('Content-Type: application/vnd.openxmlformats- officedocument.wordprocessingml.document'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
require_once APPPATH . '/src/PhpWord/Autoloader.php';
use PhpOffice\PhpWord\Autoloader as Autoloader;
Autoloader::register();
Im trying to generate pdf using dompdf, how can I open the pdf in a new tab in a browser? Like, I Click A link for the PDF and it should open in a new tab, not save it automatically. I want to give the user a choice to save the file after seeing it first. how do i do that?
whenever i use $pdf->output at the end of the file, it does not change a thing, the file is still downloaded automatically.
please help. thanks.
Whether a PDF is downloaded or viewed in the browser depends on a couple of factors. One is your browser settings, but we have no control there. The second is how dompdf present the PDF to the browser. You can tell dompdf to offer the PDF for direct viewing using $dompdf->stream('my.pdf',array('Attachment'=>0));.
As far as opening in a new tab. That depends on how you are generating the PDF. But the simplest way it to provide a link with a target attribute.
I have a same problem into my site (http://www.pdfwebcreator.com)
My solution is:
$myfile = fopen($strFileName, "r") or die("Unable to open file!");
$fileSize = filesize($strFileName);
header("HTTP/1.1 200 OK");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"temporaryPdf.pdf\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fileSize);
echo fread($myfile, $fileSize);
}
I don't know if you got, but if you use false in this line:
$dompdf-> stream("pasta/doc/relatorio.pdf", array("Attachment" => false));
You can see the pdf in the browser.
Well that you can do with the ->stream(); at the end of the chain.
Example:
//Routes (web.php in case laravel version >= 5.4)
Route::get('/pdf', 'PdfController#pdfStream')->name('pdfStream');
//PdfController.php
public function pdfStream(Request $request) {
$data["info"] = "I is usefull!";
$pdf = PDF::loadView('whateveryourviewname', $data);
return $pdf->stream('whateveryourviewname.pdf');
}
//yourViewPage.blade.php
<a href="{{route("pdfStream")}}" target="_blank" > click me to pdf </a>
Here more information
Am still experimenting, but something like this works fine as well
$pdf->loadView('file/path', compact('values'));
return $pdf->stream();
With this you can add dynamic values to your pdf file page within the browser.