PHPExcel Save PDF - php

Help me i want to save excel in pdf format
The code:
include_once 'Classes/PHPExcel.php';
/** PHPExcel_IOFactory */
require_once ('Classes/PHPExcel/IOFactory.php');
include_once ('Classes/PHPExcel/Writer/Excel2007.php');
include_once ('Classes/PHPExcel/Writer/PDF.php');
include_once ('Classes/PHPExcel/Writer/PDF/DomPDF.php');
$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
//$rendererLibrary = 'tcPDF5.9';
//$rendererLibrary = 'mPDF5.4';
$rendererLibrary = 'domPDF0.6.0beta3';
$rendererLibraryPath = '/' . $rendererLibrary;
$objPHPExcel->getActiveSheet()->setTitle('Orari');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->setSheetIndex(0);
$objWriter->save('esp.pdf');
I want to save in pdf format, but when i try to save i have an error response from phpexcel:
Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'Unable to load PDF Rendering library' in C:\xampp\htdocs\fserv\Classes\PHPExcel\Writer\PDF\DomPDF.php:34 Stack trace: #0 C:\xampp\htdocs\fserv\esportatore.php(190): include_once() #1 C:\xampp\htdocs\fserv\index.php(44): include('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\fserv\Classes\PHPExcel\Writer\PDF\DomPDF.php on line 34

You need DomPDF for rendering the PDF. Check if you have the DomPDF library, otherwise download and implement it as follow:
$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'domPDF0.6.0beta3';
$rendererLibraryPath = dirname(__FILE__). 'libs/classes/dompdf' . $rendererLibrary;

Related

How to fix 'Unable to load PDF Rendering library' Export Excel to PDF using PHPExcel TCPDF

I want to export my Excel to PDF file using TCPDF in PHPExcel and save it to the path, but I found this error 'Unable to load PDF Rendering library'. Please help, how to fix this?
I'm not sure this is because of rendererLibraryPath
$excel2 = PHPExcel_IOFactory::createReader('Excel2007');
$excel2 = $excel2->load('./template/invoice.xlsx'); // Empty Sheet
$excel2->setActiveSheetIndex(0);
$excel2->getActiveSheet()->setCellValue('C6', $row['nama'])
->setCellValue('C7', $row['idpel'].$row['periode_tagihan'])
->setCellValue('H6', $row['idpel'])
->setCellValue('C6', $row['nama'])
->setCellValue('H7', $row['rekening_bulan'])
->setCellValue('H8', $row['tarif'])
->setCellValue('J8', $row['daya'])
->setCellValue('E12', $row['rpptl'])
->setCellValue('E13', $row['rpbpju'])
->setCellValue('E14', $row['rpppn'])
->setCellValue('E15', $row['rpmat'])
->setCellValue('E16', $row['tagsus'])
->setCellValue('E17', $row['rpbk'])
->setCellValue('E19', $row['rptottag'])
->setCellValue('C22', $terbilang)
->setCellValue('E25', $row['batas_akhir_bayar'])
->setCellValue('H33', date('d F Y'));
//$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
//$objWriter->save("./template/cetak/".$row['idpel'].$row['periode_tagihan'].".xlsx");
$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'tcpdf';
$rendererLibraryPath = dirname(__FILE__) . '/' . $rendererLibrary;
PHPExcel_Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
$path = "./template/cetak/".$row['idpel'].$row['periode_tagihan'].".pdf";
$objWriter = new PHPExcel_Writer_PDF($excel2);
$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'PDF');
$objWriter->setSheetIndex(0);
$objWriter->save($path);
* Remove TCPDF folder from vendor and install again with php composer.phar *
Hope this link help to find your solution

how to correct the error "Uncaught exception 'PHPExcel_Reader_Exception' "

I am trying to send two values to two cells in an excel ALREADY CREATED, which has the name see.xlsx ... but it turns out that I get the following error:
Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open consulta.xlsx for reading! File does not exist.' in C:\xampp\htdocs\importexcel\lib\PHPExcel\PHPExcel\Reader\Excel2007.php:343 Stack trace: #0 C:\xampp\htdocs\importexcel\f.php(15): PHPExcel_Reader_Excel2007->load('see.xlsx') #1 {main} thrown in C:\xampp\htdocs\importexcel\lib\PHPExcel\PHPExcel\Reader\Excel2007.php on line 343
my excel file I have stored on my desktop, and I'm using excel 2010
the code is the following:
f.php
<?php
error_reporting(E_ALL);
set_time_limit(0);
date_default_timezone_set('Europe/London');
set_include_path(get_include_path() . PATH_SEPARATOR . 'lib/PHPExcel/');
require_once 'lib/PHPExcel/PHPExcel/IOFactory.php';
$fileType = 'Excel2007';
$fileName = 'see.xlsx';
// Read the file
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);
// Change the file
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B1', 'World!');
// Write the file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->save($fileName);
?>

Converting .xlsx to .pdf with PHPExcel in Kohana

I am using Kohana and I have a problem with converting an xlsx file to *.pdf.
This is my code for generating a PDF:
$rendererName=PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'DomPDF.php';
$rendererLibraryPath = APPPATH."modules/phpexcel/vendor/PHPExcel/Classes/PHPExcel/Writer/PDF/".$rendererLibrary;
if (!PHPExcel_Settings::setPdfRenderer(
$rendererName,
$rendererLibraryPath
)) {
die(
'Please set the $rendererName and $rendererLibraryPath values' .
PHP_EOL .
' as appropriate for your directory structure'
);
}
$objWriter = new PHPExcel_Writer_PDF($spreadsheet);
$objWriter->save("php://output");
And here is the spreadsheet variable:
$spreadsheet = PHPExcel_IOFactory::createReader('Excel5')->load('modules/phpexcel/vendor/template.xlsx');
When I try to run this code I get a response
PHPExcel_Writer_Exception [ 0 ]: PDF Rendering library has not been defined.
Could anyone help? I suppose the problem could be in my phpdom including code, but I have no idea how to fix it in Kohana.

How to set the stream content in PHPExcel? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm using PHP and PHPExcel to get the image filename from a database and perform the stream reader operation. I set my image by using the setImageResource function but it isn't working as expected. Here's my code:
<?php
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
require_once '../classes/PHPExcel/IOFactory.php';
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Asia/Calcutta');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'dompdf.php';
$rendererLibraryPath = dirname(__FILE__) . '/../libs/classes/dompdf/';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Tes');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Image...');
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$img = imagecreatefrompng('C:/Users/Downloads/logo.png');
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Test image');
$objDrawing->setDescription('Test image');
$objDrawing->setImageResource($img);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150);
$objDrawing->setCoordinates('A4');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
if (!PHPExcel_Settings::setPdfRenderer($rendererName,$rendererLibraryPath)) {
die(
'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
'<br />' .
'at the top of this script as appropriate for your directory structure'
);
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('test.pdf');
exit;
Okey, First thing first PHPExcel_Worksheet_MemoryDrawing() can't solve your problem if you insist to use stream content and pass that to your worksheet your PDF will not render your image. But you can use `PHPExcel_Worksheet_Drawing()' if you want to render your image too. You can trick it like this :
You can retrieve your image file from stream_get_contents or file_get_contents
and then you recreate it to the file.
The last you pass your image file path to the $objDrawing->setPath($path_to_your_image);
So, if you want to render your image too to the worksheet you must use PHPExcel_Worksheet_Drawing() and for that you must point to your file path to render that image.
I assume this an Issue to the PHPExcel working with DomPDF. So, save a little discuss here and edit your objDrawing to this :
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath('logo.png');
For Complete sample I've made, Try this :
include 'PHPExcel.php';
require_once("dompdf/dompdf_config.inc.php");
$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'dompdf.php';
$rendererLibraryPath = dirname(__FILE__). '/dompdf';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Tes');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Image...');
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objPHPExcel->getActiveSheet()
->getColumnDimension('A')
->setWidth(50);
$objPHPExcel->getActiveSheet()
->getRowDimension('2')
->setRowHeight(200);
// $img = imagecreatefrompng('logo.png');
$data = file_get_contents("logo.png");
// $img = imagecreatefromstring($data);
$fp = fopen("logo1.png","w");
fwrite($fp,$data);
fclose($fp);
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath('logo1.png');
// $objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Test image');
$objDrawing->setDescription('Test image');
// $objDrawing->setImageResource($img);
// $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
// $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT);
// $objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150);
$objDrawing->setCoordinates('A2');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
if (!PHPExcel_Settings::setPdfRenderer($rendererName,$rendererLibraryPath)) {
die(
'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
'<br />' .
'at the top of this script as appropriate for your directory structure'
);
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('test.pdf');
echo date('H:i:s') . " Done writing file.\r\n";
Glad to help you, Hope this will help you out :)

How can I convert excel file to pdf using TCPDF?

I need to generate a report with graphs in excel to pdf. I tried using phpexcel but it fails to load the charts. It only generates PDF with tables. Here's my code in phpexcel.
$newpdf = "report-".date('m_d_Y_H_i_s', strtotime('now')).".pdf";
$objPHPExcel1 = new PHPExcel();
$objPHPExcel1 = PHPExcel_IOFactory::load('csv/'.$filename);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setFitToWidth(true);
$objPHPExcel1->getActiveSheet(0)->getPageSetup()->setFitToHeight(true);
$objPHPExcel1->getActiveSheet()->setShowGridLines(false);
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="'.$newpdf.'"');
header('Cache-Control: max-age=0'); //no cache
$objWriter1 = new PHPExcel_Writer_PDF($objPHPExcel1);
$objWriter1->save('php://output');
exit;
I also tried loading the contents from HTML using jchartfx for graphs without success. Any idea?
Thanks in advance.
PHPExcel can only read charts from Excel2007 (OfficeOpenXML .xlsx) files, and will only load them if you explicitly tell it to do so.
if (PHPExcel_IOFactory::identify('csv/'.$filename) == "Excel2007") {
$objReader = PHPExcel_IOFactory::createReader("Excel2007");
$objReader->setIncludeCharts(TRUE);
} else {
die("Can't load charts");
}
$objPHPExcel = $objReader->load('csv/'.$filename);
When writing to a PDF file, you must have the relevant library installed (tcpdf in your case) and tell PHPExcel that you want to use it and the directory where tcpdf is installed.
$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'tcPDF5.9';
$rendererLibraryPath = '/php/libraries/PDF/' . $rendererLibrary;
if (!PHPExcel_Settings::setPdfRenderer(
$rendererName,
$rendererLibraryPath
)) {
die('NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
EOL .
'at the top of this script as appropriate for your directory structure'
);
}
You also need jpgraph installed, and again PHPExcel needs to know where it is installed
$rendererName = PHPExcel_Settings::CHART_RENDERER_JPGRAPH;
$rendererLibrary = 'jpgraph3.5.0b1/src';
$rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary;
if (!PHPExcel_Settings::setChartRenderer(
$rendererName,
$rendererLibraryPath
)) {
die('NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
PHP_EOL .
'at the top of this script as appropriate for your directory structure'
);
}
Finally, you need to tell the PDF Writer explicitly to generate the charts
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save($outputFileName);

Categories