0

I have a project that I need to convert HTML-PAGE into PDF-FILE.

I have found this repo: https://github.com/mpdf/mpdf. I have cloned it into my computer, and I can't find out what should I do.

in the tutrial they give this code below:

<?php

require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->Output();

?>

I can't find the directory called "vendor".

Thx for your help.

2
  • how are you doing your local development? are you using a mac or windows machine? are you using mamp/wamp for your local server? Commented Dec 25, 2016 at 17:43
  • hey, i'm using wamp on windows Commented Dec 25, 2016 at 17:49

2 Answers 2

1

Preparation

In the interest of complete clarity, I'm going to take you through all of the steps I use to make this work on Windows with Wamp.

When you install Wamp on windows, the Apache document root gets set to c:/wamp64/www or something similar by default. This is a good thing because you can then set up virtual hosts on your Windows machine which will allow you to visit actual urls in your browser (like dev.mpdftest.com for example) instead of having to visit localhost/my-project-directory.

So the first thing to do is create a folder in the www directory, calling it whatever you want (I called mine mpdf_test).

Next, set up a virtual host for that project. To do that:

  1. Left-click the Wamp icon in your system tray.
  2. Under the 'Apache' menu item, click the 'httpd-vhosts.conf' file - this will open it in your text editor.
  3. If you have never added a virtual host, there will only be one entry in this file (for localhost) that looks like this:

    enter image description here

  4. Copy that entire entry and paste it underneath, making appropriate changes to point at your new project directory. Here's how mine looks (you can make yours the same, just change the project folder name to whatever you called yours):

    enter image description here

  5. Save this file.

  6. Open 'C:\Windows\System32\drivers\etc\hosts' in your text editor.

  7. Create an entry for your new site directly below the entry for localhost, using the ServerName from the virtual hosts entry, like this:

    enter image description here

  8. Save this file.

  9. Restart your Wamp services by left-clicking the Wamp icon in your system tray and clicking 'Restart All Services'.

  10. You should now be able to visit dev.yourprojectname.com in your browser. You can add a simple index.php file to your project that just prints "Hello World" to test this.

Installing Composer

To install composer on Windows, you should be able to just download and execute (double-click on) this file. Just accept all the defaults.

Installing Mpdf In Your Project

  1. Switch into your project directory in a terminal window (command prompt on Windows) and execute the following command:

    • composer require mpdf/mpdf
  2. This will add the vendor directory (which includes mpdf) to your project. Your project structure will look like this:

    enter image description here

  3. You need to change the permissions for three of the sub-directories. If you don't already have it, install Git for Windows.

  4. One of the programs that will be installed is called Git BASH. It gives you the ability to set file permissions the same way you would in Linux or on a Mac. Run this program and cd into your project directory:

    • cd /c/wamp64/www/your_project_directory/
  5. Execute the commands below:

    • chmod 775 vendor/mpdf/mpdf/ttfontdata
    • chmod 775 vendor/mpdf/mpdf/tmp
    • chmod 775 vendor/mpdf/mpdf/graph_cache

Testing Your Mpdf Installation

  1. If you have not done so already, create an index.php file in your project directory with the following code:

    enter image description here

  2. Save that file and visit your site in your browser (dev.yoursitename.com). If you have done everything correctly, you should be immediately redirected to a PDF. The sole contents of that PDF will be the h1 you defined in your code. Here's mine:

    enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Hey, I have done everything you said and its still give the same error
@itzikb - I added steps 3, 4, and 5 to the 'Installing Mpdf In Your Project' section (this was the only thing I forgot to add when I originally posted my answer). Follow those steps and make sure the php code in your index.php file matches what I show in step 1 of my 'Testing Your Mpdf Installation' section. After this, there's no reason why you shouldn't have success if you've followed my instructions exactly.
Thanks you, that work. but its dont detect the language Hebrew you know how to fix it?
Hmmm ... I have not used this library for that purpose before, but I believe it has support for Hebrew. You will probably find the information you need under the 'Fonts & Languages' section of the mPDF manual: mpdf.github.io. I'm glad you got things working! Good luck with the language issue.
0

You have to install the library via composer, as the documentation says:

Official installation method is via composer and its packagist package    mpdf/mpdf.
  composer require mpdf/mpdf

If composer isn't on your system, install it:

https://getcomposer.org/download/

4 Comments

OK, I have download composer and used this command $ composer require mpdf/mpdf what next? where is the files?
The right command to install the library is composer require mpdf/mpdf
without $. it will create the vendor folder.
ok, i found the dir vendor and run the php code, and I get Fatal error: Class 'Mpdf\Mpdf' not found in D:\wamp64\www\mpdf\index.php this error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.