Merge PDF files with PHP into single file - php

If i have some PDF files, each has one page and i would like to merge all files into on single file using PHP
I have read dozens of questions here about same but most of all are out of date or no longer working since for PHP version >= 7.0
One of the solutions was to use PDFMerger and here is my code
require_once ('PDFMerger.php');
use PDFMerger\PDFMerger;
$pdf = new PDFMerger;
$pdf->addPDF('books/1.pdf');
$pdf->addPDF('books/2.pdf');
$pdf->merge('download','books/merged.pdf');
but if simply gives blank page and no file is created and same to this library PDFMerger
UPDATE1! wihtout namespace use PDFMerger\PDFMerger; iam getting this error
Fatal error: Uncaught Error: Class "PDFMerger" not found in mypath\sample.php:4 Stack trace: #0 {main} thrown in mypath\sample.php on line 4
so is there any way that is still working to merger PDF files without have to care how such file are created.
NOTE! I can not use shell_exec since files are on shared hosting

After trying a lot of time , i decide to shift back to a lower PHP version 7.1 rather than 8.0 and everything goes fine though

Related

OCR wrapper for php

I am using this for ocr. This is wrapper for Tesseract ocr(I previosly installed Tesseract itself).
At first i dowloaded it via composer and followed examples in repo and also several posts on SO itself. And all can show is in network tab failed to load response data.
My alternate approaches is that i tried downloading repo itself, then tried to call it from my index.php which for test purposes is situated in same folder where class TesseractOCR is. I tried with images in repo and also tried with black letters on white background images with simple text.
This SO post looks promising, but i'm unsure where OP's file with example code is residing...
use thiagoalessio\TesseractOCR\TesseractOCR;
//or//require "TesseractOCR.php";//if it's in the same dir as test.php
$content = new TesseractOCR('text.png');
$text = $content->run();
echo $text;
Did i miss something obvious? Any help is appreciated.
EDIT1: I tried using in win powershell cli. By putting text.png in directory where tesseract is installed, then calling shell with administrator privileges, subsequently typing in it tesseract text.png output which creates output.txt in same directory with recognized text from that image.
So tesseract its working, my implementation with php wrapper is not.
EDIT2:
Forgot to add, page itself shows:
This page isn’t working
localhost is currently unable to handle this request.
HTTP ERROR 500
Not sure why it happens.
Edit3:
My code:
try{
//use thiagoalessio\TesseractOCR\TesseractOCR;
require "./vendor/thiagoalessio/tesseract_ocr/src/TesseractOCR.php";
echo $temp;//It's value is set in TesseractOCR.php
$content = new TesseractOCR('text1.png');
$text = $content->run();
echo $text;
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
Value set in $temp variable is visible through state file path, so why TesseractOCRclass itself isn't?
Edit4:
Even if i put absolute path to TesseractOCR.php which holds class, in include statement, it doesn't work.
It throws this error:
Fatal error: Uncaught Error: Class 'TesseractOCR' not found in C:\xampp\htdocs\myocr\index.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\myocr\index.php on line 10
This is TesseractOCR.//echoed text from file that holds TesseractOCR class.
Inclusion path:
include ("C:/xampp/htdocs/myocr/vendor/thiagoalessio/tesseract_ocr/src/TesseractOCR.php");
If i use(which is suggested in repo readme, use thiagoalessio\TesseractOCR\TesseractOCR;, then it throws:
Fatal error: Uncaught Error: Class 'thiagoalessio\TesseractOCR\TesseractOCR' not found in C:\xampp\htdocs\myocr\index.php:10 Stack trace: #0 {main} thrown in C:\xampp\htdocs\myocr\index.php on line 10
My question is: How it hits test message, but won't hit TesseractOCR class?
EDIT5:
If i require_once "./vendor/autoload.php"; , it throws:
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:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\xampp\php;C:\ProgramData\ComposerSetup\bin;C:\Users\Eddie\AppData\Local\Microsoft\WindowsApps;;C:\Users\Eddie\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\Eddie\AppData\Roaming\npm;C:\Users\Eddie\AppData\Roaming\Composer\vendor\bin;C:\Program Files\heroku\bin in C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php:48 Stack trace: #0 C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\TesseractOCR.php(26): thiagoalessio\TesseractOCR\FriendlyErrors::checkTesseractPresence('tesseract') #1 C:\xampp\ht in C:\xampp\htdocs\myocr\vendor\thiagoalessio\tesseract_ocr\src\FriendlyErrors.php on line 48
Btw, i added its patch to env variable:
I solved it!. My problem occurs that i didn't know about autoloaders in php.
Link that helped me is this.
My project structure is this:
Created project folder myocr.
After previous, downloaded latest stable version of Tesseract and installed it.
Depending on your system, you may be required to add value to your system env variable. You need to do that here
then
then
then
I'm assuming it self explanatory with images provided.
Next is getting TesseractOCR via composer:
composer require thiagoalessio/tesseract_ocr
Finally, before using code sample in repo, you need to call autoloader.
Index.php:
require_once('./vendor/autoload.php');//<-This!
use thiagoalessio\TesseractOCR\TesseractOCR;
$content = new TesseractOCR('text1.png');
$text = $content->run();
echo $text;
This worked for me. Crucial thing to look after is directory structure.
localhost > myocr > index.php with its code, and after using composer you'll get vendor dir. and it's content. Image path in new TesseractOCR('text1.png'); is directory where both index.php and image are located.

PHP: Merge PDFs without composer?

I am trying to merge PDF documents with PHP and have searched a lot and tried several solutions. What seems to be the most common is using the FPDI library and then a small class like PDF-Merger.
But the problem is that that library is dependent of other libraries and those libraries of others and so on. And the installation examples is mostly how to install with composer. But I wanna be able to merge pdf's for a site that's not using composer. And it seems to be really hard to achieve that.
Due to the manual, the only thing to do if I don't wanna use the composer is writing this to your code as the installation of the library:
require_once('path/to/src/autoload.php');
Of course, with the correct path. But no, that didn't work for me. Couldn't find the fpdi class. And if you look at the manual again, you see this warning:
Don't forget to install FPDF, TCPDF or tFPDF before!
So I manually downloaded those three libraries and also those were hard to install without composer. And are still stucked in this mess. I have made some success by changing in the PDFMerger.php file from use fpdi\FPDI; to \setasign\Fpdi\Fpdi. So now is the Fpdi class found. But still get errors.
Let me show you the current situation. Here is my current code:
<?php
require_once get_stylesheet_directory().'/classes/fpdf/fpdf.php';
require_once get_stylesheet_directory().'/classes/tfpdf/tfpdf.php';
require_once get_stylesheet_directory().'/classes/tcpdf/src/Output.php';
require_once get_stylesheet_directory().'/classes/tcpdf/src/MetaInfo.php';
require_once get_stylesheet_directory().'/classes/tcpdf/src/ClassObjects.php';
require_once get_stylesheet_directory().'/classes/tcpdf/src/Tcpdf.php';
require_once get_stylesheet_directory().'/classes/FPDI/src/autoload.php';
require_once get_stylesheet_directory().'/classes/FPDI/src/Fpdi.php';
require_once get_stylesheet_directory().'/classes/PDFMerger/PDFMerger.php';
$pdf = new \Clegginabox\PDFMerger\PDFMerger;
$pdf->addPDF('path/to/file1.pdf', 'all', 'P');
$pdf->addPDF('path/to/file2.pdf', 'all', 'P');
$pdf->merge();
?>
And here are the error messages in the browser:
Notice: Undefined index: w in
/home/public_html/wp-content/themes/my-theme/classes/PDFMerger/PDFMerger.php
on line 85
Notice: Undefined index: h in
/home/public_html/wp-content/themes/my-theme/classes/PDFMerger/PDFMerger.php
on line 85
Notice: Undefined index: w in
/home/public_html/wp-content/themes/my-theme/classes/PDFMerger/PDFMerger.php
on line 85
Notice: Undefined index: h in
/home/public_html/wp-content/themes/my-theme/classes/PDFMerger/PDFMerger.php
on line 85
Fatal error: Uncaught Exception: FPDF error: Some data has already
been output, can't send PDF file in
/home/public_html/wp-content/themes/my-theme/classes/fpdf/fpdf.php:271
Stack trace: #0
/home/public_html/wp-content/themes/my-theme/classes/fpdf/fpdf.php(1063):
FPDF->Error('Some data has a...') #1
/home/public_html/wp-content/themes/my-theme/classes/fpdf/fpdf.php(999):
FPDF->_checkoutput() #2
/home/public_html/wp-content/themes/my-theme/classes/PDFMerger/PDFMerger.php(107): FPDF->Output('I', 'newfile.pdf') #3
/home/public_html/wp-content/themes/my-theme/functions.php(269):
Clegginabox\PDFMerger\PDFMerger->merge() #4
/home/public_html/wp-includes/class-wp-hook.php(286):
cdon_woocommerce_loaded('') #5
/home/public_html/wp-includes/cla
in
/home/public_html/wp-content/themes/my-theme/classes/fpdf/fpdf.php
on line 271
And you can see the PDFMerger.php here and line 85 is this:
$fpdi->AddPage($fileorientation, array($size['w'], $size['h']));
I mean, I think it's a little exaggerated to have all these dependencies to just merge pdfs. Is this really needed? And if so, what is wrong with my installation?
You don't need composer to use PDFMerger.
Just clone the original repository from https://github.com/myokyawhtun/PDFMerger and move PDFMerge.php and the tcpdf directory into your project.
Then you can do the following:
include 'PDFMerger.php';
$pdf = new \PDFMerger\PDFMerger;
The mentioned merger class is outdated and relies on legacy versions. Just use the up to date native FPDI code which you can find here. This demo shows you how you can use FPDI to concatenate several PDF documents.
You should also notice the or in the info text of the installation instructions:
Don't forget to install FPDF, TCPDF or tFPDF before!
For sure you only need to install one of those classes. If you want to concatenated PDF pages, you should go with FPDF.

Unable to read the "../.dev.env" environment file in Dotenv.php:symfony

I want To run a symfony project in my computer but I get all times errors , and now I get this error and i didn't find any solution for my problem
Problem Failed to parse output as xml: Error on line 2: Content is not
allowed in prolog.. Command C:\xampp\php\php.exe
C:\xampp\htdocs\buzzaka\bin\console list --format=xml Output
Fatal error: Uncaught
Symfony\Component\Dotenv\Exception\PathException: Unable to read the
"C:\xampp\htdocs\buzzaka\bin/../.dev.env" environment file. in
C:\xampp\htdocs\buzzaka\vendor\symfony\dotenv\Dotenv.php:466 Stack
trace:
0 C:\xampp\htdocs\buzzaka\vendor\symfony\dotenv\Dotenv.php(51): Symfony\Component\Dotenv\Dotenv->doLoad(false, Array)
1 C:\xampp\htdocs\buzzaka\bin\console(38): Symfony\Component\Dotenv\Dotenv->load('C:\xampp\htdocs...')
2 {main} thrown in C:\xampp\htdocs\buzzaka\vendor\symfony\dotenv\Dotenv.php on line 466
If u move your file from the folder where it was created symfony will keep looking for it on this folder, i've went on autoload_runtime and changed on $runtime variable where the project is, just worked for me, lets see on the last composer dump what just happens haha
The error is quite explicit, do you have a file C:\xampp\htdocs\buzzaka\bin/../.dev.env ?
Well, you need to create one. As announced a little while ago, Symfony will now require a .env file per environment. As you are running your command in the dev environment, you need to create a .dev.env file.

Invalid Size error - Phpseclib - SFTP connection

I'm trying to use the SFTP connection in PHP application, SFTP connection throws invalid size error, please anyone help me out to solve this.
$sftp = Yii::app()->phpseclib->createSFTP($ftp_server,8822);
if (!$sftp->login($ftp_username, $ftp_password)) { // Login failed
}
Error throws as:
2018/01/30 11:42:11 [error] [php] Invalid size (D:\xampp\htdocs\viahttps\protected\extensions\phpseclib\classes\Net\SSH2.php:2607)
Stack trace:
#0 D:\xampp\htdocs\viahttps\protected\extensions\phpseclib\classes\Net\SSH2.php(1720): Net_SFTP->_login_helper()
#1 unknown(0): Net_SFTP->_login()
#2 D:\xampp\htdocs\viahttps\protected\extensions\phpseclib\classes\Net\SFTP.php(405): call_user_func_array()
The wrapper you are using is for 0.3.6 whereas this issue of invalid size has been there since 0.3.5 and was fixed in 0.3.7 after releasing when people bumped into the same error while upgrading from 0.3.6. See this ISSUE
Currently phpseclib is on version 2.0.1 you might need to look if the wrapper you are using has the latest phpseclib source files or otherwise.
You need to look for some other extension doing the same thing.
Override the phpseclib files used by the wrapper from this URL and copy all files and folders inside the phpseclib folder and paste them in the protected/extensions/phpseclib/classes and replace duplicate or override if the file exists.
Note: there might be a possibility that the wrapper throws an error or exception somewhere as the version has a huge gap drastically so
this might involve fixing the bugs that appear after upgrading the
source classes, or maybe not.

connecting PHP to MSSQL error

I'm currently trying to connect to a MSSQL database, but from what i've read so far, there are actually extensions and files that i need to download from microsoft itself which i've went and download it here.
https://www.microsoft.com/en-us/download/details.aspx?id=20098
The error that i'm receiving currently receiving is the following.
Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in C:\xampp\htdocs\index.php:5 Stack trace: #0 {main} thrown in C:\xampp\htdocs\index.php on line 5
As far as i understand, i know that i'm supposed to append some extensions into the php.ini located in my xampp\php\php.ini file. However, even after appending the file from microsoft, i still can't seem to get connected.
I heard some people mentioned that the php version that i'm using which is currently PHP Version 7.1.1 has some issues connecting back to MSSQL databases but i'm not entirely sure.
Would someone be able to point me to right direction? i've tried looking up and down appending different files into the extension but i still can't seem to do it.
Thank you very much in advance.
edit:
PHP 7 on IIS: Call_user_function could not be located
i've also visited this link, however i'm unsure whether if this is related to the issue I have as i'm on 7.1

Categories