Can't get the PHPExcel and CodeIgniter integration to work - codeigniter

This has been confusing me for days, I can't get the code below to work:
public function index (){
//load our new PHPExcel library
$this->load->library('excel');
$objPHPExcel = new PHPExcel();
//activate worksheet number 1
$this->excel->setActiveSheetIndex(0);
//name the worksheet
$this->excel->getActiveSheet()->setTitle('test worksheet');
//set cell A1 content with some text
$this->excel->getActiveSheet()->setCellValue('A1', 'This is just some text value');
//change the font size
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
//make the font become bold
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
//merge cell A1 until D1
$this->excel->getActiveSheet()->mergeCells('A1:D1');
//set aligment to center for that merged cell (A1 to D1)
$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$filename='just_some_random_name.xls'; //save our workbook as this file name
header('Content-Type: application/vnd.ms-excel'); //mime type
header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
header('Cache-Control: max-age=0'); //no cache
//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
//if you want to save it as .XLSX Excel 2007 format
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
//force user to download the Excel file without writing it to server's HD
$objWriter->save('php://output');
}
}
All I get is a blank page. I tried tracing the codes to determine why it won't work and the line $this->load->library('excel');
is causing it.
I have created the excel.php file in /libraries, pasted the code from the example on the web.
Can someone help me on this one please?
Thanks in advance

The problem is how you install PHPExcel with CodeIgniter.
See you answer here:
PHPExcel error in CodeIgniter "Unable to load the requested class: iofactory"
Drop the phpexcel folder into application/third-party and create a library.
(for library use the class proviced by #devrooms in his answer)
Then do the following in controllers:
$this->load->library("excel");
$this->excel->load("/path/to/input.xls");
$this->excel->setActiveSheetIndex(0);
$this->excel->getActiveSheet()->SetCellValue('B2', "whatever");
$this->excel->save("/path/to/output.xls");
If the above is not working see this answer to deactivate the php safe mode on your server
phpexel and codeigniter getting error

Related

XREF table broken when creating PDF file in Joomla component

When importing and modifying a PDF file inside a Joomla component using the FPDF and FPDI libraries, after sending the file to the browser, the PDF viewer claims the XREFS table to be broken.
Here's part of what I have:
<?php
...
use \setasign\Fpdi\Fpdi;
include "fpdf/fpdf.php";
include "fpdi2/src/autoload.php";
class MycompoViewMycompo extends JViewLegacy
{
protected $pdf;
function display($tpl = null) {
...
$this->pdf = new Fpdi();
$this->pdf->AddPage();
$this->pdf->setSourceFile('components/com_mycompo/views/mycompo/template.pdf');
$tplIdx = $this->pdf->importPage(1);
$this->pdf->useImportedPage($tplIdx, 0, 0, null, null, true);
$this->pdf->SetFont('Arial', '', 13);
$this->pdf->SetTextColor(0, 0, 0);
$this->pdf->SetXY(175, 49);
$pdf->Cell(0, 0, 'some Text', 0, 0, 'R');
// This actually happens in the layout...
// But I'd like to simplify here
$doc =& JFactory::getDocument();
$doc->setMimeEncoding('application/pdf');
$this->pdf->Output('gift_coupon_generated.pdf', 'D', true);
}
...
}
?>
The document is sent to the browser.
It also can be downloaded and opened by a PDF viewer (I use PDF Exchange Editor).
All the file's conents seem to be correct. No visible problems.
But a warning is shown that the XREFS table is broken.
(The exact message reads like "One or more XREF data streams were not found". I don't know the exact English message, as I use a different language version)
When working with the same code OUTSIDE of Joomla (i. e. in a standard PHP environment and a simple php web page), the file is also created perfectly fine and this error is not shown.
Any ideas on what might be the reason for this error message?
Or how I could debug it further?
Or is using FPDF/FPDI not supported with Joomla?
Please find the generated file here: https://file.re/2021/10/16/banktransferform/
Best regards
Tom

Download TXT file from URL and save to PC on Flutter Web

I have a problem with my project, I wanna to download a .txt file and save to any directory with Flutter WEB, I think I put the setup but It does not work. Now de .txt file display the content in the same tab of browser, but I want to download this file, here is my code:
void downloadFile(String url) {
html.AnchorElement anchorElement = new html.AnchorElement(href: url);
anchorElement.download = "plantilla_simulador.txt";
anchorElement.dispatchEvent(html.Event.eventType('MouseEvent', 'click'));
anchorElement.style.display = 'none';
anchorElement.click();
}
Perhaps you are trying to download file from another site.
Keep in mind that download attribute only works for same-origin URLs, or the blob: and data: schemes.
This code should download file:
html.AnchorElement(href: 'index.html')
..download = 'some_name.txt'
..style.display = 'none'
..click();
but this one should open file in browser:
html.AnchorElement(href: 'https://raw.githubusercontent.com/flutter/flutter/master/flutter_console.bat')
..download = 'some_name.txt'
..style.display = 'none'
..click();

PHPOffice spreadsheet to PDF export

I have some spreadsheets in MS excell. Trying to convert them to PDF by using PHPOffice and mPDF class. I have a problem to create and display footers on every created PDF page
This is working code for creating a PDF from spreadsheet.
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf($pe);
Is there any solution or method i can call after this to generate footers before use save() method?
Thanks in advance
You can use the footer methods e.g. generate a footer with linenumbers
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$spreadsheet = $reader->load('<path>'); //Path of reader sheet
$sheet = $spreadsheet->getActiveSheet();
$sheet->getHeaderFooter()->setFirstFooter('&C&"-,Bold"Page &P from &N'); //generate Fristfooter
There are more header/footer possibility look documentation or class documentation

Set title of pdf using mpdf file codeigniter

$this->load->library('m_pdf');
$pdf = $this->m_pdf->load();
$pdf->SetTitle('My Doc');
$pdf->WriteHTML($html);
$pdf->Output($pdfFilePath, "F");
Used SetTitle() function of mpdf but by default it is using name of file : "Untitled Document - _Test_Company_10120.pdf"
Also set the <title></title> tag but no use.
You used $pdf->SetTitle('My Doc');
Set the title for the document. The title is displayed at the top of the Adobe Reader screen when viewing the PDF file, and is include in the document metadata, which can be seen when inspecting the document properties in Adobe Reader.
So, Try this :
$pdfFilePath = "directory/your_pdf_name.pdf";
$pdf->Output($pdfFilePath, "F");

How can give name for generate pdf using mpdf codeigniter

if anybody konws this,please help me.how to give name to pdf.
i generated pdf using mpdf codeigniter. on click of a button the pdf wil be viewed. but how can give name for that pdf? it shows 1 on the top of the pdf.how can i give name to that pdf?
My controller
public function viewpdf($key,$option) {
if($option=='1')
{
$searchdata['fetchproduct']=$this->b2bproduct_model->fetch_productdata1($key);
}
if($option=='2')
{
$searchdata['fetchproduct']=$this->b2bproduct_model->fetch_productdata2($key);
}
if($option=='3')
{
$searchdata['fetchproduct']=$this->b2bproduct_model->fetch_productdata3($key);
}
$html=$this->load->view('moderator/pdf_data', $searchdata,true);
//this the the PDF filename that user will get to download
$pdfFilePath = "shany.pdf";
//load mPDF library
$this->load->library('m_pdf');
//generate the PDF from the given html
$this->m_pdf->pdf->WriteHTML($html);
//download it.
$this->m_pdf->pdf->Output($pdfFilePath, "I");
}
Use this code.
$mpdf=new mPDF();
$mpdf->SetTitle('My Title');
$mpdf->WriteHTML('<p>Hallo World</p>');
$mpdf->Output('filename.pdf');
Set the title for the document. The title is displayed at the top of the Adobe Reader screen when viewing the PDF file
Use code as bellow
public function mypdf() {
$this->load->library('pdf');
$pdf = $this->pdf->load();
$html=$this->load->view('welcome_message',null,true);
$pdf->WriteHTML($html);
// write the HTML into the PDF
$output = 'your_given_name.pdf'; //You can give a name of your generated pdf file or you can create it auto on timestamp by using $output = time().'.pdf'.
$pdf->Output("$output", 'I');
}
If you send data to view page then replace the variable with null in line 4.
For more details please see the tutorial from here.
If you are interested on DOMPDF please see from here.

Resources