Converting DOCX to PDF with PHPWord and DomPDF - php

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.

Related

Installation of DOMPDF with PHPExcel

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.

Edit SVG file with PHP

I want to open and manipulate a SVG graphic with PHP. I found there is a PEAR class XML_SVG. This looks great, but seems to only create new files. I did not find any load_svg() function that loads an existing SVG from file or string.
Is there a way to load an existing SVG file and then manipulate it with Pear XML_SVG?
There is not. I went through all the files.
https://github.com/pear/XML_SVG

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.

Read XLS to import in MySQL with PHP

I am creating a module that users can upload a '.xls' file and then it must be inserted in MySQL with PHP.Server is free bsd and have all permissions by now.I have tried PHPexcelReader,but it is not working.Can u suggest me something that it can do the work.
p.s.: with phpexcelreader is writing 'The filename xxxxxxx it not readable', but it IS readable and can be downloaded.
The filename is not readable? Is it possible the file was renamed and you are still using the old name?
Well I use PHPExcel and it's available at this link
PHPExcel
You might need to pass the full absolute path to the XLS file, something like this for example:
/var/www/testing.somesite.com/uploads/xxxxxxx.xls
Or just CWD to the directory which contains the file before trying to access it.

Categories