How to Convert HTML/CSS to PDF in PHP on Linux - php

I have been trying to convert an html page to PDF with out any luck, I have tried to use wkhtmltopdf (http://code.google.com/p/wkhtmltopdf/) and the PHP class provided by the same developers (http://code.google.com/p/wkhtmltopdf/wiki/IntegrationWithPhp) although the following command executed correctly in terminal:
wkhtmltopdf http:/www.googl.com test.pdf
I was unable to execute the command in PHP because of the following error:
cannot connect to X server.
Can any one provide me with a step-by-step tutorial in order to be able to convert html/css to PDF file, whether using wkhtmltopdf or any other decent solution.
Thanks in advance
here is the class that i have used http://code.google.com/p/wkhtmltopdf/wiki/IntegrationWithPhp
and below is my code
$html = file_get_contents("test.html");
$pdf = new WKPDF();
$pdf->set_html($html);
$pdf->render();
$pdf->output(WKPDF::$PDF_EMBEDDED,'sample.pdf');
Below is the error:
Fatal error: Uncaught exception 'Exception' with message 'WKPDF didn't return any data.
/usr/bin/wkhtmltopdf: /opt/lampp/lib/libxml2.so.2: no version information available
(required by /usr/lib/libgstreamer-0.10.so.0) No protocol specified wkhtmltopdf: cannot
connect to X server :0 </pre>' in /var/www/c4/components/com_wkhtmltopdf
/wkhtmltopdf.class.php:211 Stack trace: #0 /var/www/c4/components/com_wkhtmltopdf
/wkhtmltopdf.php(68): WKPDF->render() #1 /var/www/c4/components/com_wkhtmltopdf
/wkhtmltopdf.php(50): html2pdf() #2 /var/www/c4/index2.php(114): require_once('/var/ww...')
#3 {main} thrown in /var/www/c4/components/com_wkhtmltopdf/wkhtmltopdf.class.php on line 211

I found the solution, The following error "No protocol specified wkhtmltopdf: cannot
connect to X server :0" was basically caused by on old version of "wkhtmltopdf" which was installed using apt-get. The solution was to download the static binary from the code.google using the following link:
http://code.google.com/p/wkhtmltopdf/downloads/list
and then perform the following actions:
tar -jxvf wkhtmltopdf-0.11.0-static.tar.bz2
sudo aptitude install ia32-libs
Next, you’ll have to make a symbolic link pointing to WKHTMLTOPDF in /usr/bin.
sudo ln -s /full_path/WKHTMLTOPDF /usr/bin/WKHTMLTOPDF

Related

php Tesseract Error! The command "tesseract" was not found

I'm trying and failing to use tesseract php. I get this error:
Fatal error: Uncaught thiagoalessio\TesseractOCR\TesseractNotFoundException: Error! The command "tesseract" was not found. Make sure you have Tesseract OCR installed on your system: https://github.com/tesseract-ocr/tesseract
The current $PATH is C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;C:\Users\Peppe\AppData\Local\Microsoft\WindowsApps;C:\Users\Peppe\AppData\Roaming\Composer\vendor\bin in C:\Users\Peppe\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php:48 Stack trace: #0
C:\Users\Peppe\vendor\thiagoalessio\tesseract_ocr\src\TesseractOCR.php(26): thiagoalessio\TesseractOCR\FriendlyErrors::checkTesseractPresence('tesseract') #1 C:\xampp\htdocs\index.php(7): thiagoalessio\TesseractOCR\TesseractOCR->run() #2 {main} thrown in C:\Users\Peppe\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php on line 48
I'm using windows 10 with xampp installed in C:\xampp. php version 7.4
I installed tesseract.exe from https://github.com/UB-Mannheim/tesseract/wiki both x32 and x64
I used composer to install the https://github.com/thiagoalessio/tesseract-ocr-for-php and it gave no error.
<?php
require_once 'C:\Users\Peppe\vendor\autoload.php';
use thiagoalessio\TesseractOCR\TesseractOCR;
$ocr = new TesseractOCR("caption.jpg");
$content = $ocr->run();
echo $content;
?>
finally, the caption.jpg is in the htdocs folder, the main folder and same folder as index.php where the aforementioned code appears.
any solutions?
I found the answer:
the $PATH variable is taken from Windows. To set it right search for "system environment variables", click "environment variables", edit PATH and add the tesseract software folder, for example C:\Program Files\Tesseract-OCR
In my case I have to add new variable tesseract with full path C:\Program Files\Tesseract-OCR\tesseract.exe as showing in below screenshot
To install TESSERACT OCR and use it in PHP
You must perform the following steps:
(in my explanation, I assume you are on windows)
url src = https://tesseract-ocr.github.io/tessdoc/Installation.html
1- Download the TESSERACT software from URL:
https://github.com/UB-Mannheim/tesseract/wiki
2- From your composer, you must install:
url: https://packagist.org/packages/thiagoalessio/tesseract_ocr
composer require thiagoalessio/tesseract_ocr
3- Then, it is important to do things in the following order:
Your TESSERACT software (step 1) is installed in a directory
which will certainly be (it's up to you to check in your system)
folder = C:\Program Files\Tesseract-OCR
It is absolutely necessary to add this folder in the windows environment variables at the PATH level (user and system)
4- Then you restart your web environment (WAMP, LARAGON ...)
5- Verification that TESSERACT is operational.
Go to your CMD and write:
tesseract --version
Reply somethink like that :
tesseract v5.3.0.20221222
leptonica-1.78.0
libgif 5.1.4 : libjpeg 8d (libjpeg-turbo 1.5.3) : libpng 1.6.34 : libtiff 4.0.9 : zlib 1.2.11 : libwebp 0.6.1 : libopenjp2 2.3.0
Found AVX2
Found AVX
Found FMA
Found SSE4.1
Found libarchive 3.5.0 zlib/1.2.11 liblzma/5.2.3 bz2lib/1.0.6 liblz4/1.7.5 libzstd/1.4.5
Found libcurl/7.77.0-DEV Schannel zlib/1.2.11 zstd/1.4.5 libidn2/2.0.4 nghttp2/1.31.0
6- Operation test from a PHP script
uses an image on a white background with text
<?php
$app_class = 'thiagoalessio\TesseractOCR\TesseractOCR';
$file = 'C:/Users/admin/Desktop/im2.jpg';
$class = new ReflectionClass($app_class);
$infoImg = $class->newInstanceArgs();
$infoImg->lang('eng','jpn','spa','fr');
$infoImg->image($file);
$infoImg->withoutTempFiles();
$resultat = $infoImg->run();
var_dump($resultat);
?>

Cron Job on cPanel not running

I'm trying to run a cronjob in cPanel which gives me the following error:
Fatal error: Uncaught Error: Class 'mysqli' not found in /home/example/public_html/new_facebook_csv/csv_generator1.php:180
If run that file through browser by using the link example.com/new_facebook_csv/csv_generator1.php i get the right resaults without any errors, but if i try to run it with a cron job i get the error "Class 'mysqli' not found".
The cron job I'm running is the following:
php /home/example/public_html/new_facebook_csv/csv_generator1.php > /home/example/public_html/new_facebook_csv/facebook.log
and here is the logs i get after the cron job:
X-Powered-By: PHP/7.3.22
Content-type: text/html; charset=UTF-8
<br />
<b>Fatal error</b>: Uncaught Error: Class 'mysqli' not found in /home/example/public_html/new_facebook_csv/csv_generator1.php:180
Stack trace:
#0 {main}
thrown in <b>/home/example/public_html/new_facebook_csv/csv_generator1.php</b> on line <b>180</b><br />
I added the mysqli extension in the php.ini (extension=php_mysql.dll) but not luck with that.
Also check if mysqli is installed to my server with this commaned:
php -m|grep mysql
and that was my output
mysqli
mysqlnd
pdo_mysql
which means that mysql is installed to my server
try with curl like /usr/bin/curl example.com/new_facebook_csv/csv_generator1.php
or you can use wget wget -q -O- example.com/new_facebook_csv/csv_generator1.php
This is how I scheduled a cron job in cPanel through GoDaddy that runs the script and includes MYSQLI:
curl -s "https://yourwebsite.com/phpscript.php"
I tried many variations including cPanel's example on running a PHP script with a cron job. I tried php in front of the script path, and the same format you used above, as well as the suggested answers and I received the same errors. Just using curl -s and then the url of your php script will do the trick.

Adding php-curl in a Yocto build

I've been building a custom image using Yocto and wpewebkit to use within docker. I need to run a local web server with php, which I have accomplished no problem. I need to use curl within my project and I have apache2, php, modphp, and curl installed. All I need is php-curl but I cannot find how to install this. Does anyone know how or if I can do this? I've put in all my efforts on googling multiple phrases but I only find how to install php-curl using apt-get or yum, or I get links to the meta-* directories which I have access to within my build files anyways.
If it helps at all, this custom image was based on the image provided from the following project on GitHub: https://github.com/resin-io-playground/resin-wpe/tree/master/base-image . I already spoke to the developer and their knowledge of PHP is limited.
EDIT #1: Progress so far, had to update the curl.bb file to properly include libcurl-dev, and then worked with #pmod's suggestion to incorporate curl into the php build. Everything seemed to run okay until I received the following error:
| ../php-7.1.9/configure: line 386: test: please: integer expression expected
| configure: error: reinstall
| ../php-7.1.9/configure: line 275: return: please: numeric argument required
| ../php-7.1.9/configure: line 285: exit: please: numeric argument required
| NOTE: The following config.log files may provide further information.
| NOTE: /base-image/build/tmp-glibc/work/x86_64-linux/php-native/7.1.9-r0/build/config.log
| ERROR: configure failed
| WARNING: /base-image/build/tmp-glibc/work/x86_64-linux/php-native/7.1.9-r0/temp/run.do_configure.29590:1 exit 1 from 'exit 1'
| ERROR: Function failed: do_configure (log file is located at /base-image/build/tmp-glibc/work/x86_64-linux/php-native/7.1.9-r0/temp/log.do_configure.29590)
ERROR: Task (virtual:native:/base-image/build/../meta-openembedded/meta-oe/recipes-devtools/php/php_7.1.9.bb:do_configure) failed with exit code '1'
Edit #2: I've gone ahead and added everything I was missing. I'm getting the same error above, where the function do_configure during the php build is failing. I'm posting below what I ensured I have in my curl.bb file (I had to add this), what I have in my php.inc file, and the curl files located staging directory.
curl.bb:
PACKAGES =+ "lib${BPN} lib${BPN}-native lib${BPN}-dev lib${BPN}-dev-native"
FILES_lib${BPN} = "${libdir}/lib*.so.*"
FILES_lib${BPN}-dev = "${includedir} \
${libdir}/lib*.so \
${libdir}/lib*.a \
${libdir}/lib*.la \
${libdir}/pkgconfig \
${datadir}/aclocal \
${bindir}/*-config"
php.inc:
EXTRA_OECONF_append = " --with-curl=${libdir}/.."
DEPENDS_${PN} += "libcurl-dev libcurl-dev-native"
PHP Staging Directory:
/installeddeps/curl-native
/installeddeps/curl-native.b77f37db31cf2391919a12c5c9774bff
/installeddeps/curl-native.complete
/usr/share/aclocal/libcurl.m4
/usr/bin/curl-config
/usr/bin/crossscripts/curl-config
/usr/bin/curl
/usr/lib/pkgconfig/libcurl.pc
/usr/lib/libcurl.so
/usr/lib/libcurl.a
/usr/lib/libcurl.so.4.4.0
/usr/lib/libcurl.so.4
/usr/include/curl
/usr/include/curl/curl.h
/usr/include/curl/curlrules.h
/usr/include/curl/curlbuild.h
/usr/include/curl/curlver.h
/usr/include/curl/easy.h
According to php manual, PHP/cURL feature is enabled at php configuration stage with --with-curl=[dir] option.
I checked the main recipe include file php.inc and couldn't find such configuration option. So, try creating in your working layer php_%.bbappend file with (or add this directly to the bottom of php.inc to test):
EXTRA_OECONF_append_class-target = " --with-curl=${STAGING_LIBDIR}/.."
DEPENDS_append_class-target = " curl"
Note, that curl package among DEPENDS because it is needed at build stage.
I don't think you need to update curl recipe with PACKAGES and FILES, pkg_config should take care

Datastax - Cassandra php-driver: Uncaught Cassandra\Exception\LogicException:

I'm on Ubuntu right now trying to connect to Cassandra with PHP. I have installed Datastax php-driver and all of its dependencies but i get this error when trying to run a testfile:
PHP Fatal error: Uncaught Cassandra\Exception\LogicException: Not implemented in /home/philip/Documents/test.php:3
Stack trace:
#0 /home/user/Documents/test.php(3): Cassandra\Cluster\Builder->build()
#1 {main}
thrown in /home/user/Documents/test.php on line 3
The code looks like this:
<?php
// Connect to the cluster and keyspace "killrvideo"
$cluster = Cassandra::cluster()->build();
$keyspace = 'killrvideo';
$session = $cluster->connect($keyspace);
I manage to connect to cassandra using the shell cqlsh and i can see the table i have setup. The php code doesnt seem to work though. Any ideas why?
We have the same issue. This works for me.
Thanks a lot #AlexandruCircus!
git clone https://github.com/datastax/php-driver.git
git reset --hard f50c93da3ea73ad8fcf8b181d0313d437e559256
cd php-driver/ext
./install.sh
well this is not very clear for me but you call this from the same server ?
witch strategy you use ? simples or network topology ?
but something that I'm sure will work with this
$cluster = Cassandra::cluster()->withContactPoints('127.0.0.1')->build();
// or if you call from a different server just replace the ip

Uncaught exception 'ImagickException' with message 'Unable to read the file'

I'm running PHP 5.4.30 with imagick module 3.1.0RC2. phpinfo() does show the module as loaded and phpinfo() shows PDF as a supported format.
When running:
$image = new Imagick();
$image->readImage('./test.pdf');
I receive:
Fatal error: Uncaught exception 'ImagickException' with message 'Unable to read the file: ./test.pdf'
When I change the image type to be a JPEG and read './test.jpg' No errors are returned. Permissions have been check and different PDFs have been tested. I have inserted the absolute path to the image with an exception still being thrown. The images are for this example placed in the same directory alongside the script.
What am I missing?
I had the exact same problem. As command line it worked perfectly fine: convert book.pdf book.jpeg but pdf didn't seem to work. If not, you may want to install GhostScript
I found the solution on the issue 588918. You simply have to create a symlink of gs (the GhostScript library) in usr/bin. Basically, If you do something like
sudo ln -s /usr/local/bin/gs-noX11 /usr/bin/gs
and restart your server it should work. At least it did for me.

Categories