问题
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.
回答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:
- Left-click the Wamp icon in your system tray.
- Under the 'Apache' menu item, click the 'httpd-vhosts.conf' file - this will open it in your text editor.
If you have never added a virtual host, there will only be one entry in this file (for localhost) that looks like this:
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):
Save this file.
Open 'C:\Windows\System32\drivers\etc\hosts' in your text editor.
Create an entry for your new site directly below the entry for localhost, using the ServerName from the virtual hosts entry, like this:
Save this file.
Restart your Wamp services by left-clicking the Wamp icon in your system tray and clicking 'Restart All Services'.
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
Switch into your project directory in a terminal window (command prompt on Windows) and execute the following command:
- composer require mpdf/mpdf
This will add the vendor directory (which includes mpdf) to your project. Your project structure will look like this:
You need to change the permissions for three of the sub-directories. If you don't already have it, install Git for Windows.
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/
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
If you have not done so already, create an index.php file in your project directory with the following code:
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:
回答2:
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/
来源:https://stackoverflow.com/questions/41322746/convert-html-page-into-pdf-using-php