如何设置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');
相关推荐
ljp_nan几秒前
QT --- 初识QT
开发语言·qt
ᅠᅠᅠ@6 分钟前
异常枚举;
开发语言·javascript·ecmascript
编程版小新12 分钟前
C++初阶:STL详解(四)——vector迭代器失效问题
开发语言·c++·迭代器·vector·迭代器失效
c4fx32 分钟前
Delphi5利用DLL实现窗体的重用
开发语言·delphi·dll
鸽芷咕1 小时前
【Python报错已解决】ModuleNotFoundError: No module named ‘paddle‘
开发语言·python·机器学习·bug·paddle
Jhxbdks1 小时前
C语言中的一些小知识(二)
c语言·开发语言·笔记
java6666688881 小时前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存1 小时前
源码分析:LinkedList
java·开发语言
代码雕刻家1 小时前
数据结构-3.1.栈的基本概念
c语言·开发语言·数据结构
Fan_web1 小时前
JavaScript高级——闭包应用-自定义js模块
开发语言·前端·javascript·css·html