PHPSpreadsheet auto row height not works with LibreOffice latest version - php

I am using PHPSpreadsheet now, I am trying to get rows to auto height. It is working fine in MSOffice But Not work in LibreOffice.
Question How to make the row auto height work in LibreOffice latest version. Works fine in MSOffice.
Autoheight
$spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
foreach($spreadsheet->getActiveSheet()->getRowDimensions() as $rowID) {
$rowID->setRowHeight(-1);
}
Controller
<?php
require(APPPATH . 'vendor/autoload.php');
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class Events extends MX_Controller {
public function test() {
$spreadsheet = new Spreadsheet();
$spreadsheet->getProperties()->setCreator('')
->setLastModifiedBy('')
->setTitle('')
->setSubject('')
->setDescription('');
$spreadsheet->getDefaultStyle()->getFont()->setName('Arial');
$spreadsheet->getDefaultStyle()->getFont()->setSize(24);
foreach(range('A','B') as $columnID) {
$spreadsheet->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
}
$spreadsheet->getActiveSheet()->getStyle('A')->getAlignment()->setWrapText(true);
$spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
foreach($spreadsheet->getActiveSheet()->getRowDimensions() as $rowID) {
$rowID->setRowHeight(-1);
}
$spreadsheet->setActiveSheetIndex(0)
->setCellValue("A1",'Firstname')
->setCellValue("B1",'Lastname')
->setCellValue("A2",'John')
->setCellValue("B2",'Doe');
$spreadsheet->getActiveSheet()->setTitle('Users Information');
$spreadsheet->setActiveSheetIndex(0);
/* Here there will be some code where you create $spreadsheet */
// redirect output to client browser
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="myfile.xls"');
header('Cache-Control: max-age=0');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('php://output');
exit;
}
}

Try the following code
$spreadsheet->getActiveSheet()->getStyle('A1:D4')
->getAlignment()->setWrapText(true);

Related

view pdf stored on public/storage on laravel?

My code doesn't save pdf's, I need to show it. If it's possible download it too.
<td><a name="Manual" href="{{asset('storage/app').'/'.$machineData->getManual()}}">
Manual de {{$machineData->getMachineName()}}</a></td>
On controller I have this
protected function downloadFile($src)
{
if(is_file($src))
{
$finfo=finfo_open(FILEINFO_MIME_TYPE);
$content_type=finfo_file($finfo,$src);
finfo_open($finfo);
$file_name=basename($src).PHP_EOL;
$size=filesize($src);
header("Content-Type: $content_type");
header("Content-Disposition: attachment; filename=$file_name");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $size");
return true;
}
else
{
return false;
}
}
public function download(Request $request)
{
$note=$request->file('manual');
if(!$this->downloadFile(app_path(). $note))
{
return redirect("/machineIndex");
}
}
Please help, I just want to show the file, thanks!!
I believe you can achieve this with TCPDF
$pdf = new TCPDF('P', 'in', [$sizeX, $sizeY], true, 'UTF-8', false);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->AddPage();
$pdf->SetMargins(0, 0, 0, true);
$pdf->SetAutoPageBreak(false, 0);
$pdf->Output($outputPath, 'I');
This code would create a new pdf document, then have a look at https://tcpdf.org/ to make the necessary changes and the $pdf->Output($outputPath, 'I'); at the end will open the pdf without saving it to storage (almost like a dd).
Edit:
I wrote this with the assumption that you didn't want to save the file to storage, if the file is already saved in public/storage you can present it with a direct url to it.
You would need the symlink with php artisan storage:link then guide to where it is in the directory. For example 'laravel.test/public/pdfs/pdf1.pdf'
More info on https://laravel.com/docs/8.x/filesystem

PHPspreadsheet show blank in browser tab

I have used https://github.com/PHPOffice/phpspreadsheet this package to download
csv files in codeigniter
When I try to download it shows blank in browser tab and not showing download option
there are no errors
Tried with below code
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
class Reports extends Admin_Controller
{
public function download()
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
}
}
Can any anyone help on that please??.Thanks
Hope this will help you :
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class Reports extends Admin_Controller
{
public function index()
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$spreadsheet->setActiveSheetIndex(0);
$filename = 'hello world';
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
}
}
for csv just change the extesion .xlsx to .csv (case-sensitive)
The download() function you have there is only for writing a new file. Not anything to do with downloading a file. However I managed to output a file using this code here. I outputted a csv file so I could check it more easily on my server but the premise is still the same with xlsx files.
public function writeFile() {
$this->load->library('Spreadsheet');
$output_file = getcwd() . "/test/hello_world.csv";
$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->setCellValueByColumnAndRow("1", "1", "Hello World!");
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Csv');
$writer->save($output_file);
}
I think part of your problem was not loading the library correctly using the CodeIgniter nomenclature. If you are trying to upload files I would reccomend using the upload library which comes with CodeIgniter. The documentation can be found here: https://www.codeigniter.com/userguide3/libraries/file_uploading.html
I would then use phpspreadsheet to edit it to your hearts content once it has been uploaded. Hope this helps.

Calling PHPword third party libraries in Code Igniter

