PHP: Merge PDFs without composer? - php

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.

Related

Merge PDF files with PHP into single file

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

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.

Error on PHP Excel 1.8 Call to undefined function getNameFromNumber()

i just wanted to import with PHP Excel 1.8 class and then i got this error Fatal error: Call to undefined function getNameFromNumber() in includes/PHPExcel-1.8/Classes/PHPExcel/Writer/HTML.php on line 1617.
Can someone image what here the problem or better what i can do?
Thanks.
Olaf
I'd download a fresh source from https://github.com/PHPOffice/PHPExcel/releases/tag/1.8.1
or the latest files from github # https://github.com/PHPOffice/PHPExcel
as neither of those include that line/function you reference.

Difficulty setting up ExcelPHP

I am attempting to use require to load the ExcelPHP API to manipulate CSV spreadsheets. I am using EasyPHP 12.1 with PHP 5.4.6, Apache 2.4.2, MySQL 5.5.27 and Xdebug 2.2.1. I am on Windows 7.
When I attempt to run the following code:
require_once('classes/PHPexcel/Autoloader.php');
$mySheet = new PHPExcel();
I get a fatal error:
Notice: Use of undefined constant PHPEXCEL_ROOT - assumed 'PHPEXCEL_ROOT' in C:\Program Files\EasyPHP-12.0\www\Classes\Classes\PHPExcel\Autoloader.php on line 73
Fatal error: Class 'PHPExcel_Shared_ZipStreamWrapper' not found in C:\Program Files\EasyPHP-12.0\www\Classes\Classes\PHPExcel\Autoloader.php on line 31
I have tried commenting out line 31 in the Autoloader.php file, and that prevents the the fatal error but then the API won't work and PHP doesn't recognize the PHPExcel class.
Can someone please help me figure out how to get my library to work? Thanks in advance!
Load the PHPExcel class in your require once, rather than the Autoloader (as Seth) has said. Case-sensitivity is also a potential problem, because your scripts won't transfer without error unless you get it right:
require_once('classes/PHPexcel/Autoloader.php');
should be
require_once('classes/PHPExcel.php');
and the library is PHPExcel, not ExcelPHP as you call it in your subject line
Instead of your current require statement, you should call
require_once('classes/PHPExcel.php');
That file defines the constant PHPEXCEL_ROOT that you are missing, and then calls require_once() on the autoloader file. It also loads the main class PHPExcel that the whole library uses.
This may have come a bit too late as PHPExcel has pivoted to PHPSpreadsheet but if you're still using PHPExcel and you want to use the autoloader, all you have to do is define the PHPExcel root before requiring autoloader.
define('PHPEXCEL_ROOT', dirname(__FILE__).'/');
i use it composer in my custom mvc framework
require_once ROOT . '/vendor/autoload.php';
PHPExcel_Autoloader::Register();

Fatal error for PHPExcel classes while configuring it with WAMP (PHP 5.3.5)

While working with PHPExcel in my application I got following errors
In PHPExcel/Autoloader.php file for line “PHPExcel_Shared_ZipStreamWrapper::register();”,
Fatal error: require() [function.require]: Failed opening required '/wamp/www/xxxx/site//common/class/PHPExcel_Shared_ZipStreamWrapper.class.php' (include_path='C:\wamp\www\xxxx\site\common\html_purifier;.;C:\php\pear') in C:\wamp\www\xxxx\site\config\bootstrap.php on line 27
and for line “PHPExcel_Shared_String::buildCharacterSets();”
Fatal error: require() [function.require]: Failed opening required '/wamp/www/xxxx/site//common/class/PHPExcel_Shared_String.class.php' (include_path='C:\wamp\www\xxxx\site\common\html_purifier;.;C:\php\pear') in C:\wamp\www\xxxx\site\config\bootstrap.php on line 27
I'm using wamp with php 5.3.5. and zip xml and gd2 extensions are enable on my local machine.
Please help me for this.
Thanks.
The reference to C:\wamp\www\xxxx\site\config\bootstrap.php suggests you're working with some kind of framework: does it have an autoloader that could be conflicting with or overriding PHPExcel's autoloader?
The reference to
'/wamp/www/xxxx/site//common/class/PHPExcel_Shared_ZipStreamWrapper.class.php'
seems to confirm this.
as something is changing the filename to include ".class" as part of the filename, and it's ignoring PHPExcel's own autoloader that splits a class name to match its directory structure
If you're using a standard framework such as ZF or Symfony, then you should register PHPExcel with that framework. If it's a homebrew, make sure your autoloader is SPL registered
You seem to be having double slashes in your path:
/wamp/www/xxxx/site*//*common
So it's probably a path issue and the autoloader isn't able to load the file because the path is wrong.

Categories