如何设置PHP wkhtmltopdf

首先参考:Composer三步曲:安装、使用、发布

在 php 路径下,应能打开命令行输入php -v能够看到php版本信息。

然后执行以下三条:

cpp 复制代码
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"

php composer-setup.php

php -r "unlink('composer-setup.php');"

这时遇到报错:

bash 复制代码
Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?

参考:https://www.cnblogs.com/ec04/p/12390050.html

1.windows下的PHP,只需要到php.ini中把extension=php_openssl.dll前面的;删掉,重启服务就可以了。

经查,我的 php 没有这一项,添加,然后 iisreset 。再次执行以上三条,通过,下载安装成功。

cpp 复制代码
All settings correct for using Composer
Downloading...

Composer (version 2.7.7) successfully installed to: C:\php\composer.phar
Use it: php composer.phar

C:\php>php comoser.phar
Could not open input file: comoser.phar

C:\php>php composer.phar
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.7.7 2024-06-10 22:11:12

最后参考:php使用wkhtmltopdf html转为ptf或图片_php wkhtmlto-CSDN博客

执行:composer require mikehaertl/phpwkhtmltopdf

这时有问题,需要改为:php composer.phar require mikehaertl/phpwkhtmltopdf

执行通过:

cpp 复制代码
./composer.json has been created
Running composer update mikehaertl/phpwkhtmltopdf
Loading composer repositories with package information
Updating dependencies
Lock file operations: 3 installs, 0 updates, 0 removals
  - Locking mikehaertl/php-shellcommand (1.7.0)
  - Locking mikehaertl/php-tmpfile (1.2.1)
  - Locking mikehaertl/phpwkhtmltopdf (2.5.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
  - Downloading mikehaertl/php-tmpfile (1.2.1)
  - Downloading mikehaertl/php-shellcommand (1.7.0)
  - Downloading mikehaertl/phpwkhtmltopdf (2.5.0)
 0/3 [>---------------------------]   0%
 2/3 [==================>---------]  66%
 3/3 [============================] 100%
  - Installing mikehaertl/php-tmpfile (1.2.1): Extracting archive
  - Installing mikehaertl/php-shellcommand (1.7.0): Extracting archive
  - Installing mikehaertl/phpwkhtmltopdf (2.5.0): Extracting archive
 0/3 [>---------------------------]   0%
 3/3 [============================] 100%
Generating autoload files
No security vulnerability advisories found.
Using version ^2.5 for mikehaertl/phpwkhtmltopdf

最后是如何使用:

(这里要吐槽一下:网上的示例为何都不讲 require_once('...') 呢?)

php 复制代码
//定位文件位置
require_once('C:\php\vendor\mikehaertl\php-tmpfile\src\File.php');
require_once('C:\php\vendor\mikehaertl\php-shellcommand\src\Command.php');
require_once('C:\php\vendor\mikehaertl\phpwkhtmltopdf\src\Command.php');
require_once('C:\php\vendor\mikehaertl\phpwkhtmltopdf\src\Image.php');
require_once('C:\php\vendor\mikehaertl\phpwkhtmltopdf\src\Pdf.php');
//生成图片
use mikehaertl\wkhtmlto\Image;
//生成PDF
use mikehaertl\wkhtmlto\Pdf;

//生成HTML和CSS
$html = "
    <!DOCTYPE html>
    <html lang=\"en\">
    <head>
        <meta charset=\"UTF-8\">
        <title>Title</title>
    </head>
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
            max-width: 600px;
            margin: 0 auto;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: center;
        }
        th {
            background-color:#f2f2f2;
        }
    </style>
    <body>
    <table>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
        </tr>
        <tr>
            <td>张三</td>
            <td>20</td>
            <td>男</td>
        </tr>
        <tr>
            <td>李四</td>
            <td>25</td>
            <td>女</td>
        </tr>
    </table>
    </body>
    </html>";
//$html可以是文件路径也可以直接放html
$image = new Image($html);
// 懒得设置环境变量,直接把wkhtmltopdf安装路径写到这里就好
$image->binary = 'C:\Program Files\wkhtmltopdf\bin\wkhtmltoimage';
$image->saveAs('test.png');

$pdf = new Pdf($html);
// 懒得设置环境变量,直接把wkhtmltopdf安装路径写到这里就好
$pdf->binary = 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf';
$pdf->saveAs('test.pdf');
相关推荐
火兮明兮16 分钟前
Python训练第四十三天
开发语言·python
ascarl20101 小时前
准确--k8s cgroup问题排查
java·开发语言
fpcc2 小时前
跟我学c++中级篇——理解类型推导和C++不同版本的支持
开发语言·c++
莱茵菜苗2 小时前
Python打卡训练营day46——2025.06.06
开发语言·python
爱学习的小道长2 小时前
Python 构建法律DeepSeek RAG
开发语言·python
luojiaao2 小时前
【Python工具开发】k3q_arxml 简单但是非常好用的arxml编辑器,可以称为arxml杀手包
开发语言·python·编辑器
终焉代码2 小时前
STL解析——list的使用
开发语言·c++
SoFlu软件机器人3 小时前
智能生成完整 Java 后端架构,告别手动编写 ControllerServiceDao
java·开发语言·架构
英英_3 小时前
视频爬虫的Python库
开发语言·python·音视频
猛犸MAMMOTH3 小时前
Python打卡第46天
开发语言·python·机器学习