I have a problem with calling PHPWord library in Code Igniter, I already using PHPExcel and there are no problems everything goes smoothly.
But I have a problem with this PHPWord, because when I tried to use the same method to call the PHPExcel, it doesnt solve anything. I still seeing an error.
It's not like I don't search it on the google, I already searched it, but still, no one can solve my problem, I don't where is the problem.
So let me explain what is the problem.
FYI: I using PHP 7.1.1
Because the PHP version, PHPWord got some errors that says a class named as a String that is known as variable, so because of that error, I changed it to Strings all of them that associated with that class.
After I changed it, I want to call the class with library file.
This is the file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH.'/third_party/PHPWord/Autoloader.php';
use PhpOffice\PhpWord\Autoloader as Autoloader;
use PhpOffice\PhpWord\Settings as Settings;
use PhpOffice\PhpWord\PhpWord as PhpWord;
/* I tried two different ways */
// class Word extends PhpWord {
// public function __construct() {
// parent::__construct();
// }
// }
Autoloader::register();
Settings::loadConfig();
And this is the Controller code that calls the class.
$this->load->library('word');
$section = $this->word->createSection(array('orientation'=>'landscape'));
After I did that, it says Non-existent class: Word, I don't know anymore where is the problem, is it because I changed the class name? or it doesn't compatible with my CI version(3)?
Because I already tried different ways and still cannot solve this problem, can someone help me with this problem and show me what I did wrong? Thank you.
I haven't tried with PHP7 but I found this Github repo which worked for me.
https://github.com/webeasystep/phpword_codeigniter
I just copied the application\third_party\PhpWord folder and application\library\PhpWord.php into the same locations in my project and then used some of the code from the controller provided, e.g...
public function test()
{
$this->load->library('Phpword');
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->getCompatibility()->setOoxmlVersion(14);
$phpWord->getCompatibility()->setOoxmlVersion(15);
$filename = 'test.docx';
// add style settings for the title and paragraph
$section = $phpWord->addSection();
$section->addText("Hello, world");
$section->addTextBreak(1);
$section->addText("It's cold outside.");
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($filename);
// send results to browser to download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
flush();
readfile($filename);
unlink($filename); // deletes the temporary file
exit;
}
This works fine, I get a .docx when I hit my controller/test in a browser
Following library is working smoothly for me for PHP 7 version without any error.
https://github.com/rifqisucahyo/CI_PHPWord
class Word extends PHPWord {
public function __construct() {
parent::__construct();
$this->load->library('word');
}
function index() {
require_once APPPATH."/third_party/PHPWord.php";
$PHPWord = $this->word; // New Word Document
$section = $PHPWord->createSection(); // New portrait section
// Add text elements
$section->addText('Hello World!');
$section->addTextBreak(2);
$section->addText('Mohammad Rifqi Sucahyo.', 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));
// Save File / Download (Download dialog, prompt user to save or simply open it)
$section->addText('Ini Adalah Demo PHPWord untuk CI', 'rStyle', 'pStyle');
$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');
}
}

prestashop display attachment inline

I'm creating a custom PDF file containing some product details. I would like to display product attachments that are images. I have changed the attachment controller to
header('Content-Disposition: inline; filename="'.utf8_decode($a->file_name).'"');
When I visit the url through the browser it displays nicely:
http://domain.nl/nl/index.php?controller=attachment&id_attachment=483
But using this url inside an img tag in my PDF template gives:
TCPDF ERROR: [Image] Unable to get image: http://domain.nl/nl/index.php?controller=attachment&id_attachment=483
Does anyone have any idea on how to make this work?
I created a new file next to AttachmentController.php called AttachmentInlineController.php. Contents are slightly different:
<?php
class AttachmentInlineControllerCore extends FrontController
{
public function __construct()
{
$a = new Attachment(Tools::getValue('id_attachment'), $this->context->language->id);
if (!$a->id) {
Tools::redirect('index.php');
}
if (ob_get_level() && ob_get_length() > 0) {
ob_end_clean();
}
header('Content-Transfer-Encoding: binary');
header('Content-Type: '.$a->mime);
header('Content-Length: '.filesize(_PS_DOWNLOAD_DIR_.$a->file));
header('Content-Disposition: inline; filename="'.utf8_decode($a->file_name).'"');
#set_time_limit(0);
readfile(_PS_DOWNLOAD_DIR_.$a->file);
exit;
}
}?>
I removed the hook, changed content-Disposition to inline, and changed the function from postProcess() to __construct. Now if you use:
http://domain.nl/nl/index.php?controller=attachmentInline&id_attachment=483
It will display that attachment inline, and you can use this url in image tags etc.

PHPExcel with Codeigniter retirning Internal Server 500 Error

I tried following the following tutorial to implement the PHPExcel library to Codeigniter http://www.ahowto.net/php/easily-integrateload-phpexcel-into-codeigniter-framework/
For some reason when I run a test that looks like something like this
reports.php controller localhost/fticketarchive/index.php?/reports/excelTest
<?php
class Reports extends CI_Controller
{
function excelTest(){
//load our new PHPExcel library
$this->load->library('Excel');
//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');
}
}
so for some reason when I reach this at localhost/fticketarchive/index.php?/reports/excelTest
I get the following error in console with no server response.
500 Internal Server Error
Any ideas would be greatly appreciated! Thank you.
Have you tried moving the call to the library into __construct?
class Reports extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('excel');
}
function exceltest(){
//proceed
}
}
For me at least, this works.

Categories