使用php将html页面转换为pdf [英] Convert html page into pdf using php

查看:969
本文介绍了使用php将html页面转换为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,需要将HTML-PAGE转换为PDF-FILE.

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

我找到了这个仓库: https://github.com/mpdf/mpdf . 我已将其克隆到计算机中,但无法确定该怎么办.

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();

?>

我找不到名为"vendor"的目录.

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

感谢您的帮助.

推荐答案

准备

Preparation

为了完全清晰起见,我将带您完成在带Wamp的Windows上实现此工作的所有步骤.

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.

在Windows上安装Wamp时,Apache文档根目录默认设置为c:/wamp64/www或类似的名称.这是一件好事,因为您可以在Windows机器上设置虚拟主机,从而允许您在浏览器中访问实际的URL(例如dev.mpdftest.com),而不必访问localhost/my-project-directory

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.

因此,要做的第一件事是在www目录中创建一个文件夹,随时随地调用它(我称我为mpdf_test).

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. 左键单击系统任务栏中的Wamp图标.
  2. 在"Apache"菜单项下,单击"httpd-vhosts.conf"文件-这将在您的文本编辑器中将其打开.
  3. 如果您从未添加虚拟主机,则该文件(对于localhost)将只有一个如下所示的条目:

  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:

复制整个条目并将其粘贴在下面,进行适当的更改以指向您的新项目目录.这是我的外观(您可以将其命名为相同,只需将项目文件夹名称更改为您所称的名称即可):

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):

保存此文件.

在文本编辑器中打开"C:\ Windows \ System32 \ drivers \ etc \ hosts".

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

使用虚拟主机条目中的ServerName在本地主机条目的正下方创建一个新站点条目,如下所示:

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

保存此文件.

通过左键单击系统任务栏中的Wamp图标并单击重新启动所有服务"来重新启动Wamp服务.

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

您现在应该可以在浏览器中访问dev.yourprojectname.com.您可以在项目中添加一个简单的index.php文件,该文件仅显示"Hello World"即可进行测试.

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.

正在安装Composer

Installing Composer

要在Windows上安装作曲家,您应该只需下载并执行(双击)该文件.只需接受所有默认设置即可.

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

在项目中安装Mpdf

Installing Mpdf In Your Project

  1. 在终端窗口(在Windows上为命令提示符)中切换到项目目录,然后执行以下命令:

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

  • 撰写者需要mpdf/mpdf

这会将供应商目录(包括mpdf)添加到您的项目中.您的项目结构将如下所示:

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

您需要更改三个子目录的权限.如果尚未安装,请安装适用于Windows的Git .

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

将要安装的程序之一称为Git BASH.它使您能够像在Linux或Mac上一样设置文件权限.运行该程序,并进入您的项目目录cd:

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/

执行以下命令:

  • chmod 775供应商/mpdf/mpdf/ttfontdata
  • chmod 775供应商/mpdf/mpdf/tmp
  • chmod 775供应商/mpdf/mpdf/graph_cache

测试您的Mpdf安装

Testing Your Mpdf Installation

  1. 如果尚未这样做,请使用以下代码在项目目录中创建index.php文件:

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

保存该文件,然后在浏览器(dev.yoursitename.com)中访问您的网站.如果您正确完成了所有操作,则应立即将您重定向到PDF.该PDF的唯一内容将是您在代码中定义的h1.这是我的:

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:

这篇关于使用php将html页面转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