如何设置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 小时前
Swift + Xcode 开发环境搭建终极指南
开发语言·ios·swiftui·xcode·swift·苹果vision pro·swift5.6.3
萧鼎2 小时前
Python pyzmq 库详解:从入门到高性能分布式通信
开发语言·分布式·python
艾伦~耶格尔3 小时前
【集合框架LinkedList底层添加元素机制】
java·开发语言·学习·面试
yujkss3 小时前
Python脚本每天爬取微博热搜-终版
开发语言·python
yzx9910133 小时前
小程序开发APP
开发语言·人工智能·python·yolo
啊阿狸不会拉杆4 小时前
《算法导论》第 32 章 - 字符串匹配
开发语言·c++·算法
fakaifa5 小时前
点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
小程序·uni-app·php·源码下载·点大餐饮·扫码点单
武当豆豆5 小时前
C++编程学习(第25天)
开发语言·c++·学习
-Xie-7 小时前
Maven(二)
java·开发语言·maven
mftang7 小时前
Python可视化工具-Bokeh:动态显示数据
开发语言·python