Installation of DOMPDF with PHPExcel - php

I am having problems exporting Excel files to .pdf format using DOMPDF. I'm using PHPExcel (1.7.9, 2013-06-02), TCPDF (6.2.13) and Dompdf (0.7.0). For the moment the testing of Dompdf is done on a Win10/Wampserver 3.0 machine, but I'll have to use it on live server (Apache2.2.22 and PHP5.4.35). All three packages work OK if used alone, as expected (PHPExcel and TCPDF for more than three years on both test and live servers, and Dompdf was installed just recently on the test bed).
In addition I've been able to produce .pdf files with PHPExcel and TCPDF. The code is practically the same as in some of the PHPExcel examples and the crucial part is the following:
require_once __phpexcel__ . 'PHPExcel.php';
PHPExcel_Settings::setPdfRenderer(PHPExcel_Settings::PDF_RENDERER_TCPDF, __tcpdf__);`
Where __phpexcel__ and __tcpdf__ are constants pointing to the directories where the PHPExcel.php and tcpdf.php reside.
PHPExcel+TCPDF work fine, but they produce a rather large .pdf file (1.2MB for couple of cells with UTF-8 characters).
However, if I try to convert the same .xlsx file using Dompdf I have a problem. The crucial code in this case is
require_once __phpexcel__ . 'PHPExcel.php';
PHPExcel_Settings::setPdfRenderer(PHPExcel_Settings::PDF_RENDERER_DOMPDF, __dompdf__);`
__dompdf__ in this case is the directory where Dompdf files reside
(among others: dirs lib, etc and autoload.inc.php).
The error I receive is:
Fatal error: Class 'DOMPDF' not found in phpexcel\PHPExcel\Writer\PDF\DomPDF.php on line 36.
After examining the file __phpexcel__\PHPExcel\Writer\PDF\DomPDF.php I found that line 30 in that file is
$pdfRendererClassFile = PHPExcel_Settings::getPdfRendererPath() . '/dompdf_config.inc.php'; and there is no dompdf_config.inc.php file in the distribution of Dompdf!
Changing dompdf_config.inc.php to autoload.inc.php doesn't work!
I'm bewildered what should I do. The samples provided at PHPExcel and DOMPDF site do not provide any answers, or I'm not able to find them.

Related

Converting DOCX to PDF with PHPWord and DomPDF

I'm running a web only server (So no server access and no things like Composer or LibreOffice) and I want to convert a DOCX template to PDF. I have succesfully processed my template with PHPWord (Downloaded using php-download.com since I can't use composer), everything fine there, but when I try to use DomPDF (also downloaded through phpdownload) to convert my docx file to a PDF file, PHP throws me "Class 'Dompdf\Dompdf' not found"...
I created a seperate file to just convert an already known docx file just to exclude any environmental issues
This is my code:
require_once("includes/PHPWord/vendor/autoload.php");
$inputfile = "files/temp/offerte_Hankie-Pankie.docx";
$path = realpath(realpath(__DIR__) . '/includes/dompdf');
echo "realpath: " . $path;
\PhpOffice\PhpWord\Settings::setPdfRendererPath($path);
\PhpOffice\PhpWord\Settings::setPdfRendererName(\PhpOffice\PhpWord\Settings::PDF_RENDERER_DOMPDF);
//Load temp file
$phpWord = \PhpOffice\PhpWord\IOFactory::load($inputfile);
//Save it
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save('result.pdf');
Using the echo I confirmed the folder is set properly: "/home/myusername/domains/mydomain.nl/public_html/projects/pdftemplate/includes/dompdf"
So i tried parsing the subfolders "src" or "lib" without success, I tried parsing the actual autoloader file "autoload.inc.php" and didn't work either. I tried all of the above paths with and without ending "/" (Just to be sure) and still class not found... Even though I'm using exact copies of code found to be (presumably) working on the internet.
I tried finding it in the PHPWord documentation, which didn't give me any solutions. I also tried excluding PHPWord and just using DomPDF, but since DomPDF only accepts html as input, and I don't know how (And didn't want spend another hour trying that) to convert docx to HTML, this was also a dead end.
Just as a reference, here is my file structure:
Work dir
CodeIsInThisFile.php
includes
dompdf
lib
src
Autoloader.php
autoloader.inc.php
PHPWord
vendor
autoload.php
So either I messed something up in my file structure because of the manual downloads (Which seems the most likely to me), or my code is wrong (Also likely)...
First download the dompdf library using composer or using git.
Use the realpath() or try to use the absolute path for PDF rendering library.

PHPExcel files doesn't load

i need to read from excel file using php, the problem is these two lines:
require 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';
//Rest of code
Once I add these lines my code blocks , i'm sure the path is ok , i tried './Classes/PHPExcel etc..' doesn't work
When i execute it, it doesn't display anything , i often do echo 'smth', to see where it stops , but here only when i add these lines it stops, I use PHP 4.3 and PHPExcel 1.8
Can you guess where the error comes from ?
If they're not loading, your file paths are incorrect. Simple as that.
Verify your file structure
current folder/root folder that has myfile.php, inside does it have a folder called Classes?
or... did you forget to include /phpexcel/Classes/ ?
default installation (if you followed the instructions)
require 'phpexcel/Classes/PHPExcel.php';
require_once 'phpexcel/Classes/PHPExcel/IOFactory.php';
Add debug code
You can add echo statements in the PHPExcel libraries to see if they are really loading... if they're not... consult step 1.
Try using an earlier version of PHP
To test you can easily download XAMPP or WAMP to setup a local web host and test it out.
I resolved the problem by using PHP EXCEL Reader because i use php 4 wich wont work with this library or with .xlsx files.
now it is done , and my sheet was read , still , only one sheet is read.
Thank you All !

Where is PHPExcel.php file located

I want to create an excel file in my phalcon php app. I've downloaded PHPExcel, but I am not able to find where the PHPExcel.php file is located. Could anyone tell me where it is?
You can put the library folder (PHPExcel/Classes/) phalcon_project/app/library/.
To use the class, you must use the method of registerDirs \Phalcon\Loader
See more in create Excel file in Phalcon php framework
In the downloaded zip file PHPExcel_1.7.9_doc.zip, there is a folder called Classes, there you can find PHPExcel.php. You only need to insert the whole Classes folder and include PHPExcel.php for a correct implementation.

mpdf: cannot find TTF TrueType font file

I'm trying to implement mpdf in CodeIgniter according to this article: http://codeigniter.com/wiki/mPDF_with_Codeigniter
I did it exactly the same way that is described there, but I got an error:
mPDF Error - cannot find TTF TrueType font file - mpdf/ttfonts/DejaVuSansCondensed.ttf
But the file is there. I tested this both on a webserver and on localhost. I changed chmod, neither did that have an effect.
What can I do about it?
I found out what was wrong.
As recommended in the CodeIgniter Wiki, I first had _MPDF_PATH in mpdf.php set to:
if (!defined('_MPDF_PATH')) define('_MPDF_PATH','mpdf/');
After I changed it to the absolute path, everything worked. The absolute path looks like this:
if (!defined('_MPDF_PATH'))
define('_MPDF_PATH','/home/example.com/htdocs/ci/application/libraries/mpdf/');
I had the same problem.
I've found my problem was the font. It was corrupted
I've downloaded the font from internet and I've replaced it in the ttfonts folder.
Everything worked!
Instead of defining _MPDF_PATH like this...
if (!defined('_MPDF_PATH')) define('_MPDF_PATH','mpdf/');
Change path according to this one... this will work without any error if you placed mdpf directory in your libraries dir
if (!defined('_MPDF_PATH')) define('_MPDF_PATH', APPPATH .
'libraries/mpdf/');
Got same result after uploading library files (auto) converted to lowercase filenames by file manager.

FPDF error: Could not include font metric file

I have a app which was done by someone else and now i am asked to look into one issue.
When a pdf report is generated it throws an error. This app uses FPDF to generate the PDF
FPDF error: Could not include font metric file
Earlier it was throwing the following error
Warning: FPDF::include(helveticab.php) [function.FPDF-include]: failed to open stream: No such file or directory
Warning: FPDF::include() [function.include]: Failed opening 'helveticab.php' for inclusion
FPDF error: Could not include font metric file
This was resolved by including a font folder with helveticab.php and other php files related to other fonts
But the Error FPDF error: Could not include font metric file is still there.
On searching the net the possible reasons were
font directory missing
Doesnt have access permissions for the font files.
I am not sure what permission need to given to the font folder or files in the folder.
Any help in this regard would be of great help.
I had the same issue. The issue was the path was incorrect to the folder with all of the fonts. So, I added updated the following line in the PHP file to reflect the correct path to the folder with all of the fonts.
define('FPDF_FONTPATH','class/fpdf_font/');
So, double check the path that this line defines, and it should work fine.
I belive you have already extracted fpdf zip file onto your localhost or system
Once, zip file is extracted you see directory structure as in below image
and insert the below code in test.php
<?php
define('FPDF_FONTPATH','font/');
//above line is import to define, otherwise it gives an error : Could not include font metric file
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
Now enjoy
My problem was, because of downloading the fpdf library from the page, some of the scripts they have there uses Arial font but, that font specially, was not included in the fonts directory. I just added define('FPDF_FONTPATH','fpdf/font/'); with relative path to the fpdf dir and changed font to Courier and ready!
In my case I use Linux (Debian), I had the same issue and the directories was right. I solved adding 777 permissions to the /font directory. And now it works like charm =)))
if you are using a external class that extends FPDF
like file name Custom_pdf.php
in that file you wrote your cutom codes
require_once('fpdf.php')
class Custom_PDF extends FPDF{
...
}
then you included in to your coding like
require_once('custom_pdf.php');
$pdf = new Custom_pdf();
$pdf->Write();
....
$pdf->output();
this is problem...
So you directly place the code in the file you need
require_once('fpdf.php')
class Custom_PDF extends FPDF{
....
}
$pdf = new Custom_pdf();
$pdf->Write();
...
$pdf->output();
it will works fine...
Thanks
sorry for the english mistakes...

Categories