Peter Westerlund
Peter Westerlund

Reputation: 749

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?

Upvotes: 1

Views: 11436

Answers (2)

Jan Slabon
Jan Slabon

Reputation: 5058

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.

Upvotes: 1

Chin Leung
Chin Leung

Reputation: 14921

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;

Upvotes: 4

Related Questions