如何设置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');
相关推荐
酒尘&1 小时前
JS数组不止Array!索引集合类全面解析
开发语言·前端·javascript·学习·js
冬夜戏雪1 小时前
【java学习日记】【2025.12.7】【7/60】
java·开发语言·学习
xwill*2 小时前
分词器(Tokenizer)-sentencepiece(把训练语料中的字符自动组合成一个最优的子词(subword)集合。)
开发语言·pytorch·python
咖啡の猫2 小时前
Python列表的查询操作
开发语言·python
quikai19813 小时前
python练习第三组
开发语言·python
JIngJaneIL3 小时前
基于Java非遗传承文化管理系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot
吃西瓜的年年3 小时前
1. 初识C语言
c语言·开发语言
CHANG_THE_WORLD4 小时前
Python 字符串全面解析
开发语言·python
不会c嘎嘎4 小时前
深入理解 C++ 异常机制:从原理到工程实践
开发语言·c++
永远都不秃头的程序员(互关)4 小时前
C语言 基本语法
c语言·开发语言