How can I convert excel file to pdf using TCPDF

0 votes

I need to convert an excel report with graphs to a pdf file. I attempted to use PHP excel, but the charts do not load. The only PDF that is produced has a table. Here is my PHP excel code.

$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?

Oct 20, 2022 in Others by Kithuzzz
• 38,000 points
2,175 views

1 answer to this question.

0 votes

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);
answered Oct 21, 2022 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

How can I convert a Word document to PDF?

This is a difficult task, made even ...READ MORE

answered Oct 31, 2022 in Others by narikkadan
• 63,600 points
1,341 views
0 votes
1 answer

How to convert excel to PDF using Python

Specify your whole output path instead of ...READ MORE

answered Nov 10, 2022 in Others by narikkadan
• 63,600 points
3,044 views
0 votes
0 answers

how can I deploy a test in pytest framework to airflow using DAG?

I have established an automative testing framework ...READ MORE

Oct 4, 2021 in Others by Yuan
• 120 points
679 views
0 votes
1 answer

How to convert data from txt files to Excel files using python

Hi , there are few steps to ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
14,049 views
0 votes
1 answer

How to upload excel file to php server from <input type="file">

You first need to upload the file ...READ MORE

answered Jun 9, 2022 in JQuery by gaurav
• 23,260 points
6,810 views
0 votes
1 answer

How to create Dropdown list in excel using php

Try this: $objValidation = $objPHPExcel->getActiveSheet()->getCell('B'.$i)->getDataValidation(); $objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_LIST ); $objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_INFORMATION ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,600 points
2,473 views
0 votes
1 answer

How to find out how many rows and columns to read from an Excel file with PHPExcel?

Solution: $file_name = htmlentities($_POST['file_name']); $sheet_name = htmlentities($_POST['sheet_name']); $number_of_columns = htmlentities($_POST['number_of_columns']); $number_of_rows ...READ MORE

answered Oct 23, 2022 in Others by narikkadan
• 63,600 points
8,132 views
0 votes
1 answer

Converting excel to pdf using PHP

If utilizing PHP on a Windows PC, ...READ MORE

answered Oct 23, 2022 in Others by narikkadan
• 63,600 points
5,720 views
0 votes
1 answer

How can I convert excel to PDF by Libreoffice and keep all format from excel file?

"Times New Roman" typeface does not have ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,600 points
1,676 views
0 votes
1 answer

How to convert pdf file to excel file using python

Just specify your whole output path instead ...READ MORE

answered Nov 4, 2022 in Others by narikkadan
• 63,600 points
4,184 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP