r/PHPhelp • u/RolexV0 • Jul 11 '25
How can I use mPDF in a PHP project without Composer ?
I'm working on a PHP project where I can't use Composer (due to shared hosting restrictions). I want to generate PDFs using mPDF, but I'm having trouble setting it up manually.
Here’s what I tried:
- Downloaded the ZIP of mPDF from GitHub
- Tried including
mpdf.phpdirectly, but it gave errors related to missing dependencies - Not sure how to set up the autoloader or required classes manually
Has anyone successfully used mPDF without Composer? If so, how did you structure your project and which files did you include?
4
Upvotes
17
u/DevelopmentScary3844 Jul 11 '25 edited Jul 11 '25
https://packagist.org/packages/mpdf/mpdf
I think it will be a lot of work to do this manually without composer because of the dependecy-tree so why not:
Create a new project on your local machine.
Init with composer.
Install deps with composer
run "composer dump-autoload"
Copy vendor directory to server and add to index.php
Edit: It seems you develop on your shared hosting? Why not develop on your machine and upload the result to the server when you are done?
If this sounds good to you check out ddev (https://ddev.readthedocs.io/)
5
u/allen_jb Jul 11 '25 edited Jul 11 '25
You can still use Composer for dependency management even if you can't run it on your production hosting.
Run Composer locally to install dependencies and commit the
vendor/directory to version control. Then deploy thevendor/directory to your production hosting along with the rest of the site.You don't actually need Composer itself on the production hosting - all the files needed at run-time are generated by Composer in the
vendor/directory.Where packages require extensions that you cannot locally install (eg. you're running Composer on Windows but the package requires an extension such as ext-pcntl that only runs on Linux) you can use the
--ignore-platform-reqsoption. Alternatively use WSL or Docker to get a Linux environment.To use libraries without using Composer at all your most common problem is going to be autoloading. This mechanism allows PHP code to find files which contain class and other entity definitions at run-time without having to explicitly
require/includethe files.It is (generally) possible to configure autoloading yourself by looking at the autoload configuration in the composer.json file for the package, then setting up the same setup. Most packages use the well-defined PSR-4 autoloader.
Composer usually handles this for you - it's what you run when you include the
vendor/autoload.phpscript. There are additional files in thevendor/composerdirectory which include the actual autoloading code and configuration. These will appear somewhat more complicated than absolutely necessary because Composer includes several autoloading mechanisms.For more on autoloading and the mechanisms Composer provides see https://www.php.net/manual/en/language.oop5.autoload.php and https://getcomposer.org/doc/04-schema.md#autoload
1
u/Big_Tadpole7174 Jul 11 '25
For the autoloading you could perhaps write your own autoloader.
But as others already said, Mpdf has many dependencies. It should really be used with composer. I don't see why you can't use composer in a shared environment. You can just upload the vendor folder. Perhaps can you can use an older version that doesn't require composer, but i advise against it.
1
u/[deleted] Jul 11 '25
mPDF requires a html object interpreter which composer loads. TCPDF doesn't but the html and graphics handling takes up a of memory because its running an interpreter like program with php. Which isn't efficient. FPDF works nice, but it doesn't have an html support so no html and css. But some will use HTML2FPDF with FPDF which works faster than all the rest with html support.
1
u/BokuNoMaxi Jul 11 '25
A more advanced approach to use composer on a remote production server is to run your composer install in the pipeline and rsync your changes directly to the remote server. This way you aren't forced to commit and bloat your git repository with the vendor folder.
This is our way to deploy projects so we are hoster independent, the only thing we need is a SSH connection, and the project sets up by itself. Only thing I have to do manually is editing the .env file.
1
u/GrouchyInformation88 Jul 11 '25
I used a website to download packages and dependencies, years ago, before I realized I could use composer locally and upload to a shared host that didn’t allow ssh (back in the days when I made all changes on the live server 😅). But I can’t remember what it was called.
1
u/binaryflow Jul 12 '25
I develop locally and push up the vendor folder to the production server. No composer install (on Prod) required.
1
u/CyberJack77 Jul 11 '25
mPDF is meant to be installed with composer, because it has dependencies that need to be installed to.
If composer is not available you need to register your own autoloader and place ALL the dependencies (including dependencies of dependencies) somewhere in your project.
But why is composer not possible? because I don't get the relation with shared hosting.
0
u/[deleted] Jul 23 '25
[deleted] — view deleted comment