0

I am trying to convert data from php to pdf using mpdf library .my code showing following error. Warning: include(mpdf/vendor/autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\sms\admin\test.php on line 2

Warning: include(): Failed opening 'mpdf/vendor/autoload.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\sms\admin\test.php on line 2

Fatal error: Uncaught Error: Class 'mpdf\mpdf' not found in C:\xampp\htdocs\sms\admin\test.php:3 Stack trace: #0 {main} thrown in C:\xampp\htdocs\sms\admin\test.php on line 3

my code is

 <?php
include('../mpdf/vendor/autoload.php');
$mpdf = new \mpdf\mpdf();


$mpdf->output();



?>
roopa
  • 75
  • 7
  • 1
    Does this answer your question? [PHP - Failed to open stream : No such file or directory](https://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory) – Alon Eitan Nov 05 '20 at 09:48
  • This is a pretty much self-explanatory - The path you provided is WRONG – Alon Eitan Nov 05 '20 at 09:49
  • @AlonEitan but path is correct my web page location is C:\xampp\htdocs\sms\admin\test.php and the autoload.php file location is C:\xampp\htdocs\sms\mpdf\vendor\autoload.php – roopa Nov 05 '20 at 09:51
  • You are using XAMPP (Windows), so you Path is incorrect. Use DIRECTORY_SEPARATOR constant. Your current path is for unix systems. `include('..'.DIRECTORY_SEPARATOR .'mpdf'.DIRECTORY_SEPARATOR .'vendor'.DIRECTORY_SEPARATOR .'autoload.php');` – shelly Nov 05 '20 at 09:59
  • Very strange. Does `include( dirname( __FILE__ ) . '/../mpdf/vendor/autoload.php');` change anything? – Alon Eitan Nov 05 '20 at 09:59
  • @shelly1337 AFAIK you CAN use `/` as a directory separator in windows (It works fine on my machine), although it's a good practice to use the `DIRECTORY_SEPARATOR` constant – Alon Eitan Nov 05 '20 at 10:00
  • @AlonEitan Could be. Last time i worked on windows i didnt worked for me tho. Maybe with wsl. But i could be wrong. – shelly Nov 05 '20 at 10:02
  • @shelly1337 On the other hand, [here](https://dev.to/c33s/always-use--as-directory-seperator-in-php-43l7) it say to _Always use `/` as directory separator in php_ (**"windows and php on windows are capable of opening files with the path foo/bar\example/test.txt"**) `:P` I always adding to the root file `define( 'DS', DIRECTORY_SEPARATOR );` and then use a shorter constant - `DS` for more clarity – Alon Eitan Nov 05 '20 at 10:06

1 Answers1

0

hello @roopa please try this:

// Require composer autoload
require_once __DIR__ . '/vendor/autoload.php';
// Create an instance of the class:
$mpdf = new \Mpdf\Mpdf();

// Write some HTML code:
$mpdf->WriteHTML('Hello World');

// Output a PDF file directly to the browser
$mpdf->Output();

If your Root Folder is "mpdf", you don't need to create a new path for that.
include('../mpdf/vendor/autoload.php');
But as you describe on this line, your Root folder should be two folder before vendor folder.

The include statement includes and evaluates the specified file.
include('../mpdf/vendor/autoload.php');
In any part of mPDF documentation does not describes or comment the usage of this function

Please follow the documentation, it's better! The documentation is easy to read.
https://mpdf.github.io/getting-started/creating-your-first-file.html

In the worst case, make a new installation and all steps if you follow the documentation will be more clear for you!
Good luck!

Paulo do Porto
  • 606
  • 1
  • 9
  • 24
  • I always read that we must reqire vendor/autoload.php but there is no such a file inside MPDF folder! Where are you guys getting it from??! – showtime Jun 20 '22 at 14:15